Ask Your Question
1

OpenCV setroi

asked Apr 7 '17

tesh94 gravatar image

updated Sep 16 '0

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");
    Mat img = imread("D:/sample.jpg");
    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.

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Apr 7 '17

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() 
{
    namedWindow("Example1");
    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);
    imshow("Example1",roi);
    waitKey(0);
    return 0;
}
Preview: (hide)

Comments

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

tesh94 gravatar imagetesh94 (Apr 7 '17)edit

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

StevenPuttemans gravatar imageStevenPuttemans (Apr 7 '17)edit

Question Tools

1 follower

Stats

Asked: Apr 7 '17

Seen: 1,292 times

Last updated: Apr 07 '17