OpenCV中更稳更快的边缘检测方法,快速查找线、圆、椭圆--EdgeDrawing-C++代码

本文主要是介绍OpenCV中更稳更快的边缘检测方法,快速查找线、圆、椭圆--EdgeDrawing-C++代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

计算机视觉之家看到快速圆检测Edge Drawing,其效果比霍夫要好,速度更快(具体效果可以参考视觉之家的文章),上面C++代码不全,那么好的检测效果国内资料竟然那么少,后在opencv的开发文档中找到了C++代码,在此分享学习交流。

实战 | OpenCV中更稳更快的找圆方法--EdgeDrawing使用演示(详细步骤 + 代码)_opencv 找圆_计算机视觉之家的博客-CSDN博客

OpenCV: EdgeDrawing

OpenCV: fld_lines.cpp

#include <iostream>#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|corridor.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 performedint 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 FLDfld->detect(image, lines);double duration_ms = double(getTickCount() - start) * 1000 / freq;cout << "Elapsed time for FLD " << duration_ms << " ms." << endl;}// Show found lines with FLDMat 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 linesed->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 ellipsesed->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 ellipsesfor (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;
}

这篇关于OpenCV中更稳更快的边缘检测方法,快速查找线、圆、椭圆--EdgeDrawing-C++代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Java中有什么工具可以进行代码反编译详解

《Java中有什么工具可以进行代码反编译详解》:本文主要介绍Java中有什么工具可以进行代码反编译的相关资,料,包括JD-GUI、CFR、Procyon、Fernflower、Javap、Byte... 目录1.JD-GUI2.CFR3.Procyon Decompiler4.Fernflower5.Jav

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问