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

📄 cap.cpp

📁 网上AVI转YUV还真不少,就是不简单,我的这个算是很简单的了
💻 CPP
字号:
 #include <stdio.h>
 #include   <iostream.h> 
#include <stdlib.h>
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "cvaux.h"

char *s ="   show";
int avi_width=320;
int avi_height=240;
int framelength=10000;
IplImage* image2; 
CvCapture* capture = 0;
char* yuv420;
FILE *fpout;
void init(char *inputfilename,char *outputfilename);
void avitoyuv();
void getvideotoyuv();
int length;
int main()
{	
	char *inputfilename  = "d://snake.avi";
	char *outputfilename = "file2.yuv";
	init(inputfilename,outputfilename);
	avitoyuv();		 
	return 1;
}	

void init(char *inputfilename,char *outputfilename)
{
	capture = cvCaptureFromFile(inputfilename);
	fpout=fopen(outputfilename,"wb+");
	avi_width   = (int) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH );
	avi_height  = (int) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT );
	framelength = (int) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_COUNT );
	printf("avi_width = %d, avi_height = %d, framelength = %d\n",avi_width,avi_height,framelength);
	//////////////////////	
	if((yuv420=(char *)malloc(avi_width*avi_height*3/2)) == NULL)
	{	
		printf("Not Enough Memory!\n");
		exit(1);
	}
	image2 = cvCreateImage(cvSize(avi_width,avi_height),IPL_DEPTH_8U,3);	
	cvNamedWindow(s,1);
}
void avitoyuv()
{
	int frame_num =0;
	while(1)
	{		
		getvideotoyuv();					
		frame_num++;	

		printf("%5d\b\b\b\b\b",frame_num);
		if (frame_num == framelength )
		{
			printf("waitaaa\n");
			printf("waitbbb\n");

			break;
		}		
		cvWaitKey(1);	
	}
	free(yuv420);
	fclose(fpout); 

}

void getvideotoyuv()
{
	int i,j,m,y,x;
	IplImage* frame = 0;
	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] = ((uchar*)(  image2->imageData + image2->widthStep*y))[x*3];
			i++;
			if(y%2==0&&x%2==0)
			{
				yuv420[j+UU] = ((uchar*)(image2->imageData + image2->widthStep*y))[x*3+2];
				j++;
			}
			if(y%2==1&&x%2==0)
			{
				yuv420[m+VV] = ((uchar*)(image2->imageData + image2->widthStep*y))[x*3+1];
				m++;
			}
		}
	}
	/////////////////////////////////
	fwrite(yuv420,1,avi_width*avi_height*3/2,fpout);
}

⌨️ 快捷键说明

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