[zt]OpenCV如何获取视频当前的一帧图像
(OpenCV读取视频、OpenCV提取视频每一帧、每一帧图片合成新的AVI视频)CvCapture 是视频获取结构 被用来作为视频获取函数的一个参数 比如 CvCapture* cap; IplImage* cvQueryFrame( cap ); 从摄像头或者文件中抓取并返回一帧————————————————————————
#include "stdafx.h"
#include"highgui.h"
int main(int argc,char* argv[])
{
cvNamedWindow( "avi");
CvCapture* capture = cvCreateFileCapture( "D:\\sample.avi");
IplImage* frame;
while(1)
{
frame = cvQueryFrame(capture);
if(!frame) break ;
cvShowImage( "avi",frame);
char c = cvWaitKey(33);
if(c == 27)
break;
}
cvReleaseCapture(&capture);
cvDestroyWindow( "avi");
return 0;
}
( 1)视频文件路径没写对
( 2)没有安装解码器
( 3)如果使用的是 Opencv2.0或更高版本,那么,能否正确加载 opencv_ffmpeg210.dll
( 4)尽管是 AVI文件,但也可能使用了某种 codec,例如 :MJPEG Decompressor。 需要把它转换 OpenCV支持的 AVI文件 . OpenCV支持的AVI。例如使用狸窝全能视频转换器,在《预置方案》处,选择 AVI-Audio_Video Interleaved(*.avi)。或者使用格式工厂也可以。
( 5)读摄像头数据,需要安装与摄像头相应的驱动程序。
————————————————————
cvQueryFrame
// test3.cpp
//
// 该程序实现视频和图片的相互转换.
// Image_to_video()函数将一组图片合成AVI视频文件.
// Video_to_image()函数将AVI视频文件读入,将每一帧存储为jpg文件.
//
////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#define NUM_FRAME 300 //只处理前300帧,根据视频帧数可修改
void Video_to_image(char* filename)
{
printf("------------- video to image ... ----------------n");
//初始化一个视频文件捕捉器
CvCapture* capture = cvCaptureFromAVI(filename);
//获取视频信息
cvQueryFrame(capture);
int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
printf("tvideo height : %dntvideo width : %dntfps : %dntframe numbers : %dn", frameH, frameW, fps, numFrames);
//定义和初始化变量
int i = 0;
IplImage* img = 0;
char image_name[13];
cvNamedWindow( "mainWin", CV_WINDOW_AUTOSIZE );
//读取和显示
while(1)
{
img = cvQueryFrame(capture); //获取一帧图片
cvShowImage( "mainWin", img ); //将其显示
char key = cvWaitKey(20);
sprintf(image_name, "%s%d%s", "image", ++i, ".jpg");//保存的图片名
cvSaveImage( image_name, img); //保存一帧图片
if(i == NUM_FRAME) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("mainWin");
}
void Image_to_video()
{
int i = 0;
IplImage* img = 0;
char image_name[13];
printf("------------- image to video ... ----------------n");
//初始化视频编写器,参数根据实际视频文件修改
CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 30; // or 25
int frameW = 400; // 744 for firewire cameras
int frameH = 240; // 480 for firewire cameras
writer=cvCreateVideoWriter("out.avi",CV_FOURCC('X','V','I','D'),fps,cvSize(frameW,frameH),isColor);
printf("tvideo height : %dntvideo width : %dntfps : %dn", frameH, frameW, fps);
//创建窗口
cvNamedWindow( "mainWin", CV_WINDOW_AUTOSIZE );
while(i<NUM_FRAME)
{
sprintf(image_name, "%s%d%s", "image", ++i, ".jpg");
img = cvLoadImage(image_name);
if(!img)
{
printf("Could not load image file...n");
exit(0);
}
cvShowImage("mainWin", img);
char key = cvWaitKey(20);
cvWriteFrame(writer, img);
}
cvReleaseVideoWriter(&writer);
cvDestroyWindow("mainWin");
}
int main(int argc, char *argv[])
{
char filename[13] = "infile.avi";
Video_to_image(filename); //视频转图片
Image_to_video(); //图片转视频
return 0;
}
[zt]OpenCV如何获取视频当前的一帧图像的更多相关文章
- 从H264码流中获取视频宽高 (SPS帧) 升级篇
之前写过 <从H264码流中获取视频宽高 (SPS帧)> . 但发现很多局限性,而且有时解出来是错误的. 所以重新去研究了. 用了 官方提供的代码库来解析. 花了点时间,从代码库里单独把解 ...
- 从H264码流中获取视频宽高 (SPS帧)
获取.h264视频宽高的方法 花了2个通宵终于搞定.(后面附上完整代码) http://write.blog.csdn.net/postedit/7852406 图像的高和宽在H264的SPS帧中.在 ...
- iOS获取视频中的指定帧的两种方法
方法一 :AVFoundation #import <AVFoundation/AVFoundation.h> - (UIImage *)thumbnailImageForVideo:(N ...
- Opencv读取并获取视频属性
opencv中通过VideoCaptrue类对视频进行读取操作以及调用摄像头.常用的操作如下: 1.常用构造函数 1.VideoCapture类的构造函数:C++: VideoCapture::Vid ...
- opencv打开摄像头获取视频程序
// // main.cpp // opencv3 // // Created by PKU on 14-9-16. // Copyright (c) 2014年 PKU. All rights re ...
- Opencv+MFC获取摄像头数据,显示在Picture控件
分为两步:OpenCV获取摄像头数据+图像在Picture上显示 第一步:OpenCV获取摄像头数据 参考:http://www.cnblogs.com/epirus/archive/2012/06/ ...
- Opencv基础知识-----视频的读取和操作
Opencv读取视频代码 #include "stdafx.h" #include"highgui.h" int main(int argc,char* a ...
- 摄像头脸部识别 (1)opencv 抓取视频数据并保存
摄像头脸部识别 (1)opencv 抓取视频数据并保存 基于python 和 opencv 3.4.0 (兼容 opencv 2.X 参考注释),详细如代码 import numpy as np im ...
- Opencv学习笔记——视频进度条的随动
1. CvCapture结构体: CvCapture是一个结构体,用来保存图像捕获的信息,就像一种数据类型(如int,char等)只是存放的内容不一样,在OpenCv中,它最大的作用就是处理视频时(程 ...
随机推荐
- Javascript实现时钟
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- pythonchallenge之C++学习篇-00
前言 最近学习下C++,之前是python的用户,python解释器有诸多实现,其中最出名的要数C实现了,而且很多python的扩展模块可能要用C或者C++来写的,所以很有必要学习下C++了 为了避免 ...
- php echo return exit 区别
echo.print().printf().sprintf().vardump().varexport():都可以输出内容到网页,但不退出函数或程序. return:返回并立即退出,函数级别. die ...
- PopupWindow响应返回键的问题
假设情景是这样的:在一个Activity中弹出一个PopupWindow,要求在按返回键时关闭该PopupWindow. 如果该PopupWindow是无焦点的(默认情况),那么可以在Activity ...
- 【转】Struts2国际化
原文章:http://www.cnblogs.com/hellokitty1/p/5083663.html 简单理解 国际化简称i18n,其来源是英文单词 internationalizati ...
- 浅谈C++多态性
本文转载至http://blog.csdn.net/hackbuteer1/article/details/7475622 总结: (1)区分概念: 重载----同一个类中,相同的函数名字,不同 ...
- python重载四则运算符及输出格式设置
数学运算 Python 提供的基本数据类型 int.float 可以做整数和浮点的四则运算以及乘方等运算. 但是,四则运算不局限于int和float,还可以是有理数.矩阵等. 要表示有理数,可以用一个 ...
- Uva 11988 Broken Keyboard
因为要经常移动这些字符,所以采用的数据结构是链表. next[i]起到的作用大概就是类似链表里的next指针. 0.需要注意的是,判断cur == last ? 如果 是 则 last=i 1.另外一 ...
- coffeeScript学习01
安装 这里使用node.js npm install -g coffee-script # watch and compile coffee -w --output lib --compile src ...
- 20145223《Java程序设计》实验报告3
20145223 实验三<敏捷开发与XP实践> 实验内容 使用git上传代码 使用git相互更改代码 实验步骤: 一.使用git上传代码 $ git push 1.找到需要push的文件所 ...