用opencv简单显示图像和视频(代码有注释)

#include "stdafx.h" #include "cv.h" #include "highgui.h" void displayPic(char *argv) { IplImage* img = cvLoadImage( argv ); //opens a window on the screen that can //contain and display an image cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE ); //Whenever we have an image in the form of an IplImage* pointer, we can display it in an //existing window with cvShowImage(). //The cvShowImage() function requires that a named //window already exist (created by cvNamedWindow()). cvShowImage( "Example1", img ); //The cvWaitKey() function asks the program to stop and wait for a keystroke cvWaitKey(0); //free the allocated memory cvReleaseImage( &img ); cvDestroyWindow( "Example1" ); } void playVideo(char *argv) { cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE ); //Th e function cvCreateFileCapture() takes as its argument the name of the video file to be //loaded and then returns a pointer to a CvCapture structure CvCapture* capture = cvCreateFileCapture( argv ); IplImage* frame; while(1) //begin reading from the video file { //Unlike cvLoadImage(), which actually allocates memory for the //image, cvQueryFrame uses memory already allocated in the CvCapture structure //!thus no need to call cvReleaseImage() frame = cvQueryFrame( capture ); if( !frame ) break; cvShowImage( "Example1", frame ); //assume that it is correct to play //the video at 30 frames per second and allow user input to interrupt between each frame char c = cvWaitKey(33); if( c == 27 ) break; //Esc ASCII value == 27 } cvReleaseCapture( &capture ); cvDestroyWindow( "Example1" ); }

你可能感兴趣的:(用opencv简单显示图像和视频(代码有注释))