opencv学习(1)
#include <iostream> //#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; using namespace std; int g_slider_position = 0; int g_run = 1, g_dontset = 0; VideoCapture g_cap; int show_image() { cv::Mat img = cv::imread("/home/unicoe/CLionProjects/opencv/test1/img/example.jpg"); if(img.empty()) return -1; cv::namedWindow("window", cv::WINDOW_AUTOSIZE); cv::imshow("window", img); cv::waitKey(3000); cv::destroyWindow("window"); std::cout << "Hello, opencv!" << std::endl; return 0; } /* * 其中 #include<opencv2/opencv.hpp> * 包含了所有的 opencv所包含的库 * * * imread 能使用的图片格式 * including BMP, DIB, JPEG, JPE, PNG, PBM, PGM, PPM, SR, RAS, and TIFF. * */ int show_vedio() { namedWindow("video", WINDOW_AUTOSIZE); VideoCapture cap; cap.open("/home/unicoe/CLionProjects/opencv/test1/vedio/example.flv"); Mat frame; for(;;){ cap >> frame; if (frame.empty()) break; imshow("video", frame); if(waitKey(33)>=0) break; } return 0; } void onTrackbarSlider(int pos, void *) { g_cap.set(CAP_PROP_POS_FRAMES, pos); if(!g_dontset) g_run = 1; g_dontset = 0; } int show_TrackbarSlider() { namedWindow("TrackbarSlider", WINDOW_NORMAL); g_cap.open("/home/unicoe/CLionProjects/opencv/test1/vedio/example.flv"); int frames = (int) g_cap.get(CAP_PROP_FRAME_COUNT); int tmpw = (int)g_cap.get(CAP_PROP_FRAME_WIDTH); int tmph = (int)g_cap.get(CAP_PROP_FRAME_HEIGHT); cout << "video has " << frames << " frames of dimensions ( " << tmpw << " , " << tmph << ")."<<endl; createTrackbar("Position", "TrackbarSlider", &g_slider_position, frames, onTrackbarSlider); Mat frame; for(;;) { if(g_run != 0) { g_cap >> frame; if(frame.empty()) break; int cur_pos = (int) g_cap.get(CAP_PROP_POS_FRAMES); g_dontset = 1; setTrackbarPos("Position", "TrackbarSlider", cur_pos); imshow("TrackbarSlider", frame); g_run = -1; } char c = (char) waitKey(10); if(c == 's') { g_run = 1; cout << "single step, run = " << g_run << endl; } if(c == 'r') { g_run = -1; cout << "run mode, run = " << g_run << endl; } if(c == 27) break; } return 0; } /// page 31 int main() { //show_vedio(); //show_image(); show_TrackbarSlider(); return 0; }
#include <opencv2/opencv.hpp> 包含了opencv中所有的头文件
cv::Mat 是存放图片的数据类型
cv::imread() 用来读取图片
cv::nameWindow() 用来定义窗口
cv::imshow() 用来显示图片
cv::waitkey() 用来定义退出时间
cv::destroyWindow() 用来销毁窗口
cv::VideoCapture 用来捕捉视频对象,使视频对象实例化
cap.open() 打开一个视频文件
cv::createTrackbar() 用来定义一个进度条