Search code examples
copencvroi

Define image ROI with OpenCV in C


I have a question about how to define a ROI with OpenCV. I know what the definition of the function cvSetImageROI but I want to know if is possible to define a ROI with OpenCV with a different shape than rectangle. For example, define a ROI in a circle or in different form.

It is possible to do this?

cvSetImageROI(img1, cvRect(a, b, c, d));


Solution

  • The following code, which you could have found here or even here sets a circular ROI using a bit mask on your image.

    #include "cv.h"
    #include "highgui.h"
    
    int main(int argc, char** argv)
    {
        IplImage* src, * res, * roi;
    
        /* usage: <prog_name> <image> */
        if (argc < 2)
        {
            fprintf(stderr, "Usage: %s <image>\n", argv[0]);
            return 1;
        }
    
        src = cvLoadImage(argv[1], 1);
        res = cvCreateImage(cvGetSize(src), 8, 3);
        roi = cvCreateImage(cvGetSize(src), 8, 1);
    
        /* prepare the 'ROI' image */
        cvZero(roi);
    
        /* Note that you can use any shape for the ROI */
        cvCircle(
            roi,
            cvPoint(130, 100),
            50,
            CV_RGB(255, 255, 255),
            -1, 8, 0
        );
    
        /* extract subimage */
        cvAnd(src, src, res, roi);
    
        /*
         * do the main processing with subimage here.
         * in this example, we simply invert the subimage
         */
        cvNot(res, res);
    
        /* 'restore' subimage */
        IplImage* roi_C3 = cvCreateImage(cvGetSize(src), 8, 3);
        cvMerge(roi, roi, roi, NULL, roi_C3);
        cvAnd(res, roi_C3, res, NULL);
    
        /* merge subimage with original image */
        cvNot(roi, roi);
        cvAdd(src, res, res, roi);
    
        /* show result */
        cvNamedWindow(argv[1], 1);
        cvNamedWindow("res", 1);
        cvShowImage(argv[1], src);
        cvShowImage("res", res);
    
        cvWaitKey(0);
    
        /* be tidy */
        cvDestroyAllWindows();
        cvReleaseImage(&src);
        cvReleaseImage(&res);
        cvReleaseImage(&roi);
    
        return 0;
    }
    

    For a pentagon or hexagon just adapt it so to change the geometric shape of the mask. Check this SO Question: Using ROI in OpenCV?.