Ask Your Question
0

Problem with image ROIs

asked 2018-04-18 11:26:13 -0600

julian403 gravatar image

updated 2018-04-18 11:33:45 -0600

berak gravatar image

Hello All. Im having this problem:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /home/julian/opencv/opencv-3.2.0/modules/core/src/matrix.cpp, line 522 terminate called after throwing an instance of 'cv::Exception' what(): /home/julian/opencv/opencv-3.2.0/modules/core/src/matrix.cpp:522: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat

I know where is the problem but I dont know how to fix it. this is the code:

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <unistd.h>


using namespace cv;
using namespace std;

Mat frame1, frame2, frame3;
Mat img1, img2, img3, img4;
Mat _kernel;


int main(int argc,char ** argv)
{
    _kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size2i(3, 3), cv::Point2i(1, 1));


vector<int> compression_params;
compression_params.push_back(IMWRITE_PNG_COMPRESSION);
compression_params.push_back(1);  

  VideoCapture cap(0);
  if (!cap.isOpened()) {
    cerr << "ERROR: Unable to open the camera" << endl;
    return 0;
  }

  cout << "Start grabbing, press a key on Live window to terminate" << endl;

    cap >> frame1;
        usleep (10000);
        cap>>frame2;

        subtract(frame1, frame2, frame3);
        threshold(frame3, frame3, 15, 255, CV_THRESH_BINARY);
        morphologyEx(frame3, frame3, CV_MOP_OPEN, _kernel, cv::Point2i(-1, -1), 1);
        cv:: cvtColor(frame3, frame3, CV_BGR2GRAY);

        img1=frame3(Rect(0,0, frame3.cols/2,frame3.rows/2));
        img2=frame3(Rect(0,frame3.rows/2, frame3.cols/2, frame3.rows ));
        img3=frame3(Rect(frame3.cols/2,0, frame3.cols,frame3.rows/2));
        img4=frame3(Rect(frame3.cols/2,frame3.rows/2, frame3.cols,frame3.rows));


imwrite("img1.png", img1, compression_params);
imwrite("img2.png", img2, compression_params);
imwrite("img3.png", img3, compression_params);
imwrite("img4.png", img4, compression_params);


return 1;
}

The problem is on

img1=frame3(Rect(0,0, frame3.cols/2,frame3.rows/2));
            img2=frame3(Rect(0,frame3.rows/2, frame3.cols/2, frame3.rows ));
            img3=frame3(Rect(frame3.cols/2,0, frame3.cols,frame3.rows/2));
            img4=frame3(Rect(frame3.cols/2,frame3.rows/2, frame3.cols,frame3.rows));

But what other way can I use to cut an image?

Thanks all

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-04-18 11:29:35 -0600

berak gravatar image

updated 2018-04-18 11:55:56 -0600

take pen & paper, and try to draw your subrects.

img4 is clearly out of bounds. you can't start at frame3.cols/2 and have frame3.cols for width. (same for height, ofc.)

edit flag offensive delete link more

Comments

thanks. it works

img1=frame3(Rect(0,0,frame3.cols/2,frame3.rows/2));
img2=frame3(Rect(0, frame3.rows/2,frame3.cols/2, frame3.rows/2 ));
img3=frame3(Rect(frame3.cols/2,0,frame3.cols/2,frame3.rows/2));
img4=frame3(Rect(frame3.cols/2,frame3.rows/2,frame3.cols/2,frame3.rows/2));
julian403 gravatar imagejulian403 ( 2018-04-18 12:11:55 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-18 11:26:13 -0600

Seen: 3,348 times

Last updated: Apr 18 '18