通过串口+OpenCv 的智能车图像还原
做过智能车摄像头的都知道,我们在做车的过程中如果光是看那些单片机传回来的01字符串的图像数据的话,那可是一件相关无聊的事情。不仅如此,看这样的数据还很废眼睛的。这样的话看没过多眼睛就开始累了,就开始不想干活了。
然后再附上原代码:
#include <iostream>
#include <windows.h>
#include <winbase.h>
#include <conio.h>
#include <cstdio>
#include <cstdlib>
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <cxcore.h>
using namespace std;
HANDLE hComDev;
OVERLAPPED m_ov;
COMSTAT comstat;
DWORD m_dwCommEvents;
bool bRead = true;
bool bResult = true;
DWORD dwError = 0;
DWORD bytesRead = 0;
/**打开串口设备**/
bool openPort(char *portname);
/**设置串口设备DCB参数**/
bool setupDcb(int rate_arg);
/**设置串口设备超时限制参数**/
bool setuptimeout(DWORD ReadInterval,DWORD ReadTotalMultiplier,
DWORD ReadTotalconstant,DWORD WriteTotalMultiplier,
DWORD WriteTotalconstant);
/**串口收发数据**/
void ReceiveChar(unsigned char *RXBuffer);
bool WriteChar(char* m_szWriteBuffer,DWORD m_nToSend);
void createImage(void);
int main(int argc,char**argv)
{
bool openFlag = false;
openFlag = openPort((char*)"COM5");
if(openFlag)
{
printf("打开串口成功!\n");
if(setupDcb(115200))
{
printf("波特率设置成功\n");
}
if(setuptimeout(0, 0, 0, 0, 0))
{
printf("超时限制设置成功\n");
SetCommMask(hComDev,EV_RXCHAR);//当有字符在 inbuf 中时产生这个事件
//清除串口的所有操作
PurgeComm(hComDev,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_TXABORT);
//WriteChar((char*)"please send data now",20);
printf("received data:\n");
createImage();
CloseHandle(hComDev);
}
}
return 0;
}
/**打开串口设备**/
bool openPort(char *portname)
{
hComDev = CreateFile(portname, //串口号
GENERIC_READ|GENERIC_WRITE,//允许读写
0,//通讯设备以独占方式打开
0,//无安全属性
OPEN_EXISTING,//通讯设备已存在
FILE_FLAG_OVERLAPPED,//异步I/O
0);//通讯设备不能用模板打开
if(hComDev == INVALID_HANDLE_VALUE)
{
CloseHandle(hComDev);
printf("打开串口失败!\n");
return false;
}
else
{
return true;
}
}
bool setupDcb(int rate_arg)
{
DCB dcb;
//int rate = rate_arg;
memset(&dcb, 0, sizeof(dcb));
if(!GetCommState(hComDev, &dcb))
{
return false;
}
else
{
// set DCB to configure the serial port
//波特率为115200,无奇偶校验,8位数据位,1位停止位
//BuildCommDCB("115200,N,8,1",&dcb);
dcb.DCBlength = sizeof(dcb);
// ---------- Serial Port Config -------
dcb.BaudRate = rate_arg;//波特率
dcb.Parity = NOPARITY;//奇偶校验
dcb.fParity = 0;
dcb.StopBits = ONESTOPBIT;//1位停止位
dcb.ByteSize = 8;//8位数据位
dcb.fOutxCtsFlow = 0;
dcb.fOutxDsrFlow = 0;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = 0;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fOutX = 0;
dcb.fInX = 0;
//---------- misc parameters -----
dcb.fErrorChar = 0;
dcb.fBinary = 1;
dcb.fNull = 0;
dcb.fAbortOnError = 0;
dcb.wReserved = 0;
dcb.XonLim = 2;
dcb.XoffLim = 4;
dcb.XonChar = 0x11;
dcb.XoffChar = 0x13;
dcb.EvtChar = 0;
// set DCB
if(!SetCommState(hComDev,&dcb))
{
return false;
}
else
{
return true;
}
}
}
bool setuptimeout(DWORD ReadInterval,DWORD ReadTotalMultiplier,DWORD
ReadTotalconstant,DWORD WriteTotalMultiplier,
DWORD WriteTotalconstant)
{
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout=ReadInterval;
timeouts.ReadTotalTimeoutConstant=ReadTotalconstant;
timeouts.ReadTotalTimeoutMultiplier=ReadTotalMultiplier;
timeouts.WriteTotalTimeoutConstant=WriteTotalconstant;
timeouts.WriteTotalTimeoutMultiplier=WriteTotalMultiplier;
if(!SetCommTimeouts(hComDev, &timeouts))
return false;
else
return true;
}
void ReceiveChar( unsigned char *RXBuffer)
{
if(bRead)
{
bResult = ReadFile(hComDev,//Handle to Comm port
RXBuffer,//RX Buffer Pointer
1, //Read one byte
&bytesRead,//Stores number of bytes read
&m_ov); //pointer to the m_ov structure
if(!bResult)
{
switch(dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
bRead = false;
break;
}
default:
{
break;
}
}
}
else
{
bRead = true;
}
}
if(!bRead)
{
bRead = true;
bResult = GetOverlappedResult(hComDev,//Handle to Comm port
&m_ov,//Overlapped structure
&bytesRead,//Stores number of bytes read
true);// Wait flag
}
}
void createImage(void)
{
IplImage *src = NULL;
IplImage *dst = NULL;
int height,width,step,channels;
int i,j,k;
unsigned char *imgdata = NULL;
unsigned char data;
src = cvCreateImage(cvSize(145,48),IPL_DEPTH_8U, 1);
dst = cvCreateImage(cvSize(145 * 5,48 * 5),IPL_DEPTH_8U, 1);
height = src->height;
width = src->width;
step = src->widthStep;
channels = src->nChannels;
imgdata = (unsigned char *)src->imageData;
bRead = true;
bResult = true;
dwError = 0;
bytesRead = 0;
for(;;)
{
bResult = ClearCommError(hComDev,&dwError, &comstat);
if (comstat.cbInQue == 0)
{
continue;
}
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
for(k=0; k<channels; k++)
{
ReceiveChar(&data);
if(data == '1')
{
imgdata[i*step+j*channels+k]=255;
}
else
{
imgdata[i*step+j*channels+k]=0;
}
}
}
}
cvResize(src, dst, CV_INTER_LINEAR);
cvNamedWindow("src", CV_WINDOW_AUTOSIZE);
cvShowImage("src", dst);
if(cvWaitKey(1) == 27)
{
cvDestroyWindow ("src");
cvReleaseImage(&src);
cvReleaseImage(&dst);
return;
}
}
}
bool WriteChar(char* m_szWriteBuffer,DWORD m_nToSend)
{
BOOL bWrite = TRUE;
BOOL bResult = TRUE;
DWORD BytesSent = 0;
//HANDLE m_hWriteEvent;
//ResetEvent(m_hWriteEvent);
if (bWrite)
{
m_ov.Offset = 0;
m_ov.OffsetHigh = 0;
// Clear buffer
bResult = WriteFile(hComDev, // Handle to COMM Port
m_szWriteBuffer, // Pointer to message buffer in calling finction
m_nToSend, // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&m_ov ); // Overlapped structure
if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
// continue to GetOverlappedResults()
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
// all other error codes
break;
}
}
}
} // end if(bWrite)
if (!bWrite)
{
bWrite = TRUE;
bResult = GetOverlappedResult(hComDev, // Handle to COMM port
&m_ov, // Overlapped structure
&BytesSent, // Stores number of bytes sent
TRUE); // Wait flag
// deal with the error code
if (!bResult)
{
printf("GetOverlappedResults() in WriteFile()");
}
} // end if (!bWrite)
// Verify that the data size send equals what we tried to send
if (BytesSent != m_nToSend)
{
printf("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n",
(int)BytesSent, (int)strlen((char*)m_szWriteBuffer));
}
return true;
}
转载于:https://www.cnblogs.com/timber-wolf/p/3218808.html
通过串口+OpenCv 的智能车图像还原相关推荐
- 智能车图像部分——摄像头寻迹
智能车图像部分--摄像头寻迹(学习笔记) (1)摄像头基本原理 ·中断处理 pclk 每输出一个像素值产生一次 href 每输出完一行产生一次 vsync 每输出一场产生一次 ·DMA传输 摄像头初始 ...
- 【第十七届智能车】智能车图像处理(1)-图像预处理
本博客使用的硬件是逐飞总钻风130°无畸变摄像头,采用的图像分辨率为188*120,主控为CH32V307VCT6,使用DVI接口进行连接. 前言 我们在本次比赛中采用的是头尾车总钻风摄像头+中间车线 ...
- 智能车浅谈——图像篇
文章目录 前言 认识图像 基本含义 图像类型 数字图像 彩色图像 灰度图像 黑白图像 小结 图像处理 图像压缩 二值化 固定阈值法 大津法 图像降噪(腐蚀) 寻边线 总结 17届完赛代码 17届完赛代 ...
- OpenCV + 树莓派制作智能车【1】
前言 以前制作智能车都是在STM32上制作的,学习了一点OpenCV想要实践一下,一下就想到了买来一直在吃灰的树莓派,做一个智能小车吧! 黑线识别 我们先在Windows上写出基本能跑的检测代码: 首 ...
- 智能车复工日记【7】:关于会车的图像问题
2022.3.10新增订阅通知 近期把此专栏设置为了付费模式,可以直接花9.9买这个专栏,买了就可以直接这个专栏的所有文章了.后续也会更新一些经验贴.也可以进交流群咨询群中大佬问题,查看相关代码以及资 ...
- 【智能车Code review】——坡道图像与控制处理
博主联系方式: QQ:1540984562 微信:wxid_nz49532kbh9u22 QQ交流群:892023501 2022.3.10新增订阅通知 近期把此专栏设置为了付费模式,可以直接花9.9 ...
- 智能车竞赛技术报告 | 智能视觉组 - 哈尔滨工程大学 - 济海追风0队
简 介: 本文主要介绍了第十六届智能车竞赛智能视觉组的相关设计思想.此车系统由i.mx8处理器为上位机,RT1064为下位机.通过摄像头传感器检测赛道.使用光电编码器采集车轮速度.结合车模赛道物理模型 ...
- 智能车竞赛技术报告 | 智能车视觉 - 中国海洋大学 - The Waves
简 介: 本文详细介绍了在第十六届全国大学生智能汽车竞赛中智能视觉组的系统方案.本组别的比赛采用大赛组委会统一指定的C型车模,以恩智浦半导体公司生产的32位单片机I.MX RT1064 为核心控制器, ...
- 智能车竞赛技术报告 | 智能车视觉 - 南京邮电大学 - 栅库砸车跑路队
学 校:南京邮电大学 队伍名称:删库砸车跑路队 参赛队员:刘乐 孙锐 甘翠 带队教师:江兵 刘烨 第一章 方案设计 本章主要介绍智能汽车系统总体 ...
最新文章
- 标头“Vary:Accept-Encoding”指定方法及其重要性分析
- nlp homework 03
- 【转载】linux2.6内核initrd机制解析
- 如何配置LCD背光和LED,调试方法
- Maven编译时提示:不兼容的类型
- 【每日一题】7月1日题目精讲 借教室
- WinCE下监视设备插拔的参考代码
- xml引入约束示例(xsd文件)
- c/c++入门教程 - 3 职工管理系统 完整代码
- 【深度优先搜索/树】计蒜客:族谱
- [PWA] Check Online Status by using the NavigatorOnLine API
- js获取ck_JS获取CkEditor在线编辑的内容
- matlab阶乘函数、排列组合函数、数组求和、未定义变量带入计算,简化结果值
- 母函数与指数型母函数模板
- AAAI2020论文列表(中英对照)
- 李白的诗-按时间顺序的创作
- 零基础想要做好人物角色模型,先了解人体的构造!快来康康
- requests.exceptions.SSLError: HTTPSConnectionPool(host='XXX', port=443)问题
- 今日心得:七条人生箴言
- 图像修复必读的10篇论文
热门文章
- R语言使用strptime函数把字符串转化为时间对象、使用strftime函数把时间对象转化为字符串实战:指定时区、包含时间、分钟、秒、微妙
- 如何用matlab画图表示几组数据,Excel表格如何对多数据画图-EXCEL里如何将表示两组数据的两种图形放在同一个表......
- 新玺配资:市场再度进入纠结行情阶段
- 我的世界服务器邮件指令是什么,我的世界OP指令是什么 我的世界OP指令大全
- ftp上传文件报错 Unexpected null reply received
- ADAS一体机量产之路已通,自动泊车成魔视智能的下一个关键词
- 你好,法语A1知识点书本语法汇总
- codeforce1073 C. Vasya and Robot 二分
- Cocos2d-x添加IOS手机震动
- 【iOS程序启动与运转】- RunLoop个人小结