OpenCV边界框 [英] OpenCV Bounding Box

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

问题描述

我正在使用C ++环境中的OpenCV软件。 目标是检测拳击手套,并在手套轮廓周围绘制边框

I am working on software using OpenCV in C++ environment. The objective is to detect a boxing glove and draw a bounding box around gloves contours.

我遇到的问题是,边界框被淹没不止一次,事实上绘制了多个框。我在过去几天里试图做的是以某种方式消除绘制的框的数量,只有一个大的边框绘制。

The problem I am running into is that the bounding box is drown more than once in fact multiple boxes are drawn. What I was trying to do over the past few days is to somehow eliminate the number of boxes drawn and have only one big bounding box drawn.

我正在寻找一些技术来填充整个对象,我相信在这种情况下真的会有帮助。

I was looking at some techniques to fill in the object int its whole which i believe would really help in this case.

下面我已经发布代码我用来实现图像中显示的结果:

Below I have posted the code i used to achieve the result displayed in the image:

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<Vec3f> vecCircles;               
vector<Vec3f>::iterator itrCircles;

while(1)
{
    Mat frame;
    cap >> frame; // get a new frame from camera
    /////////////////////
    Mat imgHSV;
    cvtColor( frame, imgHSV, CV_BGR2HSV );
    ////////////////////
    Mat blur_out;
    GaussianBlur(imgHSV, blur_out, Size(1,1),2.0,2.0);
    ////////////////////
    Mat range_out;
    inRange(blur_out, Scalar(100, 100, 100), Scalar(120, 255, 255), range_out);
    ////////////////////
    findContours(range_out, contours, hierarchy, CV_RETR_TREE,  CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

     /// Approximate contours to polygons + get bounding rects and circles
     vector<vector<Point> > contours_poly( contours.size() );
     vector<Rect> boundRect( contours.size() );
     vector<Point2f>center( contours.size() );
     vector<float>radius( contours.size() );

     for( int i = 0; i < contours.size(); i++ )
     { 
         approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
         boundRect[i] = boundingRect( Mat(contours_poly[i]) );
     }

     /// Draw polygonal contour + bonding rects
     Mat drawing = Mat::zeros( range_out.size(), CV_8UC3 );
     for( int i = 0; i< contours.size(); i++ )
     {
         Scalar color = Scalar(255,0,255);
         drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
         rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );          
     }

如果任何人可以提出一些提示或提供一些信息来源,我可以找到我的问题的答案。

If anyone could suggest some tips or provide some source of information where i can find answers for my problem.

EDIT (快速更新):

我设法改善输出图片安静地逐渐到一点安静快乐的结果。关键是使用erode&膨胀以及在我的 findContours()函数。我将 CV_RETR_TREE 更改为 CV_RETR_EXTERNAL 。还有一些其他小事情我处理,但结果是好的:

I managed to improve the output image quiet gradually to a point am quiet happy with the result. The Key was the usage of erode & dilation as well as in my findContours() function. I changed the CV_RETR_TREE to CV_RETR_EXTERNAL. There were a few other minor things i tackled but the result is good:

不知道我应该在这里写还是打开新线程....但现在我需要一些帮助组件标签并提取诸如中心点和面积的参数。 :)

Dont know if i should write this here or open new thread....But now I need some help with component labeling and extracting parameters such as center points and area. :)

推荐答案

您现在在每个轮廓周围绘制一个边框,findContour将在每个连接的白色或黑色组件

You currently draw a bounding box around each contour, and findContour will find a contour around each connected white or black component, of which there are many in your picture.

因此,我要做的第一件事是在阈值图像上用一些形态操作过滤掉所有的噪声:做一些打开和关闭,两者都是延长和侵蚀

So the first thing I would do is filter all that noise with some morphological operations on the thresholded image: do some opening and closing, both of which are combinations of dilation and erosion.

在您的情况下,像cvDilate 2次); cvErode(4次); cvDilate(2次)

In your case something like cvDilate (2 times); cvErode(4 times); cvDilate(2 times)

这应该将所有白色blob合并成一个平滑的blob,但中间的黑洞将保留。
你可以找到一个正确的大小,但更容易调用findContours与CV_RETR_EXTERNAL而不是CV_RETR_TREE,那么它将只返回最外面的轮廓。

This should merge all white blobs into one smooth blob, but the black hole in the middle will remain. You could find the right one by size, but it is easier to call findContours with the CV_RETR_EXTERNAL instead of CV_RETR_TREE, then it will only return the outermost contours.

这篇关于OpenCV边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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