IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> VSCode配置之Opencv4x终极奥义 -> 正文阅读

[开发工具]VSCode配置之Opencv4x终极奥义

苦于windows下编译opencv的效率和对于大型软件如Visual Studio 2017、Visual Studio S2019等的不习惯,希望VScode也能够快速、高效编译第三方库,如opencv等,花了大概两天的时间,分析了主流的方法,最终适配出了一套极简方案:opencv4x终极奥义

  • 支持CMakeLists.txt编写自己的opencv项目
  • 支持Release和Debug两种模式运行,其中Debug速度较Release慢;
  • 支持多级文件访问和子目录CMakeLists.txt编译
  • 无需多余cmake、vscode配置,all in CMakeLists.txt
  • 简言之,一款windows下的cmake极简配置

工具准备:
opencv4.5.4下载:opencv-4.5.4-vc14_vc15.exe
cmake(仅仅下载安装,无需GUI)
Visual Studio Pro 2017(仅下载,便于支持加速编译)
添加环境变量:path\to\opencv\build\x64\vc15\bin
添加path\to\opencv\build\x64\vc15\lib下:opencv_world454.dll和opencv_world454d.dll到C:/Windows/System32中

方法:
step1 :VScode中安装插件CMake和CMake Tools,其强大之处在于保存即编译;
使用方法:

Ctrl + shift + P 输入CMake: Quick Start 生成CMakeLists.txt模板

最佳配置选项
step2: 编写自己的CMakeLists.txt内容。这里以opencv自带示例asift.cpp为例,该方法匹配两张图像(有旋转)的特征点。

# cmake needs this line
cmake_minimum_required(VERSION 3.1)


SET(CMAKE_BUILD_TYPE "Release")
# Define project name
project(opencv_example_project)

include_directories("D:/Downloads/opencv/build/include" "D:/Downloads/opencv/build/include/opencv2")

#指定dll的lib所在路径
link_directories("D:/Downloads/opencv/build/x64/vc15/lib")

# 将源代码添加到此项目的可执行文件。
add_executable (asift asift.cpp)

#指定链接库的名字,即该dll
#opencv_410d.lib在\path\to\opencv\build\x64\vc15\lib目录下
target_link_libraries(asift opencv_world454)

【注】:最后选择target_link_libraries时,根据CMake编译方法不同而不同,Release版本使用opencv_world454,Debug版本使用opencv_world454
step3:编译运行
截图从左到右一次进过cmake,build,最后run。
在这里插入图片描述
step4: 代码与效果展示

// main.cpp
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <iostream>
#include <iomanip>

using namespace std;
using namespace cv;

static void help(char** argv)
{
    cout
    << "This is a sample usage of AffineFeature detector/extractor.\n"
    << "And this is a C++ version of samples/python/asift.py\n"
    << "Usage: " << argv[0] << "\n"
    << "     [ --feature=<sift|orb|brisk> ]         # Feature to use.\n"
    << "     [ --flann ]                            # use Flann-based matcher instead of bruteforce.\n"
    << "     [ --maxlines=<number(50 as default)> ] # The maximum number of lines in visualizing the matching result.\n"
    << "     [ --image1=<image1(aero1.jpg as default)> ]\n"
    << "     [ --image2=<image2(aero3.jpg as default)> ] # Path to images to compare."
    << endl;
}

static double timer()
{
    return getTickCount() / getTickFrequency();
}

int main(int argc, char** argv)
{
    vector<String> fileName;
    cv::CommandLineParser parser(argc, argv,
        "{help h ||}"
        "{feature|brisk|}"
        "{flann||}"
        "{maxlines|50|}"
        "{image1|aero1.jpg|}{image2|aero3.jpg|}");
    if (parser.has("help"))
    {
        help(argv);
        return 0;
    }
    string feature = parser.get<string>("feature");
    bool useFlann = parser.has("flann");
    int maxlines = parser.get<int>("maxlines");
    fileName.push_back(samples::findFile(parser.get<string>("image1")));
    fileName.push_back(samples::findFile(parser.get<string>("image2")));
    if (!parser.check())
    {
        parser.printErrors();
        cout << "See --help (or missing '=' between argument name and value?)" << endl;
        return 1;
    }

    Mat img1 = imread(fileName[0], IMREAD_GRAYSCALE);
    Mat img2 = imread(fileName[1], IMREAD_GRAYSCALE);
    if (img1.empty())
    {
        cerr << "Image " << fileName[0] << " is empty or cannot be found" << endl;
        return 1;
    }
    if (img2.empty())
    {
        cerr << "Image " << fileName[1] << " is empty or cannot be found" << endl;
        return 1;
    }

    Ptr<Feature2D> backend;
    Ptr<DescriptorMatcher> matcher;

    if (feature == "sift")
    {
        backend = SIFT::create();
        if (useFlann)
            matcher = DescriptorMatcher::create("FlannBased");
        else
            matcher = DescriptorMatcher::create("BruteForce");
    }
    else if (feature == "orb")
    {
        backend = ORB::create();
        if (useFlann)
            matcher = makePtr<FlannBasedMatcher>(makePtr<flann::LshIndexParams>(6, 12, 1));
        else
            matcher = DescriptorMatcher::create("BruteForce-Hamming");
    }
    else if (feature == "brisk")
    {
        backend = BRISK::create();
        if (useFlann)
            matcher = makePtr<FlannBasedMatcher>(makePtr<flann::LshIndexParams>(6, 12, 1));
        else
            matcher = DescriptorMatcher::create("BruteForce-Hamming");
    }
    else
    {
        cerr << feature << " is not supported. See --help" << endl;
        return 1;
    }

    cout << "extracting with " << feature << "..." << endl;
    Ptr<AffineFeature> ext = AffineFeature::create(backend);
    vector<KeyPoint> kp1, kp2;
    Mat desc1, desc2;

    ext->detectAndCompute(img1, Mat(), kp1, desc1);
    ext->detectAndCompute(img2, Mat(), kp2, desc2);
    cout << "img1 - " << kp1.size() << " features, "
         << "img2 - " << kp2.size() << " features"
         << endl;

    cout << "matching with " << (useFlann ? "flann" : "bruteforce") << "..." << endl;
    double start = timer();
    // match and draw
    vector< vector<DMatch> > rawMatches;
    vector<Point2f> p1, p2;
    vector<float> distances;
    matcher->knnMatch(desc1, desc2, rawMatches, 2);
    // filter_matches
    for (size_t i = 0; i < rawMatches.size(); i++)
    {
        const vector<DMatch>& m = rawMatches[i];
        if (m.size() == 2 && m[0].distance < m[1].distance * 0.75)
        {
            p1.push_back(kp1[m[0].queryIdx].pt);
            p2.push_back(kp2[m[0].trainIdx].pt);
            distances.push_back(m[0].distance);
        }
    }
    vector<uchar> status;
    vector< pair<Point2f, Point2f> > pointPairs;
    Mat H = findHomography(p1, p2, status, RANSAC);
    int inliers = 0;
    for (size_t i = 0; i < status.size(); i++)
    {
        if (status[i])
        {
            pointPairs.push_back(make_pair(p1[i], p2[i]));
            distances[inliers] = distances[i];
            // CV_Assert(inliers <= (int)i);
            inliers++;
        }
    }
    distances.resize(inliers);

    cout << "execution time: " << fixed << setprecision(2) << (timer()-start)*1000 << " ms" << endl;
    cout << inliers << " / " << status.size() << " inliers/matched" << endl;

    cout << "visualizing..." << endl;
    vector<int> indices(inliers);
    cv::sortIdx(distances, indices, SORT_EVERY_ROW+SORT_ASCENDING);

    // explore_match
    int h1 = img1.size().height;
    int w1 = img1.size().width;
    int h2 = img2.size().height;
    int w2 = img2.size().width;
    Mat vis = Mat::zeros(max(h1, h2), w1+w2, CV_8U);
    img1.copyTo(Mat(vis, Rect(0, 0, w1, h1)));
    img2.copyTo(Mat(vis, Rect(w1, 0, w2, h2)));
    cvtColor(vis, vis, COLOR_GRAY2BGR);

    vector<Point2f> corners(4);
    corners[0] = Point2f(0, 0);
    corners[1] = Point2f((float)w1, 0);
    corners[2] = Point2f((float)w1, (float)h1);
    corners[3] = Point2f(0, (float)h1);
    vector<Point2i> icorners;
    perspectiveTransform(corners, corners, H);
    transform(corners, corners, Matx23f(1,0,(float)w1,0,1,0));
    Mat(corners).convertTo(icorners, CV_32S);
    polylines(vis, icorners, true, Scalar(255,255,255));

    for (int i = 0; i < min(inliers, maxlines); i++)
    {
        int idx = indices[i];
        const Point2f& pi1 = pointPairs[idx].first;
        const Point2f& pi2 = pointPairs[idx].second;
        circle(vis, pi1, 2, Scalar(0,255,0), -1);
        circle(vis, pi2 + Point2f((float)w1,0), 2, Scalar(0,255,0), -1);
        line(vis, pi1, pi2 + Point2f((float)w1,0), Scalar(0,255,0));
    }
    if (inliers > maxlines)
        cout << "only " << maxlines << " inliers are visualized" << endl;
    imshow("affine find_obj", vis);

    // Mat vis2 = Mat::zeros(max(h1, h2), w1+w2, CV_8U);
    // Mat warp1;
    // warpPerspective(img1, warp1, H, Size(w1, h1));
    // warp1.copyTo(Mat(vis2, Rect(0, 0, w1, h1)));
    // img2.copyTo(Mat(vis2, Rect(w1, 0, w2, h2)));
    // imshow("warped", vis2);

    waitKey();
    cout << "done" << endl;
    return 0;
}

效果图

对比:

  1. 在Vistual Studio2019中设置属性,手动添加源文件等,一次设置多次可用,唯一的缺点是软件过大,修改比较麻烦
  2. 在VScode中改变C++的参数命令,自动添加 -I -L等库文件包,需要改变task,lunch等yaml文件,使用的是mingw64编译,速度较慢(实测)
  3. 使用Cmake编译opencv,将CMakelists.txt的kit selecti设置为mingw64,可行但速度慢,相关配置如下:(可能出现无法定位程序输入点问题,将mingw64下bin/libstdc+±6.dll放置在C:/Windows/System32中)
# CMakeList.txt: CMakeProject1 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.8)
set(CMAKE_BUILD_TYPE "Release")
project ("imgShow")
#指定要引用的dll的头文件所在路径,即为文件夹opencv2的路径,
#dll的头文件地址前半部分("D:\Tool\")需根据opencv安装的位置确定。
include_directories("D:/Downloads/opencv/build/x64/mingw/install/include" "D:/Downloads/opencv/build/x64/mingw/install/include/opencv2")
#指定dll的lib所在路径
link_directories("D:/Downloads/opencv/build/x64/mingw/lib")

# 将源代码添加到此项目的可执行文件。
add_executable (asift asift.cpp)

#指定链接库的名字,即该dll
 target_link_libraries(asift 
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_calib3d454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_core454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_dnn454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_features2d454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_flann454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_gapi454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_highgui454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_imgcodecs454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_imgproc454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_ml454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_objdetect454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_photo454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_stitching454.dll" "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_video454.dll"
 "D:/Downloads/opencv/build/x64/mingw/bin/libopencv_videoio454.dll" )
# TODO: 如有需要,请添加测试并安装目标。
  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2021-12-15 18:30:19  更:2021-12-15 18:31:40 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/24 21:21:17-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计