大津的方法阈值制作一个“裹尸布" [英] Otsu's method thresholding making a 'shroud'

查看:122
本文介绍了大津的方法阈值制作一个“裹尸布"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Opencv中使用Otsu的方法对图像进行阈值处理:

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

尽管当我设定阈值时,图片的某些部分完全被白色包围,并在Opencv中创建并最终没有检测到图像中的所有轮廓.这是我使用ret,thresh=cv2.threshold(blurred,0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)进行Otsu的方法阈值操作时得到的:

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):

有人要求输入我正在使用的代码,因此这里是:

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:

推荐答案

您无需手动找到最佳位置!让OpenCV为您做到!

OpenCV正是从这样的问题中获得了自适应阈值算法,称为

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.

尝试一下:

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:

  • 大大增加了blockSize.这样可以确保每个块的内部都有一个字母,这将希望可以更好地选择阈值. (例如,将图像分为25个块而不是100个.blocksize11个像素非常小.)
  • 首先应用模糊滤镜以消除产生调味噪声的坏点. (使用图像名称blurry,我想您已经完成了此操作.
  • 首先是简单的阈值函数只是消除一些噪音.例如,将高于5 和低于100的所有像素设置为零.然后应用adaptiveThreshold.
  • 按照@Mark的建议从原始图像中减去模糊的图像. (请参见线程)
  • 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)

我希望这会有所帮助!

这篇关于大津的方法阈值制作一个“裹尸布"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆