视频中直线像素采样(cvInitLineIterator )把每个像素点的三个值写入csv文件中

#include <cv.h>
#include <highgui.h>
#include <stdio.h>

int main( int argc, char** argv )
{
// 	if( argc != 2 )
// 		return -1;
	cvNamedWindow( "Example", CV_WINDOW_AUTOSIZE );
	CvCapture* capture = cvCreateFileCapture( "video1.avi" );  //加载视频
	if( !capture )
		printf( "Couldn't open %s\n", argv[1] );
	CvPoint pt1 = cvPoint( 10, 10 );
	CvPoint pt2 = cvPoint( 10, 20 );  //直线两个顶点,读取这两个端点连成直线的像素点,总共11个像素点
	
	int max_buffer;
	IplImage* rawImage;
	int r[10000], g[10000], b[10000];  //存储r,g,b三维的像素值
	FILE* fptrb = fopen( "blines.csv","w" );  //创建文件存储数据
	FILE* fptrg = fopen( "glines.csv", "w" );  //存储每个通道的像素值
	FILE* fptrr = fopen( "rlines.csv", "w" );  //存储为csv文件
	CvLineIterator iter;  //采样迭代器
	
	int sign = 0;
	for( ;; )
	{
		if( !cvGrabFrame( capture ) )  //如果没捕捉到帧,退出,视频总共有68帧,30秒的画面
			break;
		rawImage = cvRetrieveFrame( capture );  //取回由函数cvGrabFrame抓取的图像
		max_buffer = cvInitLineIterator( rawImage, pt1, pt2, &iter, 8, 0);  //初始化直线迭代器,返回直线上两端像素点个数,总共11个
		cvShowImage( "Example", rawImage );   //显示每一帧,连续显示每一帧就是视频的的原理
	//	int c = cvWaitKey(0);
		for( int j = 0; j < max_buffer; ++j ) //max_buffer = 11
		{
			fprintf( fptrb, "%d,", iter.ptr[0] );  //写蓝值
			fprintf( fptrg, "%d,", iter.ptr[1] );
			fprintf( fptrr, "%d,", iter.ptr[2] );
			iter.ptr[2] = 255; //将直线标记为红色
			CV_NEXT_LINE_POINT( iter );  //移动指针,指向下一个pixel
		}
		fprintf( fptrb, "\n" );
		fprintf( fptrg, "\n" );
		fprintf( fptrr, "\n" );
		sign++;
		printf("\n\n%d", sign);
	}
	
	fclose( fptrb );
	fclose( fptrg );
	fclose( fptrr );
	cvReleaseCapture( &capture );
	cvDestroyWindow( "Example" );
	
	return 0;
}

你可能感兴趣的:(视频中直线像素采样(cvInitLineIterator )把每个像素点的三个值写入csv文件中)