如何正确地增加矩形的大小发现与cv::findContours

How to correctly increase the size of rectangle found with cv::findContours

本文关键字:发现 cv findContours 正确地 增加      更新时间:2023-10-16

我有一个函数,使用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));
        }
    }
}

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

下面的函数是用来用一定的"padding"来扩大一个矩形,除非它比它来自的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;
}

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

// 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)