如何实现视觉识别形状

1. 功能说明

通过摄像头识别圆形及矩形两种形状。

 2. 电子硬件

     本实验中采用了以下硬件:

主控板

Basra主控板(兼容Arduino Uno)

扩展板

Bigfish2.1

电池7.4V锂电池
通信2510通信转接板
WiFi路由器

其它

摄像头

配置OpenCV的Visual Studio 2015.net环境的计算机一台

3. 功能实现  

   工作原理:

                  ① 导入一张图片或者通过WiFi传递摄像信息给PC;

                  ② 在PC端使用OpenCV对图像转化为灰度图像;

                  ③ 检测圆形和矩形。

    检测圆形使用霍夫变换:

vector<Vec3f> circles;

HoughCircles(image, circles, HOUGH_GRADIENT, 2.0,

image.rows/8, // change this value to detect circles with different distances to each other

200, 85, 0, 0 // change the last two parameters

  // (min_radius & max_radius) to detect larger circles

);

    检测矩形:

//多边形检测,通过约束条件寻找矩形

static void findSquares(const Mat& image, vector<vector<Point> >& squares)

{

squares.clear();


Mat pyr, timg, gray0(image.size(), CV_8U), gray;


// down-scale and upscale the image to filter out the noise

pyrDown(image, pyr, Size(image.cols / 2, image.rows / 2));

pyrUp(pyr, timg, image.size());

vector<vector<Point> > contours;


// find squares in every color plane of the image

for (int c = 0; c < 3; c++)

{

int ch[] = { c, 0 };

mixChannels(&timg, 1, &gray0, 1, ch, 1);


// try several threshold levels

for (int l = 0; l < N; l++)

{

// hack: use Canny instead of zero threshold level.

// Canny helps to catch squares with gradient shading

if (l == 0)

{

// apply Canny. Take the upper threshold from slider

// and set the lower to 0 (which forces edges merging)

Canny(gray0, gray, 0, thresh, 5);

// dilate canny output to remove potential

// holes between edge segments

dilate(gray, gray, Mat(), Point(-1, -1));

}

else

{

// apply threshold if l!=0:

//     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0

gray = gray0 >= (l + 1) * 255 / N;

}


// find contours and store them all as a list

findContours(gray, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);


vector<Point> approx;


// test each contour

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

{

// approximate contour with accuracy proportional

// to the contour perimeter

approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);


// square contours should have 4 vertices after approximation

// relatively large area (to filter out noisy contours)

// and be convex.

// Note: absolute value of an area is used because

// area may be positive or negative - in accordance with the

// contour orientation

if (approx.size() == 4 &&

fabs(contourArea(Mat(approx))) > 1000 &&

isContourConvex(Mat(approx)))

{

double maxCosine = 0;


for (int j = 2; j < 5; j++)

{

// find the maximum cosine of the angle between joint edges

double cosine = fabs(angle(approx[j % 4], approx[j - 2], approx[j - 1]));

maxCosine = MAX(maxCosine, cosine);

}


// if cosines of all angles are small

// (all angles are ~90 degree) then write quandrange

// vertices to resultant sequence

if (maxCosine < 0.3)

squares.push_back(approx);

}

}

}

}

}

3.1硬件连接

    将摄像头与路由器连接,启动路由器,将PC连接到路由器的WIFI网络。

接线说明:

            ① 将2510通信转接板连接到扩展板的扩展坞上面;

            ② 用3根母对母杜邦线将2510通信转接板与WiFi路由器连接起来,GND-GND、RX-RX、TX-TX;

            ③ 找到1根USB线,一端连接到2510通信转接板接口上,另一端连接到WiFi路由器USB接口上;

            ④ 将摄像头线连接到WiFi路由器接口上。

3.2示例程序

    下面提供一个可以进行识别圆形和矩形的参考例程(ShapeDetect\ShapeDetect\MainWindow.xaml.cs):

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.Threading;


namespace ShapeDetect

{

    /// <summary>

    /// 形状识别

    /// </summary>

    public partial class MainWindow : Window

    {

        //定义检测模式

        int IMAGE_MODE = 1, VIDEO_MODE = 2;


        private AutoResetEvent exitEvent;

        private Thread m_thread;


        //导入动态链接库

        [DllImport("HoughCircles_DLL.dll")]

        //检测圆

        public static extern System.UIntPtr HoughCircles([MarshalAs(UnmanagedType.LPStr)]string address, int detect_mode);

        [DllImport("SquareDetect_DLL.dll")]

        //检测矩形

        public static extern void SquareDetector([MarshalAs(UnmanagedType.LPStr)]string address, int detect_mode);


        public MainWindow()

        {

            InitializeComponent();

        }


        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            GetIni();

            imgCheckBtn.IsChecked = true;

            circleCheckBtn.IsChecked = true;

        }


        //获取ini配置文件信息

        private void GetIni()

        {

            ini_RW.FileName = System.Windows.Forms.Application.StartupPath + "\\Config.ini";

            this.videoAddress.Text = ini_RW.ReadIni("VideoUrl", "videourl", "");

            this.ipAddress.Text = ini_RW.ReadIni("ControlUrl", "controlUrl", "");

            this.portBox.Text = ini_RW.ReadIni("ControlPort", "controlPort", "");

        }


        //修改配置

        private void setBtn_Click(object sender, RoutedEventArgs e)

        {

            ini_RW.WriteIni("VideoUrl", "videourl", this.videoAddress.Text);

            ini_RW.WriteIni("ControlUrl", "controlUrl", this.ipAddress.Text);

            ini_RW.WriteIni("ControlPort", "controlPort", this.portBox.Text);


            System.Windows.MessageBox.Show("配置成功!请重启程序以使配置生效。", "配置信息", MessageBoxButton.OK, MessageBoxImage.Information);

            //this.Close();

        }


        //计数清零

        private void BoxClean()

        {

            circleTextBox.Text = "0";

            recTextBox.Text = "0";

        }


        //打开图片地址

        private void imgBtn_Click(object sender, RoutedEventArgs e)

        {

            try

            {


                BoxClean();

                //WPF中,OpenFileDialog位于Microsoft.Win32名称空间

                Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();

                dialog.Filter = "All files (*.*)|*.*|jpg files (*.jpg)|*.jpg|png files(*.png)|*.png";


                if (dialog.ShowDialog() == true)

                {


                    string path = dialog.FileName;

                    imgAddressBox.Text = path;

                }

            }

            catch { };

        }


        //检测形状判断

        private void ShapeDetect(string address, int mode)

        {

            if (circleCheckBtn.IsChecked == true)

            {

                //System.Windows.MessageBox.Show("检测圆形");

                circleTextBox.Text = HoughCircles(address, mode).ToString();

            }

            else if (recCheckBtn.IsChecked == true)

            {

                //System.Windows.MessageBox.Show("检测矩形");

                SquareDetector(address, mode);

            }

        }


        //图片检测

        private void imgDetect()

        {


            if (imgAddressBox.Text == string.Empty)

            {

                System.Windows.MessageBox.Show(

                    "图片地址为空,请选择一张图片", "警告",

                    MessageBoxButton.OK,

                    MessageBoxImage.Information

                    );

                return;

            }

            else

            {

                ShapeDetect(imgAddressBox.Text, IMAGE_MODE);

            }

        }


        //视频检测

        private void videoDetect()

        {

            try

            {

                while (true)

                {

                    this.Dispatcher.Invoke(

                        new Action(

                            delegate

                            {

                                string ip = this.videoAddress.Text;

                                ShapeDetect(ip, VIDEO_MODE);

                            }

                            ));

                }


            }

            catch { };

        }


        //判断检测为图片还是视频,开启形状检测

        private void detectBtn_Click(object sender, RoutedEventArgs e)

        {

            BoxClean();

            if (imgCheckBtn.IsChecked == true)

            {

                imgDetect();

            }

            else if (videoCheckBtn.IsChecked == true)

            {

                try

                {

                    m_thread = new Thread(new ThreadStart(videoDetect));

                    m_thread.Start();

                }

                catch { };

            }

        }


        //按esc键退出视频检测,结束线程

        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)

        {

            if (e.Key == Key.Escape)

            {

                exitEvent.Set();

                m_thread.Join();

            }

        }


    }

}

      程序识别圆形及矩形两种形状,包括对图像以及视频中的物体的形状检测,可参考上面的演示视频进行操作。图片中物体的形状识别,文末资料下载中提供一张测试图片,然后选择图片按钮,选择要检测的圆形或者矩形,点击形状检测。

      视频中的形状识别,选择视频按钮,选择要检测的圆形或者矩形,点击形状检测,可以使用球体或者矩形状物体进行检测。

4. 资料内容

①识别形状-例程源代码

②测试图片.jpg

资料内容详见 如何实现视觉识别-识别形状

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/12782.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

MySQL having关键字详解、与where的区别

1、having关键字概览 1.1、作用 对查询的数据进行筛选 1.2、having关键字产生的原因 使用where对查询的数据进行筛选时&#xff0c;where子句中无法使用聚合函数&#xff0c;所以引出having关键字 1.3、having使用语法 having单独使用&#xff08;不与group by一起使用&a…

(SQL学习随笔3)SQL语法——SELECT语句

导航 基本认识FROM关键字LIMIT与OFFSETORDER BY WHERE条件查询单值比较多条件组合范围筛选空值匹配LIKE通配条件分组 运算符和函数数据变换 分组运算表连接内连接左(右)外连接全外连接 外键约束窗口函数UNION&#xff1a;表上下拼接子查询条件判断PostgreSQLMySQL 基本认识 SE…

两种方法实现杨辉三角(java实现)

&#x1f389;&#x1f389;&#x1f389;点进来你就是我的人了 博主主页&#xff1a;&#x1f648;&#x1f648;&#x1f648;戳一戳,欢迎大佬指点!人生格言&#xff1a;当你的才华撑不起你的野心的时候,你就应该静下心来学习! 欢迎志同道合的朋友一起加油喔&#x1f9be;&am…

Consul TTL健康检查方式

consul比较常用的健康检查方式为http健康检查方式&#xff0c;也还有使用TTL方式来进行健康检查的&#xff0c;下面从spring-cloud-consul-discovery这个SDK来着手分析。 构建ConsulAutoRegistration&#xff0c;这里的工作是组成服务注册的报文&#xff0c;有一个setCheck方法…

钉钉消息防撤回功能研究与实现-可查看历史消息[文件/图文/管理员/链接 撤回拦截]

研究背景 由于在某个大学进行上课的时候,遇到的某个老师,总是习惯发过的消息,到第二天的时候撤回,我们用聊天工具的其中一个原因,不就是因为可以随时去查看发过的消息吗&#xff0c;&#xff0c;而这位老师的操作,也让包括我在内的很多人感到痛不欲生。 想一想,当自己想要去看下…

常见的九种大数据分析模型

常见的9种大数据分析模型分别为&#xff1a; 事件分析、 属性分析、 渠道分析、 Session分析、 留存分析、 归因分析、 漏斗分析、 路径分析、 分布分析 1、【事件分析】 事件分析&#xff0c;是指用户在 APP、网站等应用上发生的行为&#xff0c;即何人&#xff0c;何时&…

【消费战略】解读100个食品品牌丨王小卤 4年10亿爆品破局

爆品破局 王小卤的聚焦发展! 王小卤创建于 2016 年&#xff0c;与饮料行业的独角兽元气森林同年。 相较于元气森林的快速增长&#xff0c;王小卤历经 三年坎坷之路&#xff0c;直至 2019 年才踏上高增长的赛道&#xff0c;实现四年十亿的增长。 “所有的消费品都值得重新 做…

网络安全-kali配置ssh服务+敏感文件泄+dirsearch脚本

网络安全-kali配置ssh服务敏感文件泄dirsearch脚本 seccure shell 就是加密的telnet 远程用的 service ssh start 开启ssh服务metstat -tpan |gerp 22 监听这个端口是否开启 可以看到本地的22端口这个文件是/etc/ssh/sshd_config 输入 set number 找到第57行 把这个前面的#注…

【记录】Truenas Scale|中危漏洞,需要SMB签名

部分内容参考&#xff1a;等保测试问题——需要SMB签名(SMB Signing not Required) 以及 ChatGPT。 Truenas常用SMB服务&#xff0c;但默认并不开启SMB签名。这样具有中间人攻击的风险。 一、漏洞详情 1.1 漏洞报告 漏洞提示如下&#xff1a; 1.2 漏洞介绍 SMB是一个协议名…

人工智能发展到GPT4经历了什么,从专家系统到机器学习再到深度学习,从大模型到现在的GPT4

大家好&#xff0c;我是微学AI&#xff0c;今天给大家讲一下人工智能的发展&#xff0c;从专家系统到机器学习再到深度学习&#xff0c;从大模型到现在的GPT4&#xff0c;讲这个的目的是让每个人都懂得人工智能&#xff0c;每个人都懂得人工智能的发展&#xff0c;未来人工智能…

4.13(LoadLibrary)

接着之前预习的知识&#xff0c;我观察的自己编译出来的bin LoadLibraryExA LoadLibraryExA函数进去&#xff0c;现时用RtInitAnsiString函数初始化了ANSI的计数字符串&#xff0c;底层是调用了LoadLibraryExW函数&#xff0c;在LoadLibrarExW函数里做了unicode的计数字符串的…

python入门(五) vscode配置Anaconda 环境,代码自动提示

文章目录 1.conda的下载地址:1.配置conda的环境变量安装conda配置path 2.vcode配置python插件3.配置conda1) Select Interpreter2) 选择conda环境 4.测试 vscode配置Anaconda 环境&#xff0c;代码自动提示. 本人工作中&#xff0c;用到了ai相关技术&#xff0c;但是java出身&a…

七项新发布,亚马逊云科技Amazon S3持续进化

17年前的3月14日&#xff0c;亚马逊云科技推出了一项“非常简单的”对象存储服务&#xff08;Amazon Simple Storage Service&#xff09;。该服务允许开发人员创建、列出和删除私有存储空间&#xff08;称为存储桶&#xff09;、上传和下载文件以及管理其访问权限。当时&#…

北京筑龙:采购供应链平台-构建能源企业数智供应链的必经之路

4月13至14日&#xff0c;“中国国际管道会议&#xff08;CIPC&#xff09;暨技术装备与成果展”高峰论坛在北京举行。来自国内外管道领域的院士、知名专家、学者齐聚一堂&#xff0c;共同探讨新时代背景下管道技术领域的发展方向。作为采购供应链数字化产品及服务提供商&#x…

每日学术速递4.13

CV - 计算机视觉 | ML - 机器学习 | RL - 强化学习 | NLP 自然语言处理 Subjects: cs.CV 1.Slide-Transformer: Hierarchical Vision Transformer with Local Self-Attention(CVPR 2023) 标题&#xff1a;Slide-Transformer&#xff1a;具有局部自注意力的分层视觉变换器 …

Camera | 8.让rk3568支持前后置摄像头

一、目标 本文主要目标是&#xff0c;支持前置摄像头0v5648、后置摄像头ov13850&#xff0c;以及移植过程遇到的一些小问题的解决。 1. 摄像头连接图 参考上图&#xff0c;摄像头详细信息如下&#xff1a; 2个摄像头均连接在I2C通道42个摄像头共用同一个MIPI数据通道2个摄像…

LeetCode:454. 四数相加 II —— 哈希表为什么叫哈希表~

&#x1f34e;道阻且长&#xff0c;行则将至。&#x1f353; &#x1f33b;算法&#xff0c;不如说它是一种思考方式&#x1f340; 算法专栏&#xff1a; &#x1f449;&#x1f3fb;123 hash是什么&#xff0c;哈希表为什么叫哈希表&#xff1f; 一、&#x1f331;454. 四数…

Java基础(八)异常处理

1. 异常概述 1.1 什么是生活的异常 男主角小明每天开车上班&#xff0c;正常车程1小时。但是&#xff0c;不出意外的话&#xff0c;可能会出现意外。 出现意外&#xff0c;即为异常情况。我们会做相应的处理。如果不处理&#xff0c;到不了公司。处理完了&#xff0c;就可以…

一文搞懂Plant Simulation中的Rotation设置

在处理3D动画或者展示时,常常需要在Plant Simulation调整数模的姿态,静态设置或动态设置Rotation是一个很重要的手段。 编辑3D属性,在Transformation选项卡中,我们可以看到Rotation的设置参数,如上图所示,只有一个角度和3个轴参数。如果对计算机图形学不了解的同学,估计…

MySQL优化——Explain分析执行计划详解

文章目录 前言一. 查看SQL执行频率二. 定位低效率执行SQL三. explain分析执行计划3.1 id3.2 select_type3.3 table3.4 type3.5 key3.6 rows3.7 extra 四. show profile分析SQL 前言 在应用的的开发过程中&#xff0c;由于初期数据量小&#xff0c;开发人员写 SQL 语句时更重视…
最新文章