1.读取图像/视频/视频播放控

来源:互联网 发布:mysql limit 动态参数 编辑:程序博客网 时间:2024/05/24 05:33

1.1图像读取

//#include <opencv2\opencv.hpp>  //#include<iostream>//using namespace cv;#include<highgui.h>int main(){    IplImage* pImage = cvLoadImage("lena.jpg");     cvNamedWindow("OpenCV Win", CV_WINDOW_AUTOSIZE);    cvShowImage("OpenCV Win", pImage);    cvWaitKey(0);    cvDestroyWindow("OpenCV Win");    cvReleaseImage(&pImage);    return 0;}

运行结果:



1.21视频读取


</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">#<span style="font-family: Arial, Helvetica, sans-serif;">include<highgui.h></span>
//using namespace cv;int main(  ){cvNamedWindow("OpenCV Win", CV_WINDOW_AUTOSIZE );CvCapture* capture = cvCreateFileCapture( "aa.avi");IplImage* frame;while (1){frame = cvQueryFrame( capture );if(!frame)break;cvShowImage( "OpenCV Win", frame );char c = cvWaitKey(10);//设定每一帧时间(ms)if(c == 27 )break;}cvReleaseCapture( &capture );cvDestroyWindow("aa");}

运行结果:



1.2视频播放控制(加滚动条)

</pre><pre name="code" class="cpp">//#include "stdafx.h"#include "cv.h"//#include <cxcore.h>#include "highgui.h"//设置全局变量,一个为滚动条的位置。回调函数需要用到的变量cvCapture也是全局变量,所以前面有g_,代表globalint g_slider_position = 0;CvCapture* g_capture = NULL;//回调函数,滚动条拖动时被调用 参数是滚动条的位置(整数) 此函数可以设置cvCapture对象的属性void onTrackbarSlide(int pos){    cvSetCaptureProperty(        g_capture,        CV_CAP_PROP_POS_FRAMES,        pos        );}//main函数int main(int argc, char** argv ){    cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);//只分配一帧的存储空间,此时指针指向avi的开头空间。    g_capture = cvCreateFileCapture("aa.avi");//获取视频的全部帧数frames    int frames = (int)cvGetCaptureProperty(        g_capture,        CV_CAP_PROP_FRAME_COUNT        );//创建滚动条    if(frames != 0){        cvCreateTrackbar(            "Position", //滚动条名称            "Video",//所属窗口            &g_slider_position,            frames,            onTrackbarSlide//当滚动条拖动时被触发            );    }    IplImage* frame;//进入while循环就开始读取avi文件    while (1){//将下一帧视频文件载入内存,返回一个对应当前帧的指针,不同与cvLoadImage为图像分配内存,cvQueryFrame使用已经在cvCapture结构中分配好的内存  frame = cvQueryFrame(g_capture);  if(!frame)   break;  cvShowImage("Video", frame);  char c = cvWaitKey(33);//显示每一帧之间有33毫秒的间隔  if(c == 27)   break; //如果在这间隔期间用户触发Esc按键 循环就退出 否则继续执行循环 }    cvReleaseCapture(&g_capture);    cvDestroyWindow("Video");    return 0;}

运行结果:


0 0
原创粉丝点击