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
资料内容详见 如何实现视觉识别-识别形状



















