Define image ROI with OpenCV in C

那年仲夏 提交于 2019-11-29 00:36:14
Matteo

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

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

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;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!