如何去除由形态学引起的斑点扩展

2024-09-28 16:18:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个图像,我正在侵蚀和膨胀,就像这样:

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)

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

enter image description here

然后我就开始这样做了:

^{pr2}$

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

enter image description here

我如何摆脱这个不受欢迎的区域?有没有更好的方法来做我正在做的事?在


Tags: 图像imageself区域imgnponescv2
3条回答

使用不同类型的阈值(adaptivethreshold,它考虑了局部的亮度)已经可以解决您的问题:自适应阈值结果就是您要寻找的。在

enter image description here

[编辑:我冒昧地在Hough circles上添加了一些代码。我承认,为了得到一个好看的结果,我使用了这张图像的参数,尽管我不知道您需要什么类型的精度来解决此类问题]

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

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

从二值图像可以很容易地用Hough变换拟合一个圆。一旦你有了圆的外边界,我建议你把边界流血,然后把边界外的部分剪出来。在

另一种方法是调整阈值。看来你可以逃脱惩罚。你可能需要一些形态学操作来获得一个干净的边缘。使用磁盘内核将有助于在很大程度上保留形状。在

由于您的问题已经被回滚到其原始版本,我已经附加了一个解决方案,使用洪水填充,这对您的图像起作用。在

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

img = cv2.imread('image.png', 0)

h, w = img.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)

gray = cv2.blur(img,(5,5))
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
print maxLoc

fixed_range = True
connectivity = 4

flooded = img.copy()
mask[:] = 0
connectivity = 4 #8
flags = connectivity 
flags |= cv2.FLOODFILL_FIXED_RANGE
cv2.floodFill(flooded, mask, maxLoc, (255, 255, 255), (60,)*3, (60,)*3, flags)

thresh = cv2.threshold(flooded, 250, 255, cv2.THRESH_BINARY)[1]

titles = ['Original Image', 'Blurred', "Floodfill", "Threshold"]
images = [img, gray, flooded, thresh]
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()

enter image description here

相关问题 更多 >