Ask Your Question
1

Decreasing ROI

asked 2015-04-30 08:16:07 -0600

Storiy gravatar image

updated 2015-10-04 06:23:57 -0600

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:image description

edit retag flag offensive close merge delete

Comments

2

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?).

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-04-30 08:33:43 -0600 )edit

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)

seeminglyHelpful gravatar imageseeminglyHelpful ( 2015-04-30 10:01:50 -0600 )edit
2

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

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-04-30 10:46:04 -0600 )edit

yep, you're right! sorry for misleading comment :)

seeminglyHelpful gravatar imageseeminglyHelpful ( 2015-05-04 09:53:15 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-04-30 16:09:55 -0600

updated 2015-05-24 14:01:48 -0600

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;
}
edit flag offensive delete link more

Comments

1

or if you know the amount pixels you want to decrease with you can just ROI -= size(x,x) x = size you want to decrease with.

215 gravatar image215 ( 2015-05-01 10:49:28 -0600 )edit
1

@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 gravatar imageStevenPuttemans ( 2015-05-04 04:13:49 -0600 )edit
1

@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.

sturkmen gravatar imagesturkmen ( 2015-05-24 14:10:00 -0600 )edit
1

This seems pretty fine! Thank you!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-05-27 08:05:19 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-30 08:16:07 -0600

Seen: 500 times

Last updated: May 24 '15