彩色图像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);

时顺序要对!大家可以试下,呵呵! 得意

你可能感兴趣的:(彩色图像RGB通道分别读到三个灰度图像)