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

C++ 如何正确增加使用cv::FindOnTours找到的矩形的大小,c++,opencv,C++,Opencv,我有一个函数,它使用cv::findContours围绕一个或多个检测到的对象创建矩形,然后我想用它来存储每个对象的裁剪图像。我的问题是cv::findContours在对象周围绘制矩形,但由于我还需要对象周围的部分背景,所以我希望增加每个矩形的大小 这可以很容易地用类似于rectangleVector[i]+=cv::Size(10,10)的东西来实现。但问题是,如果检测到的对象正好位于图像的拐角处,并且我增加了矩形的大小,然后使用矩形裁剪检测到的对象的图像,程序将崩溃,因为矩形不再位于图像区

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

        }

    }
}
void ActualRec::objectDetection(){
垫温;
阈值图像复制到(温度);
矢量等值线;
向量层次;
findContours(温度、轮廓、层次、CV_-RETR_-CCOMP、CV_-CHAIN_-SIMPLE);

如果(contours.size(),您可以先增加矩形。 之后,您可以测试边界:

// 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;
希望这有帮助

下面是来自


你可以先增加rect。 之后,您可以测试边界:

// 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;
希望这有帮助

下面是来自


下面的函数是用来放大一个带有特定“填充”的矩形,除非它比它来自的垫子大。在这种情况下,它会缩小它

/*!
 * \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;
}
/*!
*\尽可能将ROI矩形放大一个特定数量
*\param frm将设置ROI的图像
*\param boundingBox当前的边界框
*\param padding边界框周围的填充量
*\尽可能返回放大的ROI
*/
矩形放大(Mat frm、矩形边界框、整数填充){
Rect returnRect=Rect(boundingBox.x-填充,boundingBox.y-填充,boundingBox.width+(填充*2),boundingBox.height+(填充*2));
如果(returnRect.x<0)returnRect.x=0;
如果(returnRect.y<0)returnRect.y=0;
如果(returnRect.x+returnRect.width>=frm.cols)returnRect.width=frm.cols returnRect.x;
如果(returnRect.y+returnRect.height>=frm.rows)returnRect.height=frm.rows returnRect.y;
returnRect;
}

以下函数用于放大带有特定“填充”的矩形,除非它比它来自的垫子大。在这种情况下,它会将其缩放得更小

/*!
 * \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;
}
/*!
*\尽可能将ROI矩形放大一个特定数量
*\param frm将设置ROI的图像
*\param boundingBox当前的边界框
*\param padding边界框周围的填充量
*\尽可能返回放大的ROI
*/
矩形放大(Mat frm、矩形边界框、整数填充){
Rect returnRect=Rect(boundingBox.x-填充,boundingBox.y-填充,boundingBox.width+(填充*2),boundingBox.height+(填充*2));
如果(returnRect.x<0)returnRect.x=0;
如果(returnRect.y<0)returnRect.y=0;
如果(returnRect.x+returnRect.width>=frm.cols)returnRect.width=frm.cols returnRect.x;
如果(returnRect.y+returnRect.height>=frm.rows)returnRect.height=frm.rows returnRect.y;
returnRect;
}

完全做了我想做的事。谢谢!完全做了我想做的事。谢谢!这太棒了,我不知道!我也不知道可以使用&operator来做这件事,谢谢!这太棒了,我不知道!我也不知道可以使用&operator来做这件事,谢谢!