内容简介:将一张名字为01的图片放到与cpp文件同一个文件夹下。编译运行成功,说明你的OpenCV配置正确了。在系统里随意搜了一个mp4文件,拷贝到cpp文件夹下,编译运行,按下任意键退出。根据LearningOpenCV3书上第一章里的例子综合而来,必要的解释已经添加在注释中。
#include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; int main(int argc, char *argv[]) { Mat img = imread("01.jpg", CV_LOAD_IMAGE_COLOR); if(img.empty()) return -1; namedWindow( "01", CV_WINDOW_AUTOSIZE ); imshow("01", img); waitKey(0); return 0; } 复制代码
将一张名字为01的图片放到与cpp文件同一个文件夹下。编译运行成功,说明你的OpenCV配置正确了。
打开第一个视频
#include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; int main(int argc, char *argv[]) { cv::namedWindow("box", cv::WINDOW_AUTOSIZE); cv::VideoCapture cap; cap.open("box.mp4"); cv::Mat frame; for(;;) { cap >> frame; if(frame.empty()) break; cv::imshow("box", frame); if(cv::waitKey(33) > 0) break; } return 0; } 复制代码
在系统里随意搜了一个mp4文件,拷贝到cpp文件夹下,编译运行,按下任意键退出。
带有进度条的视频播放
根据LearningOpenCV3书上第一章里的例子综合而来,必要的解释已经添加在注释中。
#include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> #include <fstream> using namespace cv; using namespace std; int g_slider_position = -1; // 为了在开始时激活回调函数,显示第一帧 int g_run = 1, g_dontset = 0; int lowThreshold = 50; // 边缘检测的下门限 int maxLowThreshold = 100; // 下门限的最低值 int upLowRatio = 3; // 上门限与下门限之比 VideoCapture g_cap; string windowName = "boxWindow"; string videoName = "box.mp4"; string trackbarName = "position"; string procWindow = "procWin"; string thresholdTrackbar = "threshold"; string pyrWindow = "pyrWindow"; cv::Mat frame; cv::Mat procFrame; cv::Mat pyrFrame; void onTrackbarSlide(int pos, void*) { g_cap.set(CAP_PROP_POS_FRAMES, pos); g_cap >> frame; // 该句执行完后g_cap.get(CAP_PROP_POS_FRAMES)会+1,除非frame为空 if(!frame.empty()) { imshow(windowName, frame); cvtColor(frame, procFrame, COLOR_BGR2GRAY); // 转灰度图 GaussianBlur(procFrame, procFrame, Size(5,5), 3, 3); // 高斯模糊 pyrDown(procFrame, pyrFrame); // 金字塔下采样 imshow(pyrWindow, pyrFrame); Canny(procFrame, procFrame, lowThreshold, lowThreshold * upLowRatio); // 边缘检测 imshow(procWindow, procFrame); } else { g_cap.set(CAP_PROP_POS_FRAMES, 0); // 重头开始 } // 在连续播放模式下通过鼠标移动了进度条时可以中断连续播放 if (!g_dontset) { g_run = 0; // 不允许自动前进一步 cout << " run mode paused! " << endl; } g_dontset = 0; } int main(int argc, char *argv[]) { namedWindow(windowName, cv::WINDOW_AUTOSIZE); namedWindow(procWindow, cv::WINDOW_AUTOSIZE); namedWindow(pyrWindow, cv::WINDOW_AUTOSIZE); if (!g_cap.open(videoName)) return -1; int frames = g_cap.get(CAP_PROP_FRAME_COUNT); int width = g_cap.get(CAP_PROP_FRAME_WIDTH); int height = g_cap.get(CAP_PROP_FRAME_HEIGHT); cout << "video has " << frames << " frames of dimensions(" << width << ", " << height << ")." << endl; createTrackbar(trackbarName, windowName, &g_slider_position, frames-1, onTrackbarSlide); // 进度条的范围是0~frames-1 createTrackbar(thresholdTrackbar, procWindow, &lowThreshold, 100); // 不需要回调函数,效果会在进入下一帧时显示 for(;;) { if (g_run != 0) { // 第一次执行时current_pos为0,所以在setTrackbarPos时会执行回调函数 int current_pos = g_cap.get(CAP_PROP_POS_FRAMES); cout << "Current pos = " << current_pos << endl; g_dontset = 1; // 该语句执行时,若current_pos与trackbar当前的游标位置不同,则会执行回调函数 setTrackbarPos(trackbarName, windowName, current_pos); g_run -= 1; cout << "g_dontset = " << g_dontset << endl; } char c = (char) waitKey(1); if (c == 's') { g_run = 1; cout << "Single mode, run = " << g_run << endl; } else if (c == 'r') { g_run = -1; cout << "Run mode, run = " << g_run << endl; } else if (c == 27) { break; } } g_cap.release(); destroyAllWindows(); return 0; } 复制代码
效果如图。
打开摄像头并录制视频
代码如下。
#include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> #include <fstream> using namespace cv; using namespace std; VideoCapture g_cap; string windowName = "Window"; string polarWinName = "polarWindow"; string outFile = "output.avi"; cv::Mat frame; cv::Mat polarFrame; int main(int argc, char *argv[]) { cv::namedWindow(windowName, cv::WINDOW_AUTOSIZE); cv::namedWindow(polarWinName, cv::WINDOW_AUTOSIZE); g_cap.open(0); if (!g_cap.isOpened()) { cerr << "Can not open camera!" << endl; } double fps = g_cap.get(CAP_PROP_FPS); // 我这里摄像头视频流的fps读出来是0,只好做个保护 fps = fps >= 1 ? fps : 30; int width = g_cap.get(CAP_PROP_FRAME_WIDTH); int height = g_cap.get(CAP_PROP_FRAME_HEIGHT); cout << "video is of dimensions(" << width << ", " << height << ") and fps = " << fps << endl; Size vidSize(width, height); VideoWriter writer; writer.open(outFile, CV_FOURCC('M', 'J', 'P', 'G'), fps, vidSize); for(;;) { g_cap >> frame; if(frame.empty()) break; imshow(windowName, frame); // 转成极坐标图像 logPolar(frame, polarFrame, Point2f(frame.cols/2, frame.rows/2), 40, WARP_FILL_OUTLIERS); imshow(polarWinName, polarFrame); writer.write(frame); // 保存录制视频 char c = (char) waitKey(33); if (c == 27) { break; } } g_cap.release(); writer.release(); destroyAllWindows(); return 0; } 复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。