分享一个最简单的方法
安装好opencv后,我先在电脑实现了opencv与arduino的通信,现在写的是通过串口与arduino,若是想用其他通信,只需根据通信协议,按照数据发送接收格式改一下即可。
#include "windows.h"
#include <conio.h>
#include <tchar.h>
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <iostream>
#include <fstream>
#include "cv.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
using namespace cv;
using namespace std;
//声明全局变量
Mat src; Mat src_gray;
int thresh=10;
int max_thresh=255;
RNG rng(12345);
int n=0;
int m=0;
size_t i=0;
void thresh_callback(int,void*);
void _tmain(int argc);
string intToString(int number){
std::stringstream ss;
ss << number;
return ss.str();
}
void thresh_callback(int a,void*)
{
Mat canny_output;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
//使用canny边缘
Canny(src_gray,canny_output,thresh,thresh*2,3);
imshow("canny",canny_output);
//寻找轮廓
findContours(canny_output,contours,hierarchy, RETR_TREE,CHAIN_APPROX_SIMPLE,Point(0,0));
imshow("canny",canny_output);
Mat drawing=Mat::zeros(canny_output.size(),CV_8UC3);
for (i=0; i<contours.size();i++)
{
n++;
Scalar color=Scalar(rng.uniform(254,255),rng.uniform(254,255),rng.uniform(254,255));
//绘制轮廓
drawContours(drawing,contours,(int)i,color,2,8,hierarchy,0,Point());
}
m=i;
namedWindow("Contours",WINDOW_AUTOSIZE);
imshow("Contours",drawing);
}
int main(int argc,char* argv[])
{
VideoCapture cap(0) ;//(0)为本机摄像头,(1)以上是外接摄像头
// fJF# XHR
if (!cap.isOpened())
{
cout << "无法启动摄像头" << endl;
return -1;
}
char* windowName = "Webcam Feed";
// #?BX#X#Z
char filename [50] ;
//11 X#Zè5IS tXt
int n=0;
// Edè?
namedWindow(windowName, CV_WINDOW_AUTOSIZE);
while (1) {
Mat frame;
// M HXф5 (frame)
bool bSuccess = cap.read(frame);
// tEE SiXHX5XDJ
if (!bSuccess)
{
cout << "无法从摄像头读取帧" << endl;
break;
}
//1157tНХ6ф
imshow (windowName, frame) ;
//等待10 毫秒是否按键
switch(waitKey(10))
{
//LESC键被按下('ese' AscIL 的值是27)
case 27:
//结束程序
return 0;
break;
// I'Enter'键被按下('Enter' ASCII的值是13)
case 13:
sprintf(filename, "E:\\images\\VideoFrame.jpg",n++);
imwrite(filename, frame);
src=imread("E:\\images\\VideoFrame.jpg",1);
cvtColor(src,src_gray,COLOR_BGR2GRAY);
blur(src_gray,src_gray,Size(2,2));
//创建窗口
const char* source_window="Source";
namedWindow(source_window,WINDOW_AUTOSIZE);
imshow(source_window,src);
//建立滑竿
createTrackbar ("Canny thresh :","Source", &thresh,max_thresh,thresh_callback);
thresh_callback(0,0);
//if(m>m1)
printf("%d",m);
printf("\n" );
_tmain(m);
ofstream ocout;
ocout.open("test.txt");
ocout<<m;
ocout.close();
//else
// printf("1");
waitKey(10);
break;
}
}
return 0;
}
//d=68
//以下即实现与arduino的串口通信
void _tmain(int argc)
{
if(argc>1000){
char test[]="A"; //点亮
//创建串口
HANDLE hDevice=CreateFile(L"\\\\.\\COM6", //注意COM1~COM10与COM10以上的窗口的命名规则
GENERIC_READ|GENERIC_WRITE, //使用读写方式
0,
0,
OPEN_EXISTING,
0,
0);
if (hDevice!=INVALID_HANDLE_VALUE)
{
printf("Port opened!\n");
DCB lpTest;
GetCommState(hDevice,&lpTest); //获取当前的参数设置
lpTest.BaudRate=CBR_9600; //设置波特率
lpTest.ByteSize=8; //数据位数为8
lpTest.Parity=NOPARITY; //无校验
lpTest.StopBits = ONESTOPBIT; //1位停止位
SetCommState(hDevice,&lpTest); //设置通信参数
DWORD btsIO;
WriteFile(hDevice,test,strlen(test),&btsIO,NULL); //发送数据
Sleep(2000); //让程序暂停2s中,让二极管处在低电平维持一段时间
CloseHandle(hDevice); //关闭串口
}
}
else{
char test[]="B"; //点亮
//创建串口
HANDLE hDevice=CreateFile(L"\\\\.\\COM6", //注意COM1~COM10与COM10以上的窗口的命名规则
GENERIC_READ|GENERIC_WRITE, //使用读写方式
0,
0,
OPEN_EXISTING,
0,
0);
if (hDevice!=INVALID_HANDLE_VALUE)
{
printf("Port opened!\n");
DCB lpTest;
GetCommState(hDevice,&lpTest); //获取当前的参数设置
lpTest.BaudRate=CBR_9600; //设置波特率
lpTest.ByteSize=8; //数据位数为8
lpTest.Parity=NOPARITY; //无校验
lpTest.StopBits = ONESTOPBIT; //1位停止位
SetCommState(hDevice,&lpTest); //设置通信参数
DWORD btsIO;
WriteFile(hDevice,test,strlen(test),&btsIO,NULL); //发送数据
Sleep(2000); //让程序暂停2s中,让二极管处在低电平维持一段时间
CloseHandle(hDevice); //关闭串口
}
}
}