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

相关文章

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

MySQL实现多源复制的示例代码

《MySQL实现多源复制的示例代码》MySQL的多源复制允许一个从服务器从多个主服务器复制数据,这在需要将多个数据源汇聚到一个数据库实例时非常有用,下面就来详细的介绍一下,感兴趣的可以了解一下... 目录一、多源复制原理二、多源复制配置步骤2.1 主服务器配置Master1配置Master2配置2.2 从服

python中update()函数的用法和一些例子

《python中update()函数的用法和一些例子》update()方法是字典对象的方法,用于将一个字典中的键值对更新到另一个字典中,:本文主要介绍python中update()函数的用法和一些... 目录前言用法注意事项示例示例 1: 使用另一个字典来更新示例 2: 使用可迭代对象来更新示例 3: 使用