Otsu's method thresholding making a 'shroud'

亡梦爱人 提交于 2019-12-25 18:14:05

问题


I'm trying to threshold an image using Otsu's method in Opencv:

Although when I threshold it, some parts of the picture are completely surrounded by white and creates and ends up in Opencv not detecting all the contours in the image. This is what I get when I do Otsu's method thresholding usingret,thresh=cv2.threshold(blurred,0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU):

EDIT: Some people have asked for the code I am using so here it is:

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

cv2.imshow('Input Image', image)
cv2.waitKey(0)

blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.adaptiveThreshold(blurred,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
                               cv2.THRESH_BINARY_INV,81,2)
#ret, thresh = cv2.threshold(blurred,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
kernel = np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
#thresh_value = 70
#ret,thresh= cv2.threshold(blurred,thresh_value,255,cv2.THRESH_BINARY)

Now it makes some checkered noise:


回答1:


You do not need to manually find a sweet spot! Let OpenCV do it for you!

OpenCV has an adaptive thresholding algorithm exactly from problems like this, called adaptiveThreshold

This function divides the image into multiple sub-images, and thresholds each one individually. This means that it will find a nice threshold value for each part of the image and give you a nice and uniformly lit image. See this example.

Try this:

th3 = cv.adaptiveThreshold(blurred,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
        cv.THRESH_BINARY,11,2)

Update: Functions like these do not work perfectly out of the box. If it still creates artefacts like salt and pepper noise, you can try:

  • Significantly increasing the blockSize. This can ensure that each block has a letter inside, which will hopefully mean the threshold will be chosen better. (e.g. Dividing the image into 25 blocks instead of 100. A blocksize of 11 pixels is very small.)
  • First apply a blurring filter to ease out the bad spots creating the seasoning noise. (With the image name blurry I imagine that you've done this already.
  • First the simple threshold function to just removes some noise. For example setting all pixels above 5 and below 100 equal to zero. Then after that apply the adaptiveThreshold.
  • Follow @Mark`s advice by subtracting a blurred image from the original image. (See this thread)

I hope this helps!




回答2:


Instead of using Otsu's method try global thresholding method.

thresh_value = 50
ret,thresh= cv2.threshold(blurred,thresh_value,255,cv2.THRESH_BINARY)

change the thresh_value parameter until you get the result you want.

Get to know more about thresholding techniques please refer the documentation.



来源:https://stackoverflow.com/questions/51814912/otsus-method-thresholding-making-a-shroud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!