我使用 Otsu 阈值将灰度 img 转换为二进制。但是准确率不是很高,我想提高一下。
从 matplotlib 导入 cv2 将 pyplot 导入为 plt
定义主():
imgpath="F:\\Final_Project\\dataset\\im0001.ppm"
img=cv2.imread(imgpath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Hello",img)
#grayscale
cv2.imshow('Gray image', gray)
cv2.waitKey(5000)
cv2.destroyAllWindows()
#histogram
plt.hist(gray.ravel(),256,[0,256]);
plt.show()
#otsu thresholding
blur = cv2.GaussianBlur(gray,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow("Otsu",th3)
cv2.waitKey(0)
cv2.destroyAllWindows()
# find otsu's threshold value with OpenCV function
thresh=-1
ret, otsu = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
print( "{} {}".format(thresh,ret) )
如果名称== “主”:主()