⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cap.cpp

📁 自己写的一个摄像头视频捕捉文件,用的是vc and opencv,可以显示!
💻 CPP
字号:
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "cvaux.h"

void main()
{	
	int i,j,m,y,x;
	int frame_num = 0 ;
	IplImage* image2; 
	IplImage* frame = 0;
	CvCapture* capture = 0;
	unsigned char n1=0;
	unsigned char n2=128;
	char* yuv420,*yuvcif;
	FILE *fpout;
	int avi_width=320;
	int avi_height=240;
	int framelen=(int)(avi_width*avi_height*1.5);	
	char *s ="       show";
	fpout=fopen("file.yuv","wb+");

	capture = cvCaptureFromCAM(-1);
	yuv420 = new char[avi_width*avi_height*3/2];
	yuvcif = new char[152064];//352*288*3/2
	image2 = cvCreateImage(cvSize(avi_width,avi_height),IPL_DEPTH_8U,3);

	cvNamedWindow(s,-1);
	cvWaitKey(1);
	while(1)
	{
		frame = cvQueryFrame(capture );	
		cvShowImage(s,frame);

		cvCvtColor(frame, image2, CV_BGR2YCrCb);
		cvFlip(image2,image2,0);
		//////////////////////////////
		i=0;
		j=0;
		m=0;
		int UU= avi_height*avi_width;
		int VV=UU+UU/4;
		for (y = 0; y < avi_height; y++) 
		{ 
			for (x = 0; x < avi_width; x++)
			{ 
				yuv420[i] = ((char*)(  image2->imageData + image2->widthStep*y))[x*3];
				i++;
				if(y%2==0&&x%2==0)
				{
					yuv420[j+UU] = ((char*)(image2->imageData + image2->widthStep*y))[x*3+2];
					j++;
				}
				if(y%2==1&&x%2==0)
				{
					yuv420[m+VV] = ((char*)(image2->imageData + image2->widthStep*y))[x*3+1];
					m++;
				}
			}
		}
		//////////////////////////////////Y
		memset(&yuvcif[0],n1,352*24);
		for(i=0;i<240;i++)
		{	
			memset(&yuvcif[352*24+352*i],n1,16);
			memcpy(&yuvcif[352*24+16+352*i],&yuv420[320*i],320);
			memset(&yuvcif[352*24+16+320+352*i],n1,16);
		}
		memset(&yuvcif[352*264],n1,352*24);
		//////////////////////////////////U
		memset(&yuvcif[352*288],n2,176*12);
		for(i=0;i<120;i++)
		{	
			memset(&yuvcif[352*288+176*12+176*i],n2,8);
			memcpy(&yuvcif[352*288+176*12+8+176*i],&yuv420[320*240+160*i],160);
			memset(&yuvcif[352*288+176*12+8+160+176*i],n2,8);
		}
		memset(&yuvcif[352*288+176*132],n2,176*12);
		//////////////////////////////////V
		memset(&yuvcif[352*288+176*144],n2,176*12);
		for(i=0;i<120;i++)
		{	
			memset(&yuvcif[352*288+176*144+176*12+176*i],n2,8);
			memcpy(&yuvcif[352*288+176*144+176*12+8+176*i],&yuv420[320*240+160*120+160*i],160);
			memset(&yuvcif[352*288+176*144+176*12+8+160+176*i],n2,8);
		}
		memset(&yuvcif[352*288+176*144+176*132],n2,176*12);
		//////////////////////////////////
		fwrite(yuvcif,1,352*288*3/2,fpout);
		//////////////////////////////////
		frame_num++;
		if (frame_num == 100) {
			break;
		}		
		cvWaitKey(1);		
	}
	free(yuv420);
	free(yuvcif);
	fclose(fpout); 
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -