Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在OpenCV中获取多个小轮廓的外部轮廓_Python_Opencv - Fatal编程技术网

Python 在OpenCV中获取多个小轮廓的外部轮廓

Python 在OpenCV中获取多个小轮廓的外部轮廓,python,opencv,Python,Opencv,因此,我有以下由多个小块组成的图像,并希望获得其外部轮廓,如下所示: 我以前使用过轮廓近似和凸包函数来获得近似的外部轮廓,但它们只是由一个单独的轮廓组成,而在这种情况下,较小的部分很重要 我之前使用的函数与此函数类似: canvas = np.zeros(img.shape, np.uint8) img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) kernel = np.ones((5,5),np.float32)/25 img2gray = cv2

因此,我有以下由多个小块组成的图像,并希望获得其外部轮廓,如下所示:

我以前使用过轮廓近似和凸包函数来获得近似的外部轮廓,但它们只是由一个单独的轮廓组成,而在这种情况下,较小的部分很重要

我之前使用的函数与此函数类似:

canvas = np.zeros(img.shape, np.uint8)

img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kernel = np.ones((5,5),np.float32)/25
img2gray = cv2.filter2D(img2gray,-1,kernel)

ret,thresh = cv2.threshold(img2gray,120,255,cv2.THRESH_BINARY_INV)
im2,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

cnt = contours[0]
max_area = cv2.contourArea(cnt)

for cont in contours:
    if cv2.contourArea(cont) > max_area:
        cnt = cont
        max_area = cv2.contourArea(cont)

hull = cv2.convexHull(cnt)

cv2.drawContours(canvas, hull, -1, (0, 255, 0), 3)
正如您所猜测的,输出与期望值相差甚远:


关于如何使其更接近所需的轮廓,有什么想法吗?

您可以应用形态学操作来闭合轮廓,也许这会起作用

  kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
  thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
然后应用“扩张”操作来闭合具有小间隙的轮廓

  thresh = cv2.dilate(thresh, kernel,iterations = 1)

请给我你的反馈。

正如@Amine所说,形态学手术将是一条路,尤其是扩张术。可以找到更多信息。举一个小例子,你可以微调,但我认为它非常接近理想的输出

import cv2
import numpy as np

cv_img = cv2.imread('spot.jpg', 0)
im_copy = cv_img.copy()

kernel_dilation = np.ones((5,5), np.uint8)
dilation = cv2.dilate(cv_img, kernel_dilation, iterations=12)
ret, thresh = cv2.threshold(dilation, 127, 255, 0)

im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

cnt = contours[0]
max_area = cv2.contourArea(cnt)

for cont in contours:
    if cv2.contourArea(cont) > max_area:
        cnt = cont
        max_area = cv2.contourArea(cont)

cv2.drawContours(im_copy, [cnt], 0, (255, 255, 0), 3)
cv2.imshow('Contour', im_copy)
cv2.waitKey(0)
输出:

关于您想要的输出红线,底部,中间偏左一点:任何算法如何知道,如何在这里继续轮廓?为什么不允许进一步向上移动到小部分?在二值化之前你有原始图像吗?也许有更好的阈值解决方案!?请记住:这将是另一个问题,因此是一个新的、独立的SO问题。