1. 程式人生 > >彩色影象RGB通道分別讀到三個灰度影象

彩色影象RGB通道分別讀到三個灰度影象

這個剛開始不知道怎麼弄,後來問了下網友,原來一個spit函式搞定!

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

int main()
{

	IplImage* src = cvLoadImage( "F:\\hehua.jpg", 1 );
	IplImage * RedChannel = cvCreateImage( cvGetSize(src), 8, 1);
	IplImage * GreenChannel = cvCreateImage( cvGetSize(src), 8, 1);
	IplImage * BlueChannel = cvCreateImage( cvGetSize(src), 8, 1);
	IplImage * alphaChannel = cvCreateImage( cvGetSize(src), 8, 1);
	cvSplit(src,BlueChannel,GreenChannel,RedChannel,0);

	cvNamedWindow( "RedChannel", 1 );
	cvNamedWindow( "GreenChannel", 1 );
	cvNamedWindow( "BlueChannel", 1 );
	cvNamedWindow( "lphaChannel", 1 );

	cvShowImage( "RedChannel", RedChannel );
	cvShowImage( "GreenChannel", GreenChannel );
	cvShowImage( "BlueChannel", BlueChannel );
	cvShowImage( "lphaChannel", alphaChannel );

	cvWaitKey(0);
	cvReleaseImage( &RedChannel );
	cvReleaseImage( &GreenChannel );
	cvReleaseImage( & BlueChannel );
	cvReleaseImage( & alphaChannel );

	return 0;
}

注意通道的順序,因為opencv內影象存放順序為BGR,所以再寫
cvSplit(src,BlueChannel,GreenChannel,RedChannel,0);

時順序要對!大家可以試下,呵呵!得意