import numpy as np import cv2 from matplotlib import pyplot as plt im = cv2.imread('riz.jpg') imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 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)) #imgray=imgray*0.5 #imgray=imgray.astype(np.uint8) if False: #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() print(im.shape) cv2.imwrite('riz_gray.png',imgray) # Otsu's thresholding ret,thresh1 = cv2.threshold(imgray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) print ("ret: "+str(ret)) #seuillage à la main #ret,thresh1 = cv2.threshold(imgray,150,255,cv2.THRESH_BINARY) cv2.imwrite('riz_thresh1.png',thresh1) kernel = np.ones((3,3),np.uint8) erosion = cv2.erode(thresh1,kernel,iterations = 2) cv2.imwrite('riz_thresh1_erosion.png',erosion) dilatation = cv2.dilate(erosion,kernel,iterations = 1) cv2.imwrite('riz_thresh1_erosion_dilatation.png',dilatation) cv2.imshow('image',thresh1) cv2.waitKey(0) cv2.destroyAllWindows()