如何正确增加使用 cv::findContours 找到的矩形的大小 [英] How to correctly increase the size of rectangle found with cv::findContours

查看:203
本文介绍了如何正确增加使用 cv::findContours 找到的矩形的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a function that uses cv::findContours to create rectangles around one or many detected objects which I then want to use to store a cropped image of each object. My problem is that cv::findContours draws its rectangle right around the object but since I also need parts of the background around the object I want to increase each rectangles size.

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

This could be easily done with something like rectangleVector[i] += cv::Size(10, 10). The problem with this though is that if the detected object is right on the corner of the image and I increase the rectangles size and then use the rectangle to crop an image of just the detected object the program will crash since the rectangle is not within the images area anymore.

我需要以某种方式检查矩形的位置,然后仅在生成的矩形不会超出边界时才增加其大小.

I need to somehow check where the rectangle is and then only increase its size if the resulting rectangle will not be out of bounds.

请在下面找到我的函数.

Please find below my function.

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

        }

    }
}

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

As you can see I tried to implement a way to check the position of the rectangle and then increase its size accordingly. Unfortunately it doesn't do what I want and only leaves me with really small cropped images. Whats the correct way to achieve this?

推荐答案

以下函数旨在用特定的填充"放大矩形,除非它大于它来自的 Mat.在这种情况下,它会将其缩放得更小.

The following function is meant to enlarge a rect with a certain "padding", unless it's bigger than the Mat it came from. In that case it scales it smaller.

/*!
 * \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;
}

这篇关于如何正确增加使用 cv::findContours 找到的矩形的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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