#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
int main()
{
int height,width,step,channels;
uchar *data,*data1,*data2,*data3;
int i,j,k;
IplImage *frame = NULL;
CvCapture *capture = cvCaptureFromCAM( CV_CAP_ANY );
frame = cvQueryFrame(capture);
IplImage* img1 = cvCreateImage( cvGetSize(frame), 8, 3 );/*for Red */
IplImage* img2 = cvCreateImage( cvGetSize(frame), 8, 3 );/*for Green*/
IplImage* img3 = cvCreateImage( cvGetSize(frame), 8, 3 );/*for Blue*/
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "mywindow1", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "mywindow2", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "mywindow3", CV_WINDOW_AUTOSIZE );
height = frame -> height;/*Height of frame stored in height*/
width = frame -> width;/*width of frame stored in width and the same way below*/
step = frame -> widthStep;
channels = frame -> nChannels;
data = (uchar *)frame -> imageData;
data1 = (uchar *)img1 -> imageData;
data2 = (uchar *)img2 -> imageData;
data3 = (uchar *)img3 -> imageData;
while (1){
frame = cvQueryFrame( capture );
for(k=0;k < (channels);k++)
{ for(i=0;i < (height);i++)
{ for(j=0;j < (width);j++)/*operating Width wise*/
{
if(k==0)
{
data1[i*step+j*channels+k]=data[(i)*step+j*channels+k];
}
else
data1[i*step+j*channels+k]=0;/*Make data in other channels zero*/
}
}
for(i=0;i < (height);i++)
{
for(j=0;j < (width);j++)
{
if(k==1)/*Second channel copied */
{
data2[i*step+j*channels+k]=data[(i)*step+j*channels+k];
}
else data2[i*step+j*channels+k]=0;/*Other than the Required Channel make image data Zero.*/
}
}
for(i=0;i < (height);i++)
{
for(j=0;j < (width);j++)
{
if (k==2)
{
data3[i*step+j*channels+k]=data[(height-i)*step+j*channels+k];
}
else data3[i*step+j*channels+k]=0;
}
}
}
if (cvWaitKey(100) == 27)
break;
cvShowImage( "mywindow", frame );
cvShowImage( "mywindow1", img1 );
cvShowImage( "mywindow2", img2 );
cvShowImage( "mywindow3", img3 );
}
cvReleaseCapture( &capture );
cvReleaseImage(&img1);
cvReleaseImage(&img2);
cvReleaseImage(&img3);
cvDestroyWindow( "mywindow" );/*Releasing all the Initialized sturctures*/
cvDestroyWindow( "mywindow1" );
cvDestroyWindow( "mywindow2" );
cvDestroyWindow( "mywindow3" );
return 0;
}
程序来源于:OPEN COMPUTRE VISION 一国外学者的博客