#include <cv.h>
#include <highgui.h>
#include <math.h>
int main(int argc, char** argv)
{
IplImage* src = cvLoadImage( argv[1], 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);
cvSetImageCOI(src,1);
cvCopy(src,BlueChannel); //提取蓝色
cvSetImageCOI(src,2);
cvCopy(src,GreenChannel); //提取绿色
cvSetImageCOI(src,3);
cvCopy(src,RedChannel); //提取红色
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;
}
转自
http://blog.sina.com.cn/s/blog_71fa0df50100pw03.html