Ask Your Question

OpenCV setroi

asked 2017-04-06 21:12:10 -0600

tesh94 gravatar image

updated 2020-09-16 15:07:31 -0600

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() 
    Mat img = imread("D:/sample.jpg");
    if ( img.empty() ) { /* bark!!*/ }
    Mat roi = img( Rect(170,150,250,100));
    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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-04-07 01:43:35 -0600

berak gravatar image

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() 
    Mat img = imread("D:/sample.jpg");
    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);
    return 0;
edit flag offensive delete link more



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.


tesh94 gravatar imagetesh94 ( 2017-04-07 02:43:02 -0600 )edit

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

StevenPuttemans gravatar imageStevenPuttemans ( 2017-04-07 03:57:17 -0600 )edit

Question Tools

1 follower


Asked: 2017-04-06 21:12:10 -0600

Seen: 592 times

Last updated: Apr 07 '17