Search code examples
c++opencvmat

setImageROI using MAT image instead of Iplimage


 cvSetImageROI(dst, cvRect(0, 0,img1->width,img1->height) );
 cvCopy(img1,dst,NULL);
 cvResetImageROI(dst);

I was using these commands to set image ROI but now i m using MAT object and these functions take only Iplimage as a parameter. Is there any similar command for Mat object? thanks for any help


Solution

  • You can use the cv::Mat::operator() to get a reference to the selected image ROI.

    Consider the following example where you want to perform Bitwise NOT operation on a specific image ROI. You would do something like this:

    img = imread("image.jpg",  CV_LOAD_IMAGE_COLOR);  
    
    int x = 20, y = 20, width = 50, height = 50;
    
    cv::Rect roi_rect(x,y,width,height);
    
    cv::Mat roi = img(roi_rect);
    
    /* ROI data pointer points to a location in the same memory as img. i.e.
     No separate memory is created for roi data */
    
    cv::Mat complement;
    cv::bitwise_not(roi,complement);
    complement.copyTo(roi);
    
    cv::imshow("Image",img);
    cv::waitKey();
    

    The example you provided can be done as follows:

    cv::Mat roi = dst(cv::Rect(0, 0,img1.cols,img1.rows));
    img1.copyTo(roi);