首页 技术 正文
技术 2022年11月7日
0 收藏 932 点赞 1,075 浏览 3920 个字

串口通信,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_SERIALchar 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_SERIALwrite(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_SERIALsprintf( 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_SERIALif( i!=0 )write(serial, buffer(“\r\n”, strlen(“\r\n”)));#endif
//~~~~~~~~~~~~~~~~~~~~~~~~imshow(“imgThresholded”, imgThresholded);//滤过颜色cvResizeWindow(“imgThresholded”,640,480);//Image can adjust sizeimshow(“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;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,494
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297