1. Preparation
1. Download the opencv4.4.0 exe file from the official website and extract it to the specified folder
2. Add the conversion variable F:\file\opencv\opencv\build\x64\vc15\bin to the path. I am using vs2019, so vc14 is not added.
3. Copy opencv_world440.dll and opencv_world440d.dll to the folder C:\Windows\SysWOW64.
Note: 64-bit system users need to pay attention. The 32-bit DLL files are placed in [C:\Windows\SysWOW64], and the 64-bit ones are placed in [C:\Windows\SysWOW64]. The DLL file is placed in "[C:\Windows\System32].
4. Open vs2019 and create a new command line program. After the creation is successful, open the property manager. First select the specified configuration manager.
Add to the include directory:
F:\file\opencv\opencv\build\include
Add to the library directory:
F:\file\opencv\opencv\build\x64\vc15\lib
Select input -> Additional dependencies Add opencv_world440d.lib ( Make changes according to the actual version downloaded. It can be viewed in opencv\build\x64\vc15\lib)
5. Test it and read a photo
#include <opencv2/opencv.hpp>
int main(int argc, char** argv) {
cv::Mat img = cv::imread("F:\\file\\abcdefg.jpg");
if (img.empty()) {
printf("资源未找到");
return -1;
}
cv::namedWindow("Example1", cv::WINDOW_AUTOSIZE);
cv::imshow("Example1", img);
cv::waitKey(0);
cv::destroyWindow("Example1");
return 0;
}
2. Start
1. Read a video
#include <opencv2/opencv.hpp>
int main(int argc, char** argv) {
cv::namedWindow("Example2", cv::WINDOW_AUTOSIZE);
cv::VideoCapture cap;
cap.open("F:\\file\\VID_20200131_115507.mp4");
cv::Mat Frame;
for (;;) {
cap >> Frame;
if (Frame.empty()) break;
cv::imshow("Example2", Frame);
if (cv::waitKey(33) >= 0) break; //等待输入,时长为33毫秒;
//30FPS播放,准确数字为33.3ms...,60FPS则设置等待(1000/60)ms
}
return 0;
}
2. Video player with progress bar
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
using namespace std;
int g_slider_position = 0;
int g_run = 1, g_dontset = 0;//start out int single step mode
cv::VideoCapture g_cap;
void onTrackbarSlide(int pos, void*) {
g_cap.set(cv::CAP_PROP_POS_FRAMES, pos);
if (!g_dontset)
g_run = 1;
g_dontset = 0;
}
int main(int argc, char** argv) {
cv::namedWindow("Example2_4", cv::WINDOW_AUTOSIZE); //创建一个窗口
g_cap.open("F:\\file\\VID_20200131_115507.mp4"); //将指定的视频读取到g_cap中
int frames = (int)g_cap.get(cv::CAP_PROP_FRAME_COUNT); //获取视频的总帧数
int tmpw = (int)g_cap.get(cv::CAP_PROP_FRAME_WIDTH); //获取视频的宽度
int tmph = (int)g_cap.get(cv::CAP_PROP_FRAME_HEIGHT); //获取视频的高度
cout << "Video has" << frames << "frames of dimensions("
<< tmpw << "," << tmph << ")." << endl;
cv::createTrackbar("Position", "Example2_4", &g_slider_position, frames,
onTrackbarSlide);//创建进度条
cv::Mat frame;
for (;;) {
if (g_run != 0) {
g_cap >> frame;
if (frame.empty()) break;
int current_pos = (int)g_cap.get(cv::CAP_PROP_POS_FRAMES); //获取当前的帧数位置
g_dontset = 1;
cv::setTrackbarPos("Position", "Example2_4", current_pos);
cv::imshow("Example2_4", frame);
g_run -= 1;
}
char c = (char)cv::waitKey(10);
if(c=='s')//single step
{
g_run = 1; cout << "Single step,run=" << g_run << endl;
}
if (c == 'r')//run mode
{
g_run = -1; cout << "Run mode,run=" << g_run << endl;
}
if (c == 27)
break;
}
return 0;
}
3. Gaussian kernel spatial filtering
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
using namespace std;
//简单处理函数
void example2_5(const cv::Mat& image) {
//创建连个窗口用来显示出入和输出的图像
cv::namedWindow("Example2_5-in", cv::WINDOW_NORMAL);
cv::namedWindow("Example2_5-out", cv::WINDOW_NORMAL);
cv::imshow("Example2_5-in", image);
cv::Mat out; //输出的图像
//高斯核模糊滤波
cv::GaussianBlur(image, out, cv::Size(5, 5), 3, 3);
cv::GaussianBlur(out, out, cv::Size(5, 5), 3, 3);
//将处理后的图像打印在输出窗口
cv::imshow("Example2_5-out", out);
cv::waitKey(0);
}
int main(int argc, char** argv) {
cv::Mat image = cv::imread("F:\\file\\abcdefg.jpg");
example2_5(image);
return 0;
}
When creating a window, AUTOSIZE will automatically adjust the window to the size of the wrapped image, but there is a maximum value. When the image exceeds the maximum value, the image will not be fully displayed. When using the NORMAL parameter, a fixed-size window will be created, and the image will be Scale to fit window size