Converting RGB videos to grayscale in OpenCV

cvtColor Converts an image from one color space to another.

Example

void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )

Parameters

src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC… ), or single-precision floating-point.
dst output image of the same size and depth as src.
code color space conversion code (see the description below).
dstCn number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code

Example

#include <cv.h>
#include <highgui.h>

using namespace std;
using namespace cv;

int main(int, char**)
{
	cvNamedWindow("grayImage", CV_WINDOW_AUTOSIZE);
	CvCapture* capture = cvCreateFileCapture("video.avi");
	IplImage* gray_out;
	IplImage* frame;
	while(1) 
	{
		frame = cvQueryFrame(capture);
		if( !frame ) break;

		gray_out = cvCreateImage( cvGetSize(frame), IPL_DEPTH_8U, 1 );

		// Converts Image from RGB Gray
		cvCvtColor(frame , gray_out, CV_RGB2GRAY);

		// displaying gray image
		cvShowImage("gray image", gray_out);

		char c = cvWaitKey(33);
		if( c == 27 ) break;
	}
	cvReleaseCapture(&capture);
	cvDestroyWindow("grayImage");
	return 0;
}
Share

You may also like...