如何正确地增加矩形的大小发现与cv::findContours
How to correctly increase the size of rectangle found with cv::findContours
我有一个函数,使用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)
相关文章:
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 概念中的cv限定符需要表达式参数列表
- 将CHW格式的浮点向量转换为cv::Mat
- 错误的cv::face FacemarkLBF实例化
- 如何检查给定的参数是否为 cv::noArray()?
- 开放 CV 中的动态内存分配,用于视频处理
- 如何在 opencv 中使用 cv::VideoCapture::waitAny()
- 错误:未定义对cv::cudacodec::createVideoReader的引用
- OpenCV 3.4.1 error readNetFromTensorflow 无法在 cv::d nn::ReadProtoFromBinaryFile 中打开 .pb
- C++:从GPU内存(cudaMemcpy2D)获取BGR图像(cv::Mat)
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- 将 cv::mat 转换为 QImage
- Inference pytorch C++ with alexnet and cv::imread image
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用
- cv::Normalise() 中的 L2_NORM 和 NORM_MINMAX 实现有什么区别?
- CTest 项目目录.未发现任何测试
- 请发现它在 dev 中工作的错误,但在 hackerrank 中不起作用
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- 将 cv::Mat 转换为 std::vector 的通用函数
- 如何正确地增加矩形的大小发现与cv::findContours