Domanda

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));

È stato utile?

Soluzione

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

Altri suggerimenti

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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top