如何删除由形态学引起的blob的扩展 [英] How to remove an extension to a blob caused by morphology

查看:142
本文介绍了如何删除由形态学引起的blob的扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张我正在侵蚀和扩张的图像:

  kernel = np.ones((5, 5),np.float32)/ 1 
eroded_img = cv2.erode(self.inpainted_adjusted_image,kernel,iterations = 10)
dilated_img = cv2.dilate(eroded_img,kernel,iterations = 10)

以下是侵蚀和膨胀的结果:





然后我就像这样采取阈值:

  self.thresh = cv2.threshold(dilated_img,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] 

但是阈值给了我一个不需要的扩展,我在下面的图像中标记了(红线上方的区域是不需要的区域):





  import cv2 
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('image。 png',0)
thresh = cv2.threshold(img,210,255,cv2.ADAPTIVE_THRESH_MEAN_C)[1]
canny = cv2.Canny(thresh,50,150)
cimg = cv2。 cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,20,param1 = 50,param2 = 23,minRadius = 0,maxRadius = 0)
circles = np.uint16(np.around(circles))


for i in circle [0,:]:
#draw the outer circle
cv2.circle(cimg,(i [0],i [1]),i [2],(255,0,0),3)
#画圆圈的中心
cv2。 circle(cimg,(i [0],i [1]),2,(0,0,255),3)

titles = ['Original Image','Adaptive Thresholding',Canny ,Hough Circle]
images = [img,thresh,ca nny,cimg]
for x in xrange(4):
plt.subplot(2,2,i + 1),plt.imshow(images [i],'gray')
plt.title(titles [i])
plt.xticks([]),plt.yticks([])
plt.show()

如果还不够,请告诉我们。


I have an image that I'm eroding and dilating like so:

kernel = np.ones((5,5),np.float32)/1
        eroded_img = cv2.erode(self.inpainted_adjusted_image, kernel, iterations=10)
        dilated_img = cv2.dilate(eroded_img, kernel, iterations=10)

Here's the result of the erosion and dilation:

and then I'm taking a threshold of it like so:

self.thresh = cv2.threshold(dilated_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

But the threshold gives me an unwanted extension that I've marked in the image below (The region above the red line is the unwanted region):

How do I get rid of this unwanted region? Is there a better way to do what I'm doing?

解决方案

Working with a different type of threshold (adaptive threshold, which takes local brigthness into account) will already get rid of your problem: The adaptive threshold result is what you are looking for.

[EDIT: I have taken the liberty of adding some code on Hough circles. I admit that I have played with the parameters for this single image to get a nice looking result, though I do not know what type of accuracy you are needing for such a type of problem]

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('image.png',0)
thresh = cv2.threshold(img, 210, 255, cv2.ADAPTIVE_THRESH_MEAN_C)[1]
canny = cv2.Canny(thresh,50,150)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,20, param1=50,param2=23,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))


for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(255,0,0),3)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

titles = ['Original Image', 'Adaptive Thresholding', "Canny", "Hough Circle"]
images = [img, thresh, canny, cimg]
for i in xrange(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

Let us know if this is not yet sufficient.

这篇关于如何删除由形态学引起的blob的扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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