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

相关文章

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.