OpenCV Circular ROI

Mar 3, 2010 | Tags: OpenCV | del.icio.us del.icio.us | digg Digg

A few days ago, a reader asked me about selecting circular ROI using OpenCV. I think such a function for selecting non-rectangular subimage would be useful, so I tried to write one.

OpenCV doesn't provide a function to make non-rectangle ROI, so I use image mask to do the trick. It works like this. say we have two images:

  • img - an RGB image.
  • mask - a black image, with white circle on it.

We can extract a region from img by:

res = img & mask

res now contains a portion from img where the white circle is located.

Selecting circular ROI and applying some processing on it.
Fig 1. Selecting circular ROI and applying some processing on it.

The full listing is shown below. The code loads an image, select circular ROI, and applying some processing on it.

Listing 1: Extract a circular ROI

  1. #include "cv.h"
  2. #include "highgui.h"
  3.  
  4. int
  5. main(int argc, char** argv)
  6. {
  7.     IplImage* src, * res, * roi;
  8.  
  9.     /* usage: <prog_name> <image> */
  10.     if (argc < 2)
  11.     {
  12.         fprintf(stderr, "Usage: %s <image>\n", argv[0]);
  13.         return 1;
  14.     }
  15.  
  16.     src = cvLoadImage(argv[1], 1);
  17.     res = cvCreateImage(cvGetSize(src), 8, 3);
  18.     roi = cvCreateImage(cvGetSize(src), 8, 1);
  19.  
  20.     /* prepare the 'ROI' image */
  21.     cvZero(roi);
  22.  
  23.     /* Note that you can use any shape for the ROI */
  24.     cvCircle(
  25.         roi,
  26.         cvPoint(130, 100),
  27.         50,
  28.         CV_RGB(255, 255, 255),
  29.         -1, 8, 0
  30.     );
  31.  
  32.     /* extract subimage */
  33.     cvAnd(src, src, res, roi);
  34.  
  35.     /*
  36.      * do the main processing with subimage here.
  37.      * in this example, we simply invert the subimage
  38.      */
  39.     cvNot(res, res);
  40.  
  41.     /* 'restore' subimage */
  42.     IplImage* roi_C3 = cvCreateImage(cvGetSize(src), 8, 3);
  43.     cvMerge(roi, roi, roi, NULL, roi_C3);
  44.     cvAnd(res, roi_C3, res, NULL);
  45.  
  46.     /* merge subimage with original image */
  47.     cvNot(roi, roi);
  48.     cvAdd(src, res, res, roi);
  49.  
  50.     /* show result */
  51.     cvNamedWindow(argv[1], 1);
  52.     cvNamedWindow("res", 1);
  53.     cvShowImage(argv[1], src);
  54.     cvShowImage("res", res);
  55.  
  56.     cvWaitKey(0);
  57.  
  58.     /* be tidy */
  59.     cvDestroyAllWindows();
  60.     cvReleaseImage(&src);
  61.     cvReleaseImage(&res);
  62.     cvReleaseImage(&roi);
  63.  
  64.     return 0;
  65. }
  66.  

In the example above, I only invert the subimage to make it simple. Change the line 39 with more complex image processing.

Related Articles

Recommended Books

4 Comments

anika on Apr 5, 2010:

Hi,
Can you suggest me how can I select a circular region from an image using mouse? I badly need your help.

Anika

Chaniel on Jul 2, 2010:

Hello, i was trying to make a runtime circular ROI but i don't know how to make the radius to increase with the drag of the mouse. Can you help me with this?

i would really appreciate it

Nathan Crock on Sep 30, 2010:

This is really clever! I think I'll use this sort of approach with my project. Thanks for sharing, Nash.

momin on Aug 1, 2011:

15.Dear Sir,

I would like to know the code to detect the centre of gravity of a object and to show gravity position on the object.

It will be highly appreciate if someone help me regarding this.

Thanks in advance.

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000