import numpy as np import cv2 from matplotlib import pyplot as plt im = cv2.imread('riz.jpg') imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) #imgray=imgray*0.5 #imgray=imgray.astype(np.uint8) # affichage histogramme: #https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_histograms/py_histogram_equalization/py_histogram_equalization.html hist,bins = np.histogram(imgray.flatten(),256,[0,256]) cdf = hist.cumsum() cdf_normalized = cdf * hist.max()/ cdf.max() plt.plot(cdf_normalized, color = 'b') plt.hist(imgray.flatten(),256,[0,256], color = 'r') plt.xlim([0,256]) plt.legend(('cdf','histogram'), loc = 'upper left') plt.show() #autre methode pour l'histogramme: #cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) hist = cv2.calcHist([imgray],[0],None,[256],[0,256]) #hist = cv2.calcHist( [hsv], [0, 1], None, [180, 256], [0, 180, 0, 256] ) #plt.imshow(hist,interpolation = 'nearest') #plt.show() #print(str(hist)) #seuillage brut ou manuel ret,thresh1 = cv2.threshold(imgray,150,255,cv2.THRESH_BINARY) #seuillage Otsu's thresholding ret2,th2 = cv2.threshold(imgray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) print("ret2: "+str(ret2)) cv2.imwrite('riz_out.png',im) cv2.imwrite('riz_gray.png',imgray) cv2.imwrite('riz_otsu.png',th2) kernel = np.ones((3,3),np.uint8) erosion = cv2.erode(th2,kernel,iterations = 1) cv2.imwrite('riz_otsu_erosion.png',erosion) dilation = cv2.dilate(erosion,kernel,iterations = 1) cv2.imwrite('riz_otsu_erosion_dilatation.png',dilation) cv2.imshow('image',th2) cv2.waitKey(0) cv2.destroyAllWindows()