如何正确增加使用cv :: findContours找到的矩形的大小

时间:2014-08-14 14:15:20

标签: c++ opencv

我有一个函数使用cv::findContours围绕一个或多个检测到的对象创建矩形,然后我想用它来存储每个对象的裁剪图像。我的问题是cv::findContours在对象周围绘制了矩形,但由于我还需要对象周围的部分背景,我想增加每个矩形的大小。

这可以通过rectangleVector[i] += cv::Size(10, 10)之类的东西轻松完成。但问题是,如果检测到的对象正好位于图像的一角,并且我增加了矩形的大小,然后使用矩形来裁剪刚检测到的对象的图像,程序将崩溃,因为矩形不在图像区域了。

我需要以某种方式检查矩形的位置,然后只有在生成的矩形不会超出范围时才增加其大小。

请在下面找到我的功能。

void ActualRec::objectDetection(){
    Mat temp;
    thresholdedImage.copyTo(temp);
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours( temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

    if (contours.size()<5){    
        vector<vector<Point>> contours_poly( contours.size() );
        vector<Rect> boundRect( 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]) );
        }


        for( int i = 0; i< contours.size(); i++ ){
            //won't do the job..
            Point BRx = boundRect[i].br();
            Point TLy = boundRect[i].tl();
            if(BRx.x-10 > 0) BRx.x-=10;
            if(BRx.y-10 > 0) BRx.y-=10;
            if(TLy.x+10 < thresholdedImage.cols-1) TLy.x+=10;
            if(TLy.y+10 < thresholdedImage.rows-1) TLy.y+=10;
            Rect finalRect(BRx,TLy);

            //store cropped images            
            vecCropped.push_back(originalImage(finalRect));
            vecCroppedTresh.push_back(thresholdedImage(finalRect));

        }

    }
}

正如您所看到的,我试图实现一种方法来检查矩形的位置,然后相应地增加其大小。不幸的是,它没有做我想要的事情,只留下了非常小的裁剪图像。什么是正确的方法来实现这一目标?

2 个答案:

答案 0 :(得分:5)

你可以先增加矩形。 之后,您可以测试边框:

// just increase your rect
cv::Rect yourIncreasedRect = ...;

// rect of the image borders
cv::Rect imageRect = cv::Rect(0,0,imageWidth,imageHeight);

// rect that contains all that is in BOTH rects:
cv::Rect fittingRect = yourIncreasedRect & imageRect;

希望这会有所帮助

这里有一些来自http://docs.opencv.org/modules/core/doc/basic_structures.html#rect

的矩形运算符
rect = rect +/- point (shifting a rectangle by a certain offset)
rect = rect +/- size (expanding or shrinking a rectangle by a certain amount)
rect += point, rect -= point, rect += size, rect -= size (augmenting operations)

rect = rect1 & rect2 (rectangle intersection)
rect = rect1 | rect2 (minimum area rectangle containing rect2 and rect3 )
rect &= rect1, rect |= rect1 (and the corresponding augmenting operations)

rect == rect1, rect != rect1 (rectangle comparison)

答案 1 :(得分:4)

以下功能用于扩大带有某种&#34; padding&#34;的矩形,除非它比它来自的Mat大。在这种情况下,它会缩小它。

/*!
 * \brief Enlarge an ROI rectangle by a specific amount if possible 
 * \param frm The image the ROI will be set on
 * \param boundingBox The current boundingBox
 * \param padding The amount of padding around the boundingbox
 * \return The enlarged ROI as far as possible
 */
Rect enlargeROI(Mat frm, Rect boundingBox, int padding) {
    Rect returnRect = Rect(boundingBox.x - padding, boundingBox.y - padding, boundingBox.width + (padding * 2), boundingBox.height + (padding * 2));
    if (returnRect.x < 0)returnRect.x = 0;
    if (returnRect.y < 0)returnRect.y = 0;
    if (returnRect.x+returnRect.width >= frm.cols)returnRect.width = frm.cols-returnRect.x;
    if (returnRect.y+returnRect.height >= frm.rows)returnRect.height = frm.rows-returnRect.y;
    return returnRect;
}