从头开始学opencv,长期更新记录。参考书籍Learning OpenCv3
visual studio+opencv配置
下载opencv网址https://opencv.org/releases/
下载visual studio2022 网址:https://visualstudio.microsoft.com/zh-hans/vs/
新建一个控制台应用:
右击属性
分别在VC++目录添加:D:\opencv\build\include\opencv2
库目录添加:D:\opencv\build\x64\vc15\lib
输入附加依赖项添加:opencv_world460d.lib
注意d表示vs debug模式下
如何仍出现报错,把opencv下opencv_world460d.lib,opencv_world460.lib
复制到C盘下C:\Windows\SysWOW64和C:\Windows\System32下
显示照片的程序
#include <iostream>
#include <opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat img = imread(argv[1], -1);//读取图片文件
std::cout << "Hello World!\n";
if (img.empty())
return -1;
namedWindow("Example", WINDOW_AUTOSIZE);
imshow("Example", img);
waitKey(0);
destroyWindow("Example");
return 0;
std::cout << "Hello World!\n";
}
Mat img = imread(argv[1], -1)//
argv[1]表示图片文件路径,-1标志位,可点开enum参看枚举
直接生成解决方案,
win+R,CMD
这个就是方便调用任意位置照片。
也可以手动代码调用:
string img1 = “D:/mycode/opencv_001/x64/Debug/001.jpg”;
Mat img = imread(img1, -1);
这个debug就可以显示照片
视频播放器
#include <iostream>
#include <opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
cv::namedWindow("Example", WINDOW_AUTOSIZE);
cv::VideoCapture cap;
cap.open(argv[1], 0);
cv::Mat frame;
while(1) {
cap >> frame;
if (frame.empty())break;
cv::imshow("Example", frame);
if (cv::waitKey(33) >= 0)break;
}
destroyWindow("Example");
return 0;
}
以上代码能播放单纯视频文件,按任意键退出
#include <iostream>
#include <opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<fstream>
using namespace std;
using namespace cv;
int g_slide_position = 0;
int g_run = 1, g_dontset = 0;
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("Example", WINDOW_AUTOSIZE);
//cv::VideoCapture cap;
g_cap.open(argv[1], 0);
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 << "frame of dimensions " << tmpw << "," << tmph << endl;
cv::createTrackbar("Position", "Example", &g_slide_position, frames, onTrackbarSlide);
cv::Mat frame;
while(true) {
if (g_run != 0) {
g_cap >> frame;
//g_run = -1;
if (frame.empty())break;
int current_pos = (int)g_cap.get(cv::CAP_PROP_POS_FRAMES);
g_dontset = 1;
cout << "debug1" << endl;
cv::setTrackbarPos("Position", "Example", current_pos);
cout << "debug2" << endl;
cv::imshow("Example", frame);
g_run -= 1;
}
char c = (char)waitKey(33);
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||c=='q') {
break;
}
}
cv::destroyWindow("Example");
return 0;
}
按esc或q退出,s单帧观看,r连续查看
后面就是GaussianBlur高斯模糊,pyrDown降采样(每操作一次,图像长宽降低一半,面积降低1/4),Canny边缘检测(需将图像灰度后执行此操作),代码如下
添加函数
void gBlur(const cv::Mat& image) {
cv::namedWindow("in", WINDOW_AUTOSIZE);
cv::namedWindow("out", WINDOW_AUTOSIZE);
cv::imshow("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("out", out);
}
cv::Mat frame;
cv::Mat frame1, frame2;
cv::Mat img_gry, img_cny;
while(true) {
if (g_run != 0) {
g_cap >> frame;
//g_run = -1;
if (frame.empty())break;
int current_pos = (int)g_cap.get(cv::CAP_PROP_POS_FRAMES);
g_dontset = 1;
cv::setTrackbarPos("Position", "Example", current_pos);
cv::imshow("Example", frame);
cv:cvtColor(frame, img_gry, cv::COLOR_BGR2GRAY);
cv::imshow("gray", img_gry);
cv::Canny(img_gry, img_cny, 10, 100, 3, true);
cv::imshow("Canny", img_cny);
cv::pyrDown(frame, frame1);
cv::pyrDown(frame1, frame1);
cv::imshow("Example1", frame1);
gBlur(frame);
g_run -= 1;
}
效果如下:
注意cv::destroyWindow(“xxx”)各个窗口。