Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Problem with image cuts

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

click to hide/show revision 2
None

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

berak gravatar image

Problem with image cuts

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