1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| from __future__ import print_function
# Top-Hat morphological transformation
import cv2
import pylab
from pylab import *
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from matplotlib.pyplot import *
from skimage.morphology import white_tophat, black_tophat, square
from skimage.io import imread
from skimage.color import rgb2gray
import matplotlib.pylab as pylab
from skimage.morphology import white_tophat, black_tophat, square, disk
from skimage import io
import argparse
# https://skopas.pagesperso-orange.fr/Python%20et%20OpenCV-manuels/python_pratique.pdf
from scipy import misc
from scipy import ndimage
def plot_image(image, title=''):
pylab.title(title, size=20), pylab.imshow(image)
#pylab.axis('off') # comment this line if you want axis ticks
"""ap = argparse.ArgumentParser()
ap.add_argument("-i","--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())
originalImage = cv2.imread(args["originalImage"])"""
originalImage = cv2.imread('20160129-pt-3f9fed2f1c-std-cdf25fa63a-seq-1-ang-p31.4-p15.3-f-00084.png',0)
print("Affectation & affichage du type de : originalImage ")
originalImageType = originalImage.dtype
print("originalImage est de Type : ")
print(originalImageType)
print("image est de Type : ")
originalImageShape = np.shape(originalImage)
print(originalImageShape)
cv2.imshow("Original", originalImage)
print("max of 255: {}".format(cv2.add(np.uint8([200]), np.uint8([100]))))
print("min of 0: {}".format(cv2.subtract(np.uint8([50]), np.uint8([100]))))
print("wrap around: {}".format(np.uint8([200]) + np.uint8([100])))
print("wrap around: {}".format(np.uint8([50]) - np.uint8([100])))
M = np.ones(originalImage.shape, dtype = "uint8")*100
added = cv2.add(originalImage, M)
cv2.imshow("Added", added)
M = np.ones(originalImage.shape, dtype = "uint8")*50
subtracted = cv2.subtract(originalImage, M)
cv2.imshow("Subtracted", subtracted)
cv2.waitKey(20000)
#im = imread('../images/tagore.png')[...,3]
#im = imread('20160129-pt-3f9fed2f1c-std-cdf25fa63a-seq-1-ang-p31.4-p15.3-f-00084.png')[...,3]
im_bis = imread('20160129-pt-3f9fed2f1c-std-cdf25fa63a-seq-1-ang-p31.4-p15.3-f-00084.png')[...,3]
im_ter = cv2.imread('20160129-pt-3f9fed2f1c-std-cdf25fa63a-seq-1-ang-p31.4-p15.3-f-00084.png')[...,2]
# pbl resolu via : https://stackoverflow.com/questions/41196754/image-read-through-skimage-io-imread-have-suspicious-shape
im = io.imread('20160129-pt-3f9fed2f1c-std-cdf25fa63a-seq-1-ang-p31.4-p15.3-f-00084.png', plugin='matplotlib')
#img = cv2.imread('20160129-pt-3f9fed2f1c-std-cdf25fa63a-seq-1-ang-p31.4-p15.3-f-00084.png',0)
print("Affectation & affichage du type de : im_ter ")
im_terType = im_ter.dtype
print("im_ter est de Type : ")
print(im_terType)
print("im_ter est de Type : ")
im_terShape = np.shape(im_ter)
print(im_terShape)
#a1d = np.ones((512))
#a2d = np.expand_dims(a1d, axis=2)
#im_bis2D = a2d.shape
#print(im_bis2D)
#-------------------------------------------------
print("Affectation & affichage du type de : im_bis ")
im_bisType = im_bis.dtype
print("im_bis est de Type : ")
print(im_bisType)
print("im_bis est de Type : ")
im_bisShape = np.shape(im_bis)
print(im_bisShape)
#a1d = np.ones((512))
#a2d = np.expand_dims(a1d, axis=2)
#im_bis2D = a2d.shape
#print(im_bis2D)
#-------------------------------------------------
print("Affectation & affichage du type de : im ")
imType = im.dtype
print("im est de Type : ")
print(imType)
print("im_bis est de Type : ")
imShape = np.shape(im)
print(imShape)
im[im <= 0.5] = 0
im[im > 0.5] = 1
#im[im <= 0.25] = 0
#im[im > 0.25] = 1
#im1 = white_tophat(im, square(5))
#im2 = black_tophat(im, square(5))
# doesn_t work
#im1 = white_tophat(im, selem=None, disk(7)) # 37
#im2 = black_tophat(im, selem=None, disk(7)) # 37
#im1 = white_tophat(im, disk(7)) # 37
#im2 = black_tophat(im, disk(7)) # 37
# https://het.as.utexas.edu/HET/Software/Scipy/generated/scipy.ndimage.morphology.white_tophat.html#scipy.ndimage.morphology.white_tophat
# scipy.ndimage.morphology.white_tophat(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0)
im1 = scipy.ndimage.morphology.white_tophat(im, size=(8,8), footprint=(2,5), structure=(4,3), mode='constant', cval=1.0, origin=2)
# https://het.as.utexas.edu/HET/Software/Scipy/generated/scipy.ndimage.morphology.black_tophat.html#scipy.ndimage.morphology.black_tophat
# scipy.ndimage.morphology.black_tophat(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0)
im2 = scipy.ndimage.morphology.white_tophat(im, size=(8,8), footprint=(2,5), structure=(4,3), mode='constant', cval=1.0, origin=2)
print("Affectation & affichage du type de : im1 ")
im1Type = im1.dtype
print("im1Type est de Type : ")
print(im1Type)
print("Affectation & affichage du type de : im2 ")
im2Type = im2.dtype
print("im2Type est de Type : ")
print(im2Type)
pylab.figure(figsize=(20,15))
pylab.subplot(1,2,1), plot_image(im1, 'white tophat')
pylab.subplot(1,2,2), plot_image(im2, 'black tophat')
pylab.show()
# error message :
# RuntimeError: sequence argument must have length equal to input rank
# needed to be changed to a 3D one:
# Contrast Enhancement using Top-Hats
# Enhanced contrast image = f + WTH - BTH
#M = np.ones(originalImage.shape, dtype = "uint8")*100
f_WTH_added = cv2.add(im, im1)# M
cv2.imshow("f_WTH_added", f_WTH_added)
#M = np.ones(originalImage.shape, dtype = "uint8")*50
fPlusWTH_BTH_subtracted = cv2.subtract(f_WTH_added, im2) # M
cv2.imshow("fPlusWTH_BTH_subtracted", fPlusWTH_BTH_subtracted)
cv2.waitKey(20000)
# Saving the image
im_out_ECI_converted = cv2.convertScaleAbs(fPlusWTH_BTH_subtracted, alpha=(255.0))
cv2.imshow("im_out_ECI_converted", im_out_ECI_converted)
cv2.waitKey(20000)
cv2.imwrite('20160129-pt-3f9fed2f1c-std-cdf25fa63a-seq-1-ang-p31.4-p15.3-f-00084_ECI_TH.png',im_out_ECI_converted) |
Partager