# OpenCV setroi

Hello to whomever is reading this. I'm new to opencv and I've only recently installed opencv.

I would like to ask as to how do I go on about in setting the ROI of an image. An example is I have an image of an electric meter and I would just like to get the ROI of the values part of the electric meter. Not the entire electric meter. I would love to post some image as example but I'm lacking in karma as I'm a new user. Sorry about that

I've used this code over here

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

int main()
{
namedWindow("Example1");
if ( img.empty() ) { /* bark!!*/ }
Mat roi = img( Rect(170,150,250,100));
imshow("Example1",roi);
waitKey(0);
return 0; // yea, c++, no cleanup nessecary!
}


I've not set the proper rect for the ROI but for some reason I'm always getting an error even before I can test it out. The error has something to do with the + &ThisException 0x0000004532f7f6d0 {ExceptionCode=3765269347 ExceptionFlags=1 ExceptionRecord=0x0000000000000000 {ExceptionCode=...} ...} EHExceptionRecord *

When I use a different image, it isn't an issue.

Bear in mind I'm new to opencv so if the question does sound pretty noobish, sorry.

edit retag close merge delete

Sort by » oldest newest most voted

your example code is lacking 2 checks:

• did the image load at all ? ("bark" probably means, you have to do something ...)
• was the image large enough for the roi to fit in ?

int main()
{
namedWindow("Example1");
if ( img.empty() ) {
cout << "damn, it did not load !" << endl;
return -1;
}
Rect r(170,150,250,100);         // desired roi
Rect R(0,0,img.cols, img.rows);  // image size
r &= R;                          // crop if nessecary
Mat roi = img(r);
imshow("Example1",roi);
waitKey(0);
return 0;
}

more

1

yeap the size of the image was too small.

the image that was able to load had a larger size. i've sorted it out.

thanks

( 2017-04-07 02:43:02 -0500 )edit

I added some karma, so that you can actually accept this answer, instead of closing the topic.

( 2017-04-07 03:57:17 -0500 )edit

Official site

GitHub

Wiki

Documentation