python - 检测位于图像opencv侧面的对象

标签 python opencv contour edge-detection canny-operator

我正在尝试检测图像中的多个对象;但是,有些对象位于边缘,因此图像中并未显示所有轮廓。我们如何才能以某种方式检测“被裁剪”的对象?我们可以将轮廓包围在图像的边缘吗?
首先,我对图像进行了模糊处理,应用了Canny检测器,进行了扩张,然后腐 eclipse 了边缘。
这是我的代码:

img = cv2.imread('porosity1.png')

img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

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

blur = cv2.GaussianBlur(gray, (7,7),0)

med = np.median(blur)

lower = int(max(0,0.7*med))

upper = int(min(255,1.3*med))

edged = cv2.Canny(blur, lower, upper)

edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)
这就是我进行边缘检测所得到的,这对我来说很好。
enter image description here
但是,当我要填充轮廓以检查检测器是否能够检测所有对象(甚至是图像侧面的对象)时,我得到以下信息:
gray, cnts, hierarchy = cv2.findContours(edged, mode = cv2.RETR_CCOMP,method = cv2.CHAIN_APPROX_NONE )

cnts1 = []

external_contours = np.zeros(gray.shape)

for i,cnt in enumerate(cnts):
    
    
    #External contours
    if cv2.contourArea(cnt)>100.0: #To exclude small contour areas
        cv2.drawContours(external_contours, cnts, i, 1, -1)
        cnts1.append(cnt)
        
        
        #Last column in each row in the hierarchy

plt.imshow(external_contours, cmap='gray')
enter image description here
我要检测的原因是我想找到对象的封闭区域。

最佳答案

您可以在找到的轮廓上使用cv2.convexHull函数,然后将关闭边缘上的轮廓。

关于python - 检测位于图像opencv侧面的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63390540/

相关文章:

python - 如何在 Canvas 上画线?

python - 在实时绘图期间在 matplotlib 中移动 x Axis (python)

python - 从许多文本文件中读取数字并对它们进行平均 Python

java - 使用 OpenCV 和 Java 将图像与背景图像进行比较以查找对象

c++ - OpenCV cmake 构建总是在 Windows 中抛出错误

c++ - 绘制从二值图像中检索到的轮廓

python - 加速 Tensorflow 2.0 渐变带

python - 使用边框从图像裁剪脸部

OpenCV : number of nested contours

python - 如何在 matplotlib 中根据 x、y、z 坐标绘制等高线图? (plt.contourf 或 plt.contour)