1 #include <highgui.h> 2 #include <cv.h> 3 4 #pragma comment (lib,"opencv_calib3d231d.lib") 5 #pragma comment (lib,"opencv_contrib231d.lib") 6 #pragma comment (lib,"opencv_core231d.lib") 7 #pragma comment (lib,"opencv_features2d231d.lib") 8 #pragma comment (lib,"opencv_flann231d.lib") 9 #pragma comment (lib,"opencv_gpu231d.lib") 10 #pragma comment (lib,"opencv_haartraining_engined.lib") 11 #pragma comment (lib,"opencv_highgui231d.lib") 12 #pragma comment (lib,"opencv_imgproc231d.lib") 13 #pragma comment (lib,"opencv_legacy231d.lib") 14 #pragma comment (lib,"opencv_ml231d.lib") 15 #pragma comment (lib,"opencv_objdetect231d.lib") 16 #pragma comment (lib,"opencv_ts231d.lib") 17 #pragma comment (lib,"opencv_video231d.lib") 18 19 /* 20 *《学习OpenCV》第四章第一题b部分 21 * 完成时间:19:24 3/10 星期日 2013 22 */ 23 24 int main() 25 { 26 char * file_path = "H:/TDDOWNLOAD/Video/1111.avi"; 27 CvCapture * capture = 0; 28 capture = cvCreateFileCapture(file_path); 29 30 // 载入视频文件失败 31 if(capture == NULL) 32 { 33 printf("Can't load the video file, quit... "); 34 return 0; 35 } 36 37 // 每一帧 38 IplImage * frame = cvQueryFrame(capture);; 39 // 灰度后的每一帧 40 IplImage * gray_frame = cvCreateImage( cvGetSize(frame), frame->depth, 1); 41 IplImage * gray_frame_3 = cvCreateImage(cvGetSize(frame), frame->depth, 3); 42 // 边缘检测后的每一帧 43 IplImage * canny_frame = cvCreateImage( cvGetSize(frame), frame->depth, 1); 44 IplImage * canny_frame_3 = cvCreateImage(cvGetSize(frame), frame->depth, 3); 45 // 聚合来的每一帧 46 IplImage * total_frame = cvCreateImage( cvSize(frame->width * 3, frame->height), 47 frame->depth, frame->nChannels); 48 49 while(1) 50 { 51 // 从视频文件读入数据 52 frame = cvQueryFrame(capture); 53 if(!frame) 54 break; 55 56 // 将读入数据转换为灰度图 57 cvCvtColor( frame, gray_frame, CV_RGB2GRAY ); 58 cvCvtColor( gray_frame, gray_frame_3, CV_GRAY2BGR ); 59 60 // 对图像做Canny边缘检测 61 cvCanny( gray_frame, canny_frame, 30, 100, 3); 62 cvCvtColor( canny_frame, canny_frame_3, CV_GRAY2BGR ); 63 64 // 将三幅图像分别复制到新的图像中 65 cvSetImageROI( total_frame, cvRect(0, 0, frame->width, frame->height)); 66 cvCopy( frame, total_frame, 0); 67 cvResetImageROI(total_frame); 68 69 cvSetImageROI( total_frame, cvRect(frame->width, 0, gray_frame->width, gray_frame->height)); 70 cvCopy( gray_frame_3, total_frame, 0); 71 cvResetImageROI(total_frame); 72 73 cvSetImageROI( total_frame, cvRect(frame->width*2, 0, canny_frame->width, canny_frame->height)); 74 cvCopy( canny_frame_3, total_frame, 0); 75 cvResetImageROI(total_frame); 76 77 // 在图像的三个不同的部分写上合适的文字标签 78 CvFont textFont = cvFont( 10, 1); 79 cvInitFont( &textFont, CV_FONT_HERSHEY_SIMPLEX, 0.5f, 0.5f, 0, 1 ); 80 81 cvPutText( total_frame, "Frame", cvPoint( 10, 20 ), &textFont, cvScalar( 0, 0, 255 ) ); 82 cvPutText( total_frame, "Frame_Gray", cvPoint( frame->width + 10, 20 ), &textFont, cvScalar( 0, 0, 255 ) ); 83 cvPutText( total_frame, "Frame_Canny", cvPoint( frame->width * 2 + 10, 20 ), &textFont, cvScalar( 0, 0, 255 ) ); 84 85 86 87 // 显示图像 88 cvShowImage("total", total_frame); 89 90 char c = cvWaitKey(33); 91 if(c == 27) 92 break; 93 } 94 cvReleaseImage( & gray_frame ); 95 cvReleaseCapture(&capture); 96 cvDestroyAllWindows(); 97 98 return 0; 99 }
运行结果: