我有一个函数,用于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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我试图实现一种方法来检查矩形的位置,然后相应地增加其大小.不幸的是,它没有做我想要的事情,只留下了非常小的裁剪图像.什么是正确的方法来实现这一目标?
你可以先增加矩形.之后你可以测试边框:
// 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;
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
这里有一些来自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)
Run Code Online (Sandbox Code Playgroud)
以下函数旨在用特定的“填充”放大矩形,除非它比它来自的 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2812 次 |
| 最近记录: |