OPENCV 读取摄像头文件,转换到不同空间中

 

#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 一国外学者的博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值