头文件:rewiew2.hpp
#pragma once
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
class visionagin
{
public:
int Readdemo(string& str);
void Readvideo(string& str);
};
类实现:
#include"review2.h"
int visionagin::Readdemo(string & str)
{
Mat temp=imread(str);
if (temp.empty())
{
std::cout << "can not open this picture !" << std::endl;
return -1;
}
namedWindow("the picture you wanted ", WINDOW_NORMAL);
imshow("the picture you wanted ", temp);
int c = waitKey(0);
}
void visionagin::Readvideo(string& str)
{
VideoCapture cap(str);//创建视频流,打开摄像头0:(0)
if (!cap.isOpened())
{
std::cout << "流打开失败" << std::endl;
}
VideoWriter writer;
Mat temp;
int code = cap.get(CAP_PROP_FOURCC);
int width = cap.get(CAP_PROP_FRAME_WIDTH);
int height = cap.get(CAP_PROP_FRAME_HEIGHT);
int count = cap.get(CAP_PROP_FRAME_COUNT);
int fps = cap.get(CAP_PROP_FPS);
writer.open("C:\\Users\\86176\\Downloads\\visionimage\\walk2.avi", code, fps, Size(width, height), true);//创建保存视频流 尺寸与原视频一致
if (!writer.isOpened())
{
std::cout << "保存流打开失败" << std::endl;
}
while (1)
{
cap.read(temp);
if (temp.empty())
{
break;
}
imshow("video", temp);
writer.write(temp);//保存
int c = waitKey(1000/fps);
if (27 == c)
{
break;
}
}
waitKey();//让视频停止在最后一帧,不闪退
cap.release();//释放
}
main.cpp:
#include"review2.h"
int main()
{
visionagin qd;
string dir_name("C:\\Users\\86176\\Downloads\\visionimage\\image.JPEG");
//qd.Readdemo(dir_name);
string vid_name("C:\\Users\\86176\\Downloads\\visionimage\\walk.avi");
qd.Readvideo(vid_name);
return 0;
}
|