Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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;
}