示例程序011--彩色图像通道分割
// 017 彩色图片通道分割.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<cv.h>
#include<highgui.h>
using namespace std;
void ChannnelSplit()
{
IplImage *pImageChannel[4] = { 0, 0, 0, 0 }; //单通道图像数组
IplImage *pImageColor[4] = { 0, 0, 0, 0 }; //单色图像融合后的彩色图像数组
IplImage *pSrcImage = cvLoadImage( "lena.jpg", 1 ) ;
cout<<"通道数为:"<<pSrcImage->nChannels<<endl;
IplImage *pImage = cvCreateImage(cvGetSize(pSrcImage), pSrcImage->depth, pSrcImage->nChannels);
if( pSrcImage )
{
for( int i = 0; i < pSrcImage->nChannels; i++ )
{
pImageChannel[i] = cvCreateImage( cvGetSize(pSrcImage), pSrcImage->depth, 1 );
pImageColor[i] = cvCreateImage( cvGetSize(pSrcImage), pSrcImage->depth, 3 );
}
cvSplit( pSrcImage, pImageChannel[0], pImageChannel[1],pImageChannel[2], pImageChannel[3] );
cvMerge(pImageChannel[0],NULL, NULL,NULL, pImageColor[0] );
cvMerge( NULL, pImageChannel[1],NULL,NULL, pImageColor[1] );
cvMerge( NULL, NULL,pImageChannel[2],NULL, pImageColor[2] );
}
cvNamedWindow("b",CV_WINDOW_AUTOSIZE);
cvShowImage("b",pImageColor[0]);
cvNamedWindow("g",CV_WINDOW_AUTOSIZE);
cvShowImage("g",pImageColor[1]);
cvNamedWindow("r",CV_WINDOW_AUTOSIZE);
cvShowImage("r",pImageColor[2]);
cvWaitKey(0);
cvReleaseImage(&pImageChannel[0]);
cvReleaseImage(&pImageChannel[1]);
cvReleaseImage(&pImageChannel[2]);
cvReleaseImage(&pSrcImage);
cvDestroyAllWindows();
}
int main(int argc, char* argv[])
{
ChannnelSplit();
return 0;
}
运行结果: