Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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,我是OpenCV新手,我正在尝试在特定轮廓内绘制外部轮廓。这是我用来澄清的图像(已经灰度化、阈值化等) 我想要的是在外矩形内找到所有圆的轮廓(总共120个) contours = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 因此我基本上使用了RETR\u EXTERNAL,但它只返回外部矩形。我试着使用RETR_TREE,但在这种情况下,它返回的轮廓比圆多,出于某种原因我不明白。澄清一下:我只希望每

我是OpenCV新手,我正在尝试在特定轮廓内绘制外部轮廓。这是我用来澄清的图像(已经灰度化、阈值化等)

我想要的是在外矩形内找到所有圆的轮廓(总共120个)

contours =  cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
因此我基本上使用了
RETR\u EXTERNAL
,但它只返回外部矩形。我试着使用
RETR_TREE
,但在这种情况下,它返回的轮廓比圆多,出于某种原因我不明白。澄清一下:我只希望每个圆有一个轮廓

如何使用
RETR\u EXTERNAL
并忽略外轮廓(矩形),使其仅返回圆?

按区域过滤轮廓: 我按面积过滤轮廓以隔离圆。我认为您可能需要对图像进行更多的处理,以帮助从图像中的边界描绘圆圈。我使用了以下代码:

import cv2
import numpy as np

img = cv2.imread("/your/path/C03eN.jpg")

def find_contours_and_centers(img_input):

    img_gray = cv2.cvtColor(img_input, cv2.COLOR_BGR2GRAY)
    img_gray = cv2.bilateralFilter(img_gray, 3, 27,27)
    #(T, thresh) = cv2.threshold(img_input, 0, 100, 0)
    _, contours_raw, hierarchy = cv2.findContours(img_gray, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    contours = [i for i in contours_raw if cv2.contourArea(i) > 20]
    contour_centers = []

    for idx, c in enumerate(contours):
        M = cv2.moments(c)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])
        samp_bounds = cv2.boundingRect(c)
        contour_centers.append(((cX,cY), samp_bounds))

    print("{0} contour centers and bounds found".format(len(contour_centers)))

    contour_centers = sorted(contour_centers, key=lambda x: x[0])

    return (contours, contour_centers)

conts, cents = find_contours_and_centers(img.copy())

circles = [i for i in conts if np.logical_and((cv2.contourArea(i) > 650),(cv2.contourArea(i) < 4000))]

cv2.drawContours(img, circles, -1, (0,255,0), 2)

cv2.imwrite("/your/path/tester.jpg", img)
结果:

谢谢你的回复,但为什么绿色“圆圈”的形状如此糟糕?当我画圆的时候,它们看起来很好,但问题是每个圆都有一个以上的轮廓,这是我不想要的。。。理想情况下,我希望删除外部矩形,这样我就可以使用
RETR\u EXTERNAL
,这很好。有什么办法吗?不确定,我只是下载了图像并试用了一下,也许你这边的分辨率更好,我的测试图像是低分辨率的。所以你问题的最后一部分是:“我如何使用RETR_EXTERNAL并忽略外轮廓(矩形),使其只返回圆?”我只是将其作为你的预期解决方案,然后尝试只返回圆。好的,我理解,但是没有更简单的方法使外矩形变黑(就像背景一样),这样我就可以使用
RETR\u EXTERNAL
?是的,你可以使用它返回外框的轮廓,然后你可以创建一个遮罩,画一个黑色轮廓来隐藏圆,或者完全提取图像的该部分以隔离感兴趣的区域。在这种情况下,您需要使用层次结构的概念。
import cv2
import numpy as np

img = cv2.imread("/your/path/C03eN.jpg")

def find_contours_and_centers(img_input):

    img_gray = cv2.cvtColor(img_input, cv2.COLOR_BGR2GRAY)
    img_gray = cv2.bilateralFilter(img_gray, 3, 27,27)
    #(T, thresh) = cv2.threshold(img_input, 0, 100, 0)
    #_, contours_raw, hierarchy = cv2.findContours(img_gray, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    _, contours_raw, hierarchy = cv2.findContours(img_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = [i for i in contours_raw if cv2.contourArea(i) > 20]
    contour_centers = []

    for idx, c in enumerate(contours):
        M = cv2.moments(c)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])
        samp_bounds = cv2.boundingRect(c)
        contour_centers.append(((cX,cY), samp_bounds))

    print("{0} contour centers and bounds found".format(len(contour_centers)))

    contour_centers = sorted(contour_centers, key=lambda x: x[0])

    return (contours, contour_centers)

conts, cents = find_contours_and_centers(img.copy())

x,y,w,h = cv2.boundingRect(conts[0])

cropped = img[y+10:y+(h-10),x+10:x+(w-10)]

cv2.imwrite("/your/path/cropped.jpg", cropped)