串口通信,opencv3.0
窗口大小可调

编译
cmake ../
make
./color
正常光线下运行比较稳定,找圆比较准确。程序设置了圆的半径区间以及圆心最小间距,以满足产品的需求,可自行修改调试。

历程如下

#define USE_SERIAL 0 // 1:use serial  0:not use serial    //用串口调试时置1,不用串口时一定要置0,否则会报错

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <opencv2/core/core.hpp>  
#include <opencv2/opencv.hpp>


#if USE_SERIAL
#include <boost/asio.hpp>
#include <boost/bind.hpp>
using namespace boost::asio;

#endif


using namespace cv;
using namespace std;

int g_nThresh = 135;
Mat canny_output;
Mat imgOriginal;
Mat imgHSV;
Mat imgThresholded;//二值化后的图像数组
Mat bf;//对灰度图像进行双边滤波
Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
Mat dstImage;
int x,y,r;
int iLowH = 9;
int iHighH = 37;
int iLowS = 135;
int iHighS = 255;
int iLowV = 175;
int iHighV = 255;

int main(int argc, char** argv)
{
size_t i = 0;
int fd;
#if USE_SERIAL
char serial_buff[100]={0};
io_service iosev;
        //节点文件
/* fd = open("/dev/ttyUSB0",O_RDWR);
if( -1 == fd );
{
cout << "Error: Cannot open the serial port" << endl;
cout << endl;
return -1;
}
close(fd);
*/
        serial_port serial(iosev, "/dev/ttyUSB0");

        // 设置参数
        serial.set_option(serial_port::baud_rate(115200));
        serial.set_option(serial_port::flow_control(serial_port::flow_control::none));
        serial.set_option(serial_port::parity(serial_port::parity::none));
        serial.set_option(serial_port::stop_bits(serial_port::stop_bits::one));
        serial.set_option(serial_port::character_size(8));
//write(serial, buffer("Test!\r\n", sizeof("Test!\r\n")));
#endif

VideoCapture cap(-1); //capture the video from web cam

if (!cap.isOpened())  // if not success, exit program
{
cout << "Error: Cannot open the web cam" << endl;
cout << endl;
return -1;
}
#if USE_SERIAL
write(serial, buffer("Test!\r\n", sizeof("Test!\r\n")));
#endif

// namedWindow("imgThresholded", 0);    //Image can adjust size

while (true)
{

bool bSuccess = cap.read(imgOriginal); // read a new frame from video

if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
//区分颜色*****************************************************************************************************
vector<Mat> hsvSplit;//cevtor 容器
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

 //因为我们读取的是彩色图,直方图均衡化需要在HSV空间做
split(imgHSV, hsvSplit);//(多通道数组-》容器)
equalizeHist(hsvSplit[2], hsvSplit[2]);
merge(hsvSplit, imgHSV);

inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image

 //开操作 (去除一些噪点)
morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element);

//闭操作 (连接一些连通域)
morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element);

//addWeighted(imgThresholded, 1, canny_output, 1, 0., dstImage);
//morphologyEx(dstImage, dstImage, MORPH_CLOSE, element);

//找圆************************************************************************************************************

blur(imgThresholded, bf, Size(3, 3));
//用Canny算子检测边缘
Canny(bf, canny_output, g_nThresh, g_nThresh * 2, 3);

vector<Vec3f> circles;//声明一个向量,保存检测出的圆的圆心坐标和半径
HoughCircles(canny_output, circles, CV_HOUGH_GRADIENT, 1, 70, 250, 10, 5, 70);//霍夫变换检测圆1.5, 5, 150, 80, 1, 50

for ( i = 0; i < circles.size(); i++)//把霍夫变换检测出的圆画出来
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);

circle(imgOriginal, center, radius, Scalar(255, 255, 255), 3,8, 0);

// cout << "x="<<cvRound(circles[i][0]) << "\t" << "y="<< cvRound(circles[i][1]) << "\t"
// << "z="<< cvRound(circles[i][2]) << endl;//在控制台输出圆心坐标和半径
x=cvRound(circles[i][0]);
y=cvRound(circles[i][1]);
r=cvRound(circles[i][2]);
#if USE_SERIAL
sprintf( serial_buff,"A%03d%03d%03d%03dB",i,cvRound(circles[i][0])/8,cvRound(circles[i][1])/8,cvRound(circles[i][2]));
write(serial, buffer(serial_buff, 14 ));
//write(serial, buffer("AB", 2));

#endif
}
#if USE_SERIAL
if( i!=0 )
write(serial, buffer("\r\n", strlen("\r\n")));
#endif

//~~~~~~~~~~~~~~~~~~~~~~~~
imshow("imgThresholded", imgThresholded);//滤过颜色
cvResizeWindow("imgThresholded",640,480);//Image can adjust size
imshow("imgOriginal", imgOriginal);//寻找边沿
cvResizeWindow("imgOriginal",640,480);//Image can adjust size
//imshow("canny_output", canny_output);//原图显示找出的圆

char key = (char)waitKey(20);
if (key == 27)
break;
}

return 0;

}

基于ubuntu14视觉识别乒乓球_1的更多相关文章

  1. 基于Ubuntu14.04系统的nvidia tesla K40驱动和cuda 7.5安装笔记

    基于Ubuntu14.04系统的nvidia tesla K40驱动和cuda 7.5安装笔记 飞翔的蜘蛛人 注1:本人新手,文章中不准确的地方,欢迎批评指正 注2:知识储备应达到Linux入门级水平 ...

  2. 基于Ubuntu14.04下Suricata(一款高性能的网络IDS、IPS和网络安全监控引擎)的搭建(图文详解)(博主推荐)

    为什么,要写这篇论文? 是因为,目前科研的我,正值研三,致力于网络安全.大数据.机器学习研究领域! 论文方向的需要,同时不局限于真实物理环境机器实验室的攻防环境.也不局限于真实物理机器环境实验室的大数 ...

  3. WordPress博客平台的搭建--基于Ubuntu14服务器

    环境:阿里云服务器,系统Ubuntu14.04, 阿里云域名 大致流程:LNMP+WordPress 主要参考博客:VPS+LNMP+WordPress搭建个人网站/博客 遇到的问题: 1.在登陆域名 ...

  4. 基于Ubuntu14.04-LTS下安装docker

    1.sudo apt-get update --更新系统源 2.sudo apt-get install docker.io 3.将docker库的公钥中加入到本地apt中 sudo apt-key ...

  5. 全网最详细的基于Ubuntu14.04/16.04 + Anaconda2 / Anaconda3 + Python2.7/3.4/3.5/3.6安装Tensorflow详细步骤(图文)(博主推荐)

    不多说,直接上干货! 前言 建议参照最新的tensorflow安装步骤(Linux,官方网站经常访问不是很稳定,所以给了一个github的地址):         https://github.com ...

  6. Disconf 学习系列之全网最详细的最新稳定Disconf 搭建部署(基于Ubuntu14.04 / 16.04)(图文详解)

    不多说直接上干货! https://www.cnblogs.com/wuxiaofeng/p/6882596.html (ubuntu16.04) https://www.cnblogs.com/he ...

  7. 基于Ubuntu14.10的Hadoop+HBase环境搭建

    本篇博文中谈及的Hadoop和HBase都是单机版,简单了解. 首先在Ubuntu上搭建Hadoop开发环境,主要参考另外一篇博客,仔细照做并解决出现的问题即可. 地址:http://www.powe ...

  8. 基于CentOS6.5或Ubuntu14.04下Suricata里搭配安装 ELK (elasticsearch, logstash, kibana)(图文详解)

    前期博客 基于CentOS6.5下Suricata(一款高性能的网络IDS.IPS和网络安全监控引擎)的搭建(图文详解)(博主推荐) 基于Ubuntu14.04下Suricata(一款高性能的网络ID ...

  9. 搭建ubuntu14.04的hadoop集群【docker容器充当服务器】

    首先弄出来装有hadoop.java.ssh.vim的镜像起名badboyf/hadoop.做镜像有两种方法,一种是用Dockerfile来生成一个镜像,一种是基于ubuntu14.04的基础镜像生成 ...

随机推荐

  1. 数据的ID名生成新的引用索引树

    <?php $arr= [ '0'=>[ "id"=>2, "name"=>"建材", "pid" ...

  2. 二维码生成api

    <img id='qrcode_img' src='http://qr.liantu.com/api.php?text={$wenzi}&w={$width}' /> http:/ ...

  3. 执行3小时超长SQL的分析优化过程:从索引遇见IS NULL,到最佳实践

    月底高峰期,对一个典型项目抽查分析时,发现了一个超级慢.全表扫描的SQL,语句很简单,AWR中赫然在列,在我统计的截止时间内还没有结束... 使用v$active_session_history进一步 ...

  4. Linux实战教学笔记13:定时任务补充

    第十三节 定时任务补充 标签(空格分隔): Linux实战教学笔记 ---[更多资料点我查看][1] 1,生产环境常用Crontab专业实例 1.1书写crontab定时任务多个基本要领 1.1.1 ...

  5. webpack教程(二)——webpack.config.js文件

    首先我们需要安装一个webpack插件html-webpack-plugin,该插件的作用是帮助我们生成创建html入口文件.执行如下命令 npm install html-webpack-plugi ...

  6. ABP入门系列(19)——使用领域事件

    ABP入门系列目录--学习Abp框架之实操演练 源码路径:Github-LearningMpaAbp 1.引言 最近刚学习了下DDD中领域事件的理论知识,总的来说领域事件主要有两个作用,一是解耦,二是 ...

  7. maven Spring+Spring MVC+Mybatis+mysql轻量级Java web开发环境搭建

    之前一直在做的一个GIS系统项目,采用了jsp+servlet框架,数据传输框架采用了apache的thrift框架,短时多传的风格还不错,但是较其他的java web项目显得有点太臃肿了,现在给大家 ...

  8. Java的数据类型和参数传递

    Java提供的数据类型主要分为两大类:基本数据类型和引用数据类型. Java中的基本数据类型 名称 大小 取值范围 byte型 (字节) 8bit -128-127  (-2^7到2^7-1) sho ...

  9. vijos1056题解

    题目: 桌面上放了N个平行于坐标轴的矩形,这N个矩形可能有互相覆盖的部分,求它们组成的图形的面积. 在翻题目时,偶然发现了这道标号为WA的题目. 原来,以前我把一中培训的代码发了上去,却WA了4个点, ...

  10. python编程快速上手之第10章实践项目参考答案

      本章主要讲了python程序的调试,当程序有BUG或异常的时候,我们如何调试代码找出问题点.其实在本章之前的章节我们做练习的时候都会遇到各种各样的错语和异常,最初当不知道程序哪里出错的情况下不可否 ...