[英]How to take a threshold of only a part of the image with OTSU thresholding?
I'm using OTSU threshold on a dilated and eroded image as shown below: 我在膨胀和侵蚀的图像上使用OTSU阈值,如下所示:
k = np.ones((5,5),np.float32)/1
d = cv2.dilate(self.img, k, iterations=10)
e = cv2.erode(d, k, iterations=10)
self.thresh = cv2.threshold(e, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
This is the eroded and dilated image, and the one that gets thresholded: 这是腐蚀和膨胀的图像,并且有阈值:
I only want the circular bright region in the middle to be obtained from thresholding, but instead I'm getting this result: 我只希望从阈值获得中间的圆形明亮区域,但我得到了以下结果:
How do I go about thresholding such that I only get the circular region in the middle, which also seems like the brightest (visually) part of the image? 我该如何进行阈值处理,以使我只获得中间的圆形区域,该区域也似乎是图像的最亮(视觉上)部分?
Note: To avoid playing around with different values, I want to stick to OTSU thresholding, but I'm open to ideas. 注意:为了避免使用不同的值,我想坚持使用OTSU阈值,但是我愿意接受想法。
You can try a gradient based approach. 您可以尝试基于梯度的方法。 Below I've used the morphological gradient. 下面,我使用了形态梯度。 I apply Otsu thresholding to this gradient image, followed by a similar amount of morphological closing (10 iterations), then take the morphological gradient of the resulting image. 我对该梯度图像应用Otsu阈值处理,然后进行相似数量的形态学闭合(10次迭代),然后对所得图像进行形态学梯度处理。
Now the regions are easy to detect. 现在,这些区域易于检测。 You can filter the circular region from the contours, for example, using an area based approach: using bounding box dimensions of the contour, you can get an estimate of the radius, then compare the calculated area to the contour area. 例如,您可以使用基于面积的方法,从轮廓中过滤出圆形区域:使用轮廓的边界框尺寸,可以获得半径的估计值,然后将计算出的面积与轮廓面积进行比较。
Don't know how generic this approach would be for your collection. 不知道这种方法对您的收藏有多通用。
Gradient image: intensity values scaled for visualization 渐变图像:强度值经过缩放以进行可视化
Binarized gradient image 二值化的梯度图像
Closed image 封闭图像
Gradient 梯度
im = cv2.imread('LDxOj.jpg', 0)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
morph = cv2.morphologyEx(im, cv2.MORPH_GRADIENT, kernel)
_, bw = cv2.threshold(morph, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
morph2 = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel, anchor = (-1, -1), iterations = 10)
morph3 = cv2.morphologyEx(morph2, cv2.MORPH_GRADIENT, kernel)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.