OPENCV例子opencv-4.5.5\samples\gpu\generalized_hough.cpp的代码分析

2023-12-29 16:50

本文主要是介绍OPENCV例子opencv-4.5.5\samples\gpu\generalized_hough.cpp的代码分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

该程序演示了使用广义霍夫变换进行任意对象查找,仅检测位置,无需平移和旋转。

相关类的继承关系如下图:

示例的调用关系如下图:

 

main的调用关系如下图:

 

main的流程图如下图:

 

main的UML逻辑图如下图:

 

示例源代码:

#include <vector>

#include <iostream>

#include <string>

#include "opencv2/core.hpp"

#include "opencv2/core/utility.hpp"

#include "opencv2/imgproc.hpp"

#include "opencv2/cudaimgproc.hpp"

#include "opencv2/highgui.hpp"

using namespace std;

using namespace cv;

static Mat loadImage(const string& name)

{

    Mat image = imread(name, IMREAD_GRAYSCALE);

    if (image.empty())

    {

        cerr << "Can't load image - " << name << endl;//无法载入图片

        exit(-1);

    }

    return image;

}

int main(int argc, const char* argv[])

{

    CommandLineParser cmd(argc, argv,

        "{ image i        | ../data/pic1.png  | input image }"           //图片i

        "{ template t     | templ.png | template image }"                //模板        

        "{ full           |           | estimate scale and rotation }"        //估计尺度和旋转        

        "{ gpu            |           | use gpu version }"        //使用GPU

        "{ minDist        | 100       | minimum distance between the centers of the detected objects }"//最小的距离(被检测物体的中心之间)

        "{ levels         | 360       | R-Table levels }"//RTable的层级

        "{ votesThreshold | 30        | the accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected }"//检测阶段模板中心的累加器阈值。它越小,可能检测到的错误位置越多

        "{ angleThresh    | 10000     | angle votes threshold }"//角度门槛

        "{ scaleThresh    | 1000      | scale votes threshold }"//尺度门槛

        "{ posThresh      | 100       | position votes threshold }"//位置门槛

        "{ dp             | 2         | inverse ratio of the accumulator resolution to the image resolution }"//累加器分辨率与图像分辨率的反比

        "{ minScale       | 0.5       | minimal scale to detect }"//检测的最小尺度

        "{ maxScale       | 2         | maximal scale to detect }"//检测的最大尺度

        "{ scaleStep      | 0.05      | scale step }"//尺度步长

        "{ minAngle       | 0         | minimal rotation angle to detect in degrees }"//以度为单位检测的最小旋转角度

        "{ maxAngle       | 360       | maximal rotation angle to detect in degrees }"//以度为单位检测的最大旋转角度

        "{ angleStep      | 1         | angle step in degrees }"//角度步长

        "{ maxBufSize     | 1000      | maximal size of inner buffers }"//内部缓冲区的最大大小

        "{ help h ?       |           | print help message }"//打印帮助信息

    );

    cmd.about("This program demonstrates arbitrary object finding with the Generalized Hough transform.");

    if (cmd.has("help"))

    {

        cmd.printMessage();

        return 0;

    }

    const string templName = cmd.get<string>("template");

    const string imageName = cmd.get<string>("image");

    const bool full = cmd.has("full");

    const bool useGpu = cmd.has("gpu");

    const double minDist = cmd.get<double>("minDist");

    const int levels = cmd.get<int>("levels");

    const int votesThreshold = cmd.get<int>("votesThreshold");

    const int angleThresh = cmd.get<int>("angleThresh");

    const int scaleThresh = cmd.get<int>("scaleThresh");

    const int posThresh = cmd.get<int>("posThresh");

    const double dp = cmd.get<double>("dp");

    const double minScale = cmd.get<double>("minScale");

    const double maxScale = cmd.get<double>("maxScale");

    const double scaleStep = cmd.get<double>("scaleStep");

    const double minAngle = cmd.get<double>("minAngle");

    const double maxAngle = cmd.get<double>("maxAngle");

    const double angleStep = cmd.get<double>("angleStep");

    const int maxBufSize = cmd.get<int>("maxBufSize");

    if (!cmd.check())

    {

        cmd.printErrors();

        return -1;

    }

    Mat templ = loadImage(templName);

    Mat image = loadImage(imageName);

    Ptr<GeneralizedHough> alg;

    if (!full)

    {

        Ptr<GeneralizedHoughBallard> ballard = useGpu ? cuda::createGeneralizedHoughBallard() : createGeneralizedHoughBallard();

        ballard->setMinDist(minDist);

        ballard->setLevels(levels);

        ballard->setDp(dp);

        ballard->setMaxBufferSize(maxBufSize);

        ballard->setVotesThreshold(votesThreshold);

        alg = ballard;

    }

    else

    {

        Ptr<GeneralizedHoughGuil> guil = useGpu ? cuda::createGeneralizedHoughGuil() : createGeneralizedHoughGuil();

        guil->setMinDist(minDist);

        guil->setLevels(levels);

        guil->setDp(dp);

        guil->setMaxBufferSize(maxBufSize);

        guil->setMinAngle(minAngle);

        guil->setMaxAngle(maxAngle);

        guil->setAngleStep(angleStep);

        guil->setAngleThresh(angleThresh);

        guil->setMinScale(minScale);

        guil->setMaxScale(maxScale);

        guil->setScaleStep(scaleStep);

        guil->setScaleThresh(scaleThresh);

        guil->setPosThresh(posThresh);

        alg = guil;

    }

    vector<Vec4f> position;

    TickMeter tm;

    if (useGpu)

    {

        cuda::GpuMat d_templ(templ);

        cuda::GpuMat d_image(image);

        cuda::GpuMat d_position;

        alg->setTemplate(d_templ);

        tm.start();

        alg->detect(d_image, d_position);

        d_position.download(position);

        tm.stop();

    }

    else

    {

        alg->setTemplate(templ);

        tm.start();

        alg->detect(image, position);

        tm.stop();

    }

    cout << "Found : " << position.size() << " objects" << endl;

    cout << "Detection time : " << tm.getTimeMilli() << " ms" << endl;

    Mat out;

    cv::cvtColor(image, out, COLOR_GRAY2BGR);

    for (size_t i = 0; i < position.size(); ++i)

    {

        Point2f pos(position[i][0], position[i][1]);

        float scale = position[i][2];

        float angle = position[i][3];

        RotatedRect rect;

        rect.center = pos;

        rect.size = Size2f(templ.cols * scale, templ.rows * scale);

        rect.angle = angle;

        Point2f pts[4];

        rect.points(pts);

        line(out, pts[0], pts[1], Scalar(0, 0, 255), 3);

        line(out, pts[1], pts[2], Scalar(0, 0, 255), 3);

        line(out, pts[2], pts[3], Scalar(0, 0, 255), 3);

        line(out, pts[3], pts[0], Scalar(0, 0, 255), 3);

    }

    imshow("out", out);

    waitKey();

    return 0;

}

这篇关于OPENCV例子opencv-4.5.5\samples\gpu\generalized_hough.cpp的代码分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/550065

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061