OpenCV将Canny边缘转换为轮廓 [英] OpenCV converting Canny edges to contours

查看:1141
本文介绍了OpenCV将Canny边缘转换为轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OpenCV应用程序来自办公室内部的网络摄像头流(很多细节),我必须找到一个人工标记。标记是在白色背景的一个黑角规。我使用Canny来查找边缘和cvFindContours进行轮廓加工,然后使用aboutPolyDP和co。过滤和寻找候选人,然后使用局部直方图进一步过滤,bla bla bla ...

I have an OpenCV application fed from a webcam stream of an office interior (lot's of details) where I have to find an artificial marker. The marker is a black square on white background. I use Canny to find edges and cvFindContours for contouring, then approxPolyDP and co. for filtering and finding candidates, then use local histogram to filter further, bla bla bla...

这或多或少有效,但不是我想要的。 FindContours总是返回一个闭环,即使Canny创建一个非闭合线。我得到一个轮廓走在线的两侧形成一个循环。对于Canny图像(我的标记)上的封闭边缘,我得到2个轮廓,一个在内部,另一个在外部。
我对此操作有疑问:

This works more or less, but not exactly how I want. FindContours always returns a closed loop, even if Canny creates a non-closed line. I get a contour walking on both sides of the line forming a loop. For closed edges on the Canny image (my marker), I get 2 contours, one on the inside, and an other on the outside. I have to problems with this operation:


  • 我为每个标记得到2个轮廓(不是那么严重)

  • I get 2 contours for each marker (not that serious)

最无关紧要的过滤不可用(拒绝非闭合轮廓)

the most trivial filtering is not usable (reject non-closed contours)

所以我的问题:是否可以获得非封闭Canny边缘的非闭合轮廓?
或者解决上述问题的标准方法是什么?问题?

So my question: is it possible to get non-closed contours for non-closed Canny edges? Or what is the standard way to solve the above 2 issues?

Canny是一个非常好的工具,但我需要一种方法将2D黑白图像转换成易于处理的东西。类似于连接组件的东西,列出组件的步行顺序中的所有像素。所以我可以过滤循环,并将其输入到aboutPolyDP。

Canny is a very good tool, but I need a way convert the 2D b/w image, into something easily process-able. Something like connected components listing all pixels in walking order of the component. So I can filter for loops, and feed it into approxPolyDP.

更新:我错过了一些重要的细节:标记可以处于任何方向(它不是面向相机的正面,没有直角),实际上我正在做的是3D方向估计,基于标记的2D投影。

Update: I missed some important detail: the marker can be in any orientation (it's not front facing the camera, no right angles), in fact what I'm doing is 3D orientation estimation, based on the 2D projection of the marker.

推荐答案

<对于问题中的2个问题,我找到了一个干净而简单的解决方案。技巧是启用2级层次结构生成(在findCountours中)并查找具有父级的轮廓。这将返回封闭的Canny边缘的内部轮廓,仅此而已。 非封闭边缘会自动丢弃,每个标记都有一个轮廓。

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(CannyImage, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, Point(0,0) );
for (unsigned int i=0; i<contours.size(); i++)
    if (hierarchy[i][3] >= 0)   //has parent, inner (hole) contour of a closed edge (looks good)
        drawContours(contourImage, contours, i, Scalar(255, 0, 0), 1, 8);

它也可以反过来,即:寻找有孩子的轮廓(层次结构[ i] [2]> = 0),但在我的情况下,父检查产生更好的结果。

It also works the other way around, that is: look for contours which have a child (hierarchy[i][2] >= 0), but in my case the parent check yields better results.

这篇关于OpenCV将Canny边缘转换为轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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