Search code examples
c++opencviplimage

cvSetImageROI not working



I have a problem with cvSetImageROI not working. Please help me rectify the problem. I have an image(say, original_image for convention) which I am trying to divide into three equal parts. I am naming the three resultant rectangles as rectangle1, rectangle2, rectangle3 for convenience. Since my image is in the form of rectangle, so each resultant rectangle has 1/3rd of the original_image's width.
My idea for this image is, divide the width of the image by 3, so the result would be equal to each of the three rectangles' width.
Though when I try this, my setImageROI is not working.

I am posting the code snippet here for extracting rectangle1, Please guide me where I am going wrong. Her x and y values are the values of the rectangle of original_image.

int resultant_rectangle_width = ((int)(original_image->width/3));
int resultant_rectangle_height = (int)original_image->height;
cvSetImageROI(original_image, cvRect(x, y,resultant_rectangle_width,resultant_rectangle_height));

//this cout block was to check if the ROI was set on original_image,but it showed the original_image properties here.
cout<<"original_image width after setting roi:"<<original_image->width<<endl;
cout<<"original_image height after setting roi:"<<original_image->height<<endl;

//copying the ROI to another image
IplImage *rectangle1 = cvCreateImage(cvGetSize(original_image),original_image->depth,original_image->nChannels);
    cvCopy(original_image, rectangle1);//using three arguments also did not help
    cvResetImageROI(original_image);
    cvShowImage("rectangle1", rectangle1);

The rectangle1 that I am trying to show, shows wrong cropped image, i.e., with wrong height. I do not understand where I am changing the height of the original_image, for the rectangle1 to show wrong height.
I am expecting the code to give me an image that has same height as the original image, but with its width 1/3rd of the original image.


Solution

  • Tried your code, but I didn't find anything wrong. Maybe your x and y were not zero in the snippet.

    The cvWaitKey(0) are added just for debugging, it has no impact on your code.

    Your Code:

    #include <iostream>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/nonfree/features2d.hpp>
    #include <opencv2/nonfree/nonfree.hpp>
    #include <opencv2/opencv.hpp>
    using namespace std;
    
    int main(){
        IplImage *original_image = cvLoadImage("C:/1.jpg", 0);
        cvShowImage("orig", original_image);
        int x = 0;
        int y = 0;
    
        int resultant_rectangle_width = ((int)(original_image->width/3));
        int resultant_rectangle_height = (int)original_image->height;
    
        //Remember that (x,y) is the coordination of the up-left corner. 
        //Official Document: http://docs.opencv.org/2.4.9/modules/core/doc/basic_structures.html
        cvSetImageROI(original_image, cvRect(x, y,resultant_rectangle_width,resultant_rectangle_height));
    
        //this cout block was to check if the ROI was set on original_image,but it showed the original_image properties here.
        cout<<"original_image width after setting roi:"<<original_image->width<<endl;
        cout<<"original_image height after setting roi:"<<original_image->height<<endl;
        cvShowImage("3", original_image);
        cvWaitKey(0);
        //copying the ROI to another image
        IplImage *rectangle1 = cvCreateImage(cvGetSize(original_image),original_image->depth,original_image->nChannels);
        cvCopy(original_image, rectangle1);//using three arguments also did not help
        cvResetImageROI(original_image);
        cvShowImage("rectangle1", rectangle1);
        cvWaitKey(0);
        return 0;
    }
    

    Result:

    Whole image Whole Image

    ROI

    ROI

    As you can see, they are indeed the same height. So there might be a great possibility that you mistakenly set x and y to some wrong value.