为什么 OpenCV 中的 drawcontours 不会填充图像边缘的轮廓? [英] Why drawcontours in OpenCV doesn´t fill contours in the edge of the image?

查看:211
本文介绍了为什么 OpenCV 中的 drawcontours 不会填充图像边缘的轮廓?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过向图像添加 2 位帧,然后使用我的代码,最后裁剪图像以删除额外的帧来绕过该问题.这是一个丑陋的解决方案,但它有效!

I bypassed the problem by adding a 2-bit frame to the image, then using my code, and finally cropping the image to delete the extra frame. It is an ugly solution, but it works!

我遇到了一个问题,我不确定是错误还是我缺乏经验.我会尽可能清楚地总结一下:

I encountered a problem which I'm not sure if it´s a bug or just my lack of experience. I´ll try to summarize it as clearly as possible:

  1. 我得到一个二值图像,其中包含我要分析的彩色图像的轮廓(白色像素是我的算法检测到的轮廓的周长,其余为黑色).图像非常复杂,因为我想要检测的对象完全填充了图像(没有背景").

  1. I get a binary image which contains the contours of the color image I want to analyse (pixels in white are the perimeter of the contours detected by my algorithm, the rest is black). Image is pretty complex as the objects I want to detect fill the image entirely (no "background").

我对那个图像使用 findcontours:

I use findcontours with that image:

contours, hierarchy = cv2.findContours(image,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)

  • 然后for"循环检测面积小于X"像素且hierarchy[0][x][3] >= 0(让我们调用新数组contours_2")

  • Then a "for" loop detects the contours with an area lesser than "X" pixels and hierarchy[0][x][3] >= 0 (lets call the new array "contours_2")

    我在新图像中绘制了contours_2":

    I draw "contours_2" in a new image:

    cv2.drawContours(image=image2, contours=contours_2, contourIdx=-1, color=(255,255,255), thickness=-1)
    

  • 问题是 drawcontours 可以很好地绘制所有轮廓,但它没有填充"图像边界中的轮廓(即在图像边缘具有一个边界的轮廓).我尝试将图像的边框像素(如框架)设置为 True,但它不起作用,因为 findcontours 自动将这些像素设置为零(在函数描述中).

    The problem is that drawcontours draws all the contours good, BUT it doesnt "fill" the contours that are in the border of the image (this is, the ones that have one boundary in the edge of the image). I tried setting the border pixels of the image (like a frame) at True but it doesnt work, as findcontours sets automatically those pixels to zero (its in the function description).

    在前面的循环中使用 cv2.contourArea 可以很好地检测这些轮廓并返回正常值,因此无法知道绘制轮廓何时会忽略轮廓以及何时正确填充轮廓.cv2.isContourConvex 根本不起作用,因为将每个轮廓都返回为 false.

    Using cv2.contourArea in the previous loop detects those contours well and returns normal values, so there is no way to know when a contour will be ignored by drawcontours and when it will be correctly filled. cv2.isContourConvex doesn´t work at all, as returns every contour as false.

    我可以在绘制轮廓之前使用 cv2.convexHull 绘制那些边缘"轮廓,但我只需要在边缘上使用它(因为它会使轮廓变形,我想尽可能避免这种情况).问题是...我无法检测哪些轮廓在图像的边缘,哪些不在,并且可以使用 drawcontours.

    I'm able to draw those "edge" contours using cv2.convexHull before drawcontours on them, but I need to only use it on edges (because it deforms the contours and I want to avoid that as much as possible). The problem is... I have no way to detect which contours are in the edge of the image, and which ones are not and work OK with drawcontours.

    所以我想问一下为什么 drawContour 会这样,是否有办法让它填充边缘的轮廓.或者,另一种解决方案是找到如何检测图像边界中的轮廓(以便我可以在需要时应用凸包).

    So I´d like to ask why drawContour behaves like that and if there is some way to make it fill the contours in the edge. Or, another solution would be to find how to detect which contours are in the borders of the image (so I can apply convexhull when needed).

    推荐答案

    如果您只有 1 个轮廓并且使用 contourIdx=-1,它会认为轮廓中的每个点都是一个单独的轮廓.您需要将单个轮廓包装为一个列表(即 contours=[contours_2] 如果 contours_2 中只有一个轮廓)

    If you have only 1 contour and you use contourIdx=-1 it will think each point in your contour is a separate contour. You need to wrap your single contour as a list (i.e contours=[contours_2] if there is only one contour in contours_2)

    这篇关于为什么 OpenCV 中的 drawcontours 不会填充图像边缘的轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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