Define image ROI with OpenCV in C

前端 未结 3 1769
-上瘾入骨i
-上瘾入骨i 2020-12-15 14:30

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

相关标签:
3条回答
  • 2020-12-15 14:30

    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?.

    0 讨论(0)
  • 2020-12-15 14:31

    You are supposed to use masks to simulate non-rectangular ROI.

    0 讨论(0)
  • 2020-12-15 14:42

    Following is the code for Circular ROI. However, to change to a different shape simply replace CvCircle() function with some other shape. cvAnd(src, src, res, roi); is the most important masking function here.

    //(c) 2012 enthusiasticgeek for Stack overflow
    #include "cv.h"
    #include "highgui.h"
    
    #define REQUIRED_RADIUS (100)
    
    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);
        cvSetImageROI(src,cvRect(src->width/2-REQUIRED_RADIUS, src->height/2-REQUIRED_RADIUS,REQUIRED_RADIUS*2,REQUIRED_RADIUS*2)); 
        res = cvCreateImage(cvGetSize(src), 8, 3);
        roi = cvCreateImage(cvGetSize(src), 8, 1);
    
        /* prepare the 'ROI' image */
        cvZero(roi);
        cvZero(res);
    
        /* Note that you can use any shape for the ROI */
        cvCircle(
            roi,
            cvPoint(REQUIRED_RADIUS,REQUIRED_RADIUS),//src->width/2, src->height/2),
            REQUIRED_RADIUS,
            CV_RGB(255, 255, 255),
            -1, 8, 0
        );
    
        /* extract subimage */
        cvAnd(src, src, res, roi);
    
    
        /* show result */
        cvNamedWindow("res", 1);
        cvShowImage("res", res);
    
        cvWaitKey(0);
    
        /* be tidy */
        cvDestroyAllWindows();
        cvReleaseImage(&src);
        cvReleaseImage(&res);
        cvReleaseImage(&roi);
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题