Decreasing ROI
Is there a way to make already selected ROI a bit (5%-10%) smaller. I can't just set original ROI smaller, since i get it as a result of function. Basicly, i need to do something like this:
i wrote this code to train myself about this question. maybe it will be useful for newcomers like me ( Edited according to StevenPuttemans's advice
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
Mat shrinkMat(Mat src,int percent,bool clone=false)
{
if (percent>99)
return src;
Rect newroi;
newroi.width=(src.cols*percent)/100;
newroi.height=(src.rows*percent)/100;
newroi.x=(src.cols-newroi.width)/2;
newroi.y=(src.rows-newroi.height)/2;
if (clone) return src(newroi).clone();
return src(newroi);
}
int main()
{
Mat image(600,800,CV_8UC3,Scalar(220,220,220));
Mat newimage;
for (int i=2; i<99; i+=10)
{
newimage=shrinkMat(image,i);
rectangle(newimage,Rect(0,0,newimage.cols,newimage.rows),Scalar(rand()&255,rand()&255,rand()&255),3);
}
newimage=shrinkMat(image,45);
ellipse( newimage, Point(newimage.cols/2,newimage.rows/2), Size( newimage.cols/2, newimage.rows/2), 0, 0, 360, Scalar( 255, 0, 0 ), 2, 8, 0 );
newimage=shrinkMat(image,75,true);
ellipse( newimage, Point(newimage.cols/2,newimage.rows/2), Size( newimage.cols/2, newimage.rows/2), 0, 0, 360, Scalar( 255, 0, 0 ), 2, 8, 0 );
imshow( "Display window1", image );
imshow( "Display window2", newimage );
waitKey(0);
return 0;
}
@sturkmen, thanks for providing some code. Now make yourself a better piece by placing the region shrinking into a function that allows you to define a percentage :) would not be that hard but much more interesting to the topic starter!
@StevenPuttemans i changed my previous code above. it was useful for me to training . thank you for your advice and welcome your critique about new code.
Asked: 2015-04-30 08:16:07 -0600
Seen: 542 times
Last updated: May 24 '15
You can compute the new 5%-10% smaller rect from the found one (I have an idea of creating a mask like draw the filled rect on an empty mat, do an erosion with a 5% kernel size, get the new rect). Or just edit that function that returns you the rect (adding the erosion here?).
I guess you are using cv::Rect, which means you get top left coordinates (x,y) and rectangle width and height. You can play with erosion, or just use these values to calculate new, smaller rectangle. Didn't really check, but should be something like this (10%): x_new = x + w/0.1, y_new = y + h/0.1, w_new = w(0.9), h_new = h(0.9)
yes, but that is not a reduction of 10% and even if it was, then you are moving x by 10% of H and then reduce H by 10% you'll get the reduced rect moved to the right. I would do x = x + w0.05 and w = w0.9. the same for y and H ;)
yep, you're right! sorry for misleading comment :)