代码: 1.头文件 #include <stdio.h> #include <cv.h> #include <highgui.h> 2.自定义 Help 函数 void help() { printf ( "/n Read out RGB pixel values and store them to disk/nCall:/n" "./ReadRGB/n" "/n This will store to files blines.csv, glines.csv and rlines.csv/n/n" ); } 3.main函数部分 int main( int argc, char** argv ) { if(argc != 2) { help(); return -1; } cvNamedWindow( "Example9_1", CV_WINDOW_AUTOSIZE ); CvCapture* capture = cvCreateFileCapture( argv[1] ); if(!capture) { printf("/nCouldn't open %s/n",argv[1]); help(); return -1; } CvPoint pt1 = cvPoint(10,10); CvPoint pt2 = cvPoint(20,20); int max_buffer; IplImage *rawImage; int r[10000],g[10000],b[10000]; FILE *fptrb = fopen("blines.csv","w"); //Store the data here FILE *fptrg = fopen("glines.csv","w"); // for each color channel FILE *fptrr = fopen("rlines.csv","w"); CvLineIterator iterator; //MAIN PROCESSING LOOP: for(;;) { if( !cvGrabFrame( capture )) break; rawImage = cvRetrieveFrame( capture ); max_buffer = cvInitLineIterator(rawImage,pt1,pt2,&iterator,8,0); cvShowImage( "Example9_1", rawImage ); int c = cvWaitKey(10); for(int j=0; j<max_buffer; j++) { fprintf(fptrb,"%d,", iterator.ptr[0]); //Write blue value fprintf(fptrg,"%d,", iterator.ptr[1]); //green fprintf(fptrr,"%d,", iterator.ptr[2]); //red iterator.ptr[2] = 255; //Mark this sample in red CV_NEXT_LINE_POINT(iterator); //Step to the next pixel } //OUTPUT THE DATA IN ROWS: fprintf(fptrb,"/n"); fprintf(fptrg,"/n"); fprintf(fptrr,"/n"); } //CLEAN UP: printf("/nData stored to files: blines.csv, glines.csv and rlines.csv/n/n"); fclose(fptrb); fclose(fptrg); fclose(fptrr); cvReleaseCapture( &capture ); cvDestroyWindow( "Example9_1" ); }