• vscode + cmake + opencv example


    nice try on macos

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.20)
    #添加OPENCV库
    #指定OpenCV版本,代码如下
    #find_package(OpenCV 3.3 REQUIRED)
    #如果不需要指定OpenCV版本,代码如下
    find_package(OpenCV REQUIRED)
     
    #添加OpenCV头文件
    include_directories(${OpenCV_INCLUDE_DIRS})
     
    #显示OpenCV_INCLUDE_DIRS的值
    message(${OpenCV_INCLUDE_DIRS})
     
    # 添加一个可执行程序
    # 语法:add_executable( 程序名 源代码文件 )
    add_executable( main main.cpp )
     
    # 将库文件链接到可执行程序上
    target_link_libraries( main  ${OpenCV_LIBS})
    file(GLOB allCopyFiles  "./*.jpg")
    file(COPY ${allCopyFiles} DESTINATION .) #copy any .jpg file to  dir
    
    # 或者
    # file(GLOB allCopyFiles "${NSF_HOME}/lib/*")
    # execute_process(COMMAND cp ${allCopyFiles} ${LIB})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    main.cpp

        #include 
        #include "opencv2/imgproc.hpp"
        #include "opencv2/ximgproc.hpp"
        #include "opencv2/imgcodecs.hpp"
        #include "opencv2/highgui.hpp"
        using namespace std;
        using namespace cv;
        using namespace cv::ximgproc;
        int main(int argc, char **argv)
        {
            string in;
            // 修改图片
            CommandLineParser parser(argc, argv, "{@input|me.jpg|input image}{help h||show help message}");
            if (parser.has("help"))
            {
                parser.printMessage();
                return 0;
            }
            in = samples::findFile(parser.get<string>("@input"));
            Mat image = imread(in, IMREAD_GRAYSCALE);
            if (image.empty())
            {
                return -1;
            }
            // Create FLD detector
            // Param               Default value   Description
            // length_threshold    10            - Segments shorter than this will be discarded
            // distance_threshold  1.41421356    - A point placed from a hypothesis line
            //                                     segment farther than this will be
            //                                     regarded as an outlier
            // canny_th1           50            - First threshold for
            //                                     hysteresis procedure in Canny()
            // canny_th2           50            - Second threshold for
            //                                     hysteresis procedure in Canny()
            // canny_aperture_size 3            - Aperturesize for the sobel operator in Canny().
            //                                     If zero, Canny() is not applied and the input
            //                                     image is taken as an edge image.
            // do_merge            false         - If true, incremental merging of segments
            //                                     will be performed
            int length_threshold = 10;
            float distance_threshold = 1.41421356f;
            double canny_th1 = 50.0;
            double canny_th2 = 50.0;
            int canny_aperture_size = 3;
            bool do_merge = false;
            Ptr<FastLineDetector> fld = createFastLineDetector(length_threshold,
                                                            distance_threshold, canny_th1, canny_th2, canny_aperture_size,
                                                            do_merge);
            vector<Vec4f> lines;
            // Because of some CPU's power strategy, it seems that the first running of
            // an algorithm takes much longer. So here we run the algorithm 10 times
            // to see the algorithm's processing time with sufficiently warmed-up
            // CPU performance.
            for (int run_count = 0; run_count < 5; run_count++)
            {
                double freq = getTickFrequency();
                lines.clear();
                int64 start = getTickCount();
                // Detect the lines with FLD
                fld->detect(image, lines);
                double duration_ms = double(getTickCount() - start) * 1000 / freq;
                cout << "Elapsed time for FLD " << duration_ms << " ms." << endl;
            }
            // Show found lines with FLD
            Mat line_image_fld(image);
            fld->drawSegments(line_image_fld, lines);
            imshow("FLD result", line_image_fld);
            waitKey(1);
            Ptr<EdgeDrawing> ed = createEdgeDrawing();
            ed->params.EdgeDetectionOperator = EdgeDrawing::SOBEL;
            ed->params.GradientThresholdValue = 38;
            ed->params.AnchorThresholdValue = 8;
            vector<Vec6d> ellipses;
            for (int run_count = 0; run_count < 5; run_count++)
            {
                double freq = getTickFrequency();
                lines.clear();
                int64 start = getTickCount();
                // Detect edges
                // you should call this before detectLines() and detectEllipses()
                ed->detectEdges(image);
                // Detect lines
                ed->detectLines(lines);
                double duration_ms = double(getTickCount() - start) * 1000 / freq;
                cout << "Elapsed time for EdgeDrawing detectLines " << duration_ms << " ms." << endl;
                start = getTickCount();
                // Detect circles and ellipses
                ed->detectEllipses(ellipses);
                duration_ms = double(getTickCount() - start) * 1000 / freq;
                cout << "Elapsed time for EdgeDrawing detectEllipses " << duration_ms << " ms." << endl;
            }
            Mat edge_image_ed = Mat::zeros(image.size(), CV_8UC3);
            vector<vector<Point>> segments = ed->getSegments();
            for (size_t i = 0; i < segments.size(); i++)
            {
                const Point *pts = &segments[i][0];
                int n = (int)segments[i].size();
                polylines(edge_image_ed, &pts, &n, 1, false, Scalar((rand() & 255), (rand() & 255), (rand() & 255)), 1);
            }
            imshow("EdgeDrawing detected edges", edge_image_ed);
            Mat line_image_ed(image);
            fld->drawSegments(line_image_ed, lines);
            // Draw circles and ellipses
            for (size_t i = 0; i < ellipses.size(); i++)
            {
                Point center((int)ellipses[i][0], (int)ellipses[i][1]);
                Size axes((int)ellipses[i][2] + (int)ellipses[i][3], (int)ellipses[i][2] + (int)ellipses[i][4]);
                double angle(ellipses[i][5]);
                Scalar color = ellipses[i][2] == 0 ? Scalar(255, 255, 0) : Scalar(0, 255, 0);
                ellipse(line_image_ed, center, axes, angle, 0, 360, color, 2, LINE_AA);
            }
            imshow("EdgeDrawing result", line_image_ed);
            waitKey();
            return 0;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115

    提供一张名叫me.jpg的文件到build文件夹下

    REF LINKS:

    • https://docs.opencv.org/3.4/d1/d9e/fld_lines_8cpp-example.html
    • https://www.cnblogs.com/JoyPoint/p/11629521.html
  • 相关阅读:
    IB DP 语言怎么选?
    【Linux基础】工作中常用的linux命令,经常会被面试官问到
    【经典总线协议】IIC
    CTF本地靶场搭建——静态flag题型的创建
    Linux下编译libevent源码
    JUC并发编程与源码分析笔记04-说说Java“锁”事
    只出现一次的数字Ⅱ
    Java EnumMap remove()方法具有什么功能呢?
    MySQL数据库表的增删查改
    MySQL 使用 pt-archiver 删除数据
  • 原文地址:https://blog.csdn.net/m0_69086552/article/details/134276604