OpenCV Examples Part 1

Nov 4, 2008 | Tags: OpenCV | del.icio.us del.icio.us | digg Digg

5. Convert RGB to Grayscale

Listing 4 below loads an RGB image and convert it to grayscale.

Listing 4: Convert RGB to grayscale

  1. /* load image */
  2. IplImage *src = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR );
  3.  
  4. /* get image properties */
  5. width  = src->width;
  6. height = src->height;
  7.  
  8. /* create new image for the grayscale version */
  9. IplImage *dst = cvCreateImage( cvSize( width, height ), IPL_DEPTH_8U, 1 );
  10.  
  11. /* CV_RGB2GRAY: convert RGB image to grayscale */
  12. cvCvtColor( src, dst, CV_RGB2GRAY );

To convert image color space, simply use cvCvtColor. This function supports several color spaces. Consult OpenCV Reference Manuals for the details.