Ask Your Question

How to set region of interest ROI??

asked 2013-12-07 10:44:16 -0500

Nate Beghuk gravatar image

updated 2013-12-07 11:30:33 -0500

Hello expert ! I'm a newbie.. Can anyone of you help me how to set region of interest(ROI) of an opening image?? I'm still learning opencv by myself from internet wthout any guidance from my stress trying to learn something new without any help..But Im not give up !!!

Actually I want to set a ROI on a wounded skin. Let say I open an image, then I want to set ROI in that image.. My ROI is in wounded area. Here is what Im trying. But there is no just open the image.Can anyone of you help me please??

#include <opencv\cv.h>
#include <opencv\highgui.h>

using namespace cv;

int main(int argc, char** argv)
  int key = 0;
  IplImage* img = cvLoadImage( "C:\\Users\\acer\\Documents\\Visual Studio        
  2012\\Projects\\o1.jpg" ); 
  cvNamedWindow( "Example1", CV_WINDOW_NORMAL );
  cvShowImage("Example1", img);
  cvReleaseImage( &img );
  cvDestroyWindow( "Example1" );

  /* sets the Region of Interest*/
  cvSetImageROI(img, cvRect(150, 50, 150, 250));

  return 0;

here example of the wounded image that i want to set ROI
image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-12-07 11:01:13 -0500

berak gravatar image

updated 2013-12-07 11:06:17 -0500

hi Nate, simple answer, - you have to set the roi before calling cvShowImage.

besides that , 2 remarks:

  • next time, please add the actual code, not a screenshot (will make it easier for others to try/modify it)

  • if you're just starting with opencv (and thus don't have a ton a of legacy code to support), please avoid the old c-api, support for that is fading quickly, please move over to c++ asap.

so, the more modern version of your code would be like this:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;

int main() 
    Mat img = imread("a:/very/long/path/to/o1.jpg");
    if ( img.empty() ) { /* bark!!*/ }
    Mat roi = img( Rect(150,50,150,250) );
    return 0; // yea, c++, no cleanup nessecary!

".But Im not give up !!!" - ;) yea.

edit flag offensive delete link more


Thank you so much -berak- :) sorry my bad. I already edit it..thanks for the reminder.

may ask you, what if my interest of region(ROI) exactly only on that wounded area, so what is the to do that?? Is it possible to point out my desired ROI point by point?? not in a rectangular shape..did you understand my question?

Thank you bro :)

Nate Beghuk gravatar imageNate Beghuk ( 2013-12-07 11:41:05 -0500 )edit

hey, no prob at all.

by nature, a ROI is a rectangle. what you probably wanted is to find the outline of the wounded region (baa, can't stare at the pic too long ;)

things get a bit more complicated here, you'll have to:

see it as assignment #2, try as good as you can, if you get really stuck, come back with a new question.

also, the docs are your friend !

berak gravatar imageberak ( 2013-12-07 11:57:08 -0500 )edit

hey bro. berak !! thanks a lot for the advice.. I will try it. Never give up ! hell yeahh

Nate Beghuk gravatar imageNate Beghuk ( 2013-12-07 22:34:21 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-12-07 10:44:16 -0500

Seen: 58,276 times

Last updated: Dec 07 '13