服务器之家

服务器之家 > 正文

opencv+arduino实现物体点追踪效果

时间:2021-06-11 15:36     来源/作者:wi162yyxq

本文所要实现的结果是:通过在摄像头中选择一个追踪点,通过pc控制摄像头的舵机,使这一点始终在图像的中心。

要点:使用光流法在舵机旋转的同时进行追踪,若该点运动,则摄像头跟踪联动。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include<opencv2\opencv.hpp>
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<math.h>
#include<Windows.h>
#include<string.h>
 
 
using namespace std;
using namespace cv;
 
#define WINDOW_NAME "【程序窗口】"   
 
void on_MouseHandle(int event, int x, int y, int flags, void* param);
void DrawRectangle( cv::Mat& img, cv::Rect box );
void tracking(Mat &frame,vector<Point2f> temp);
 
HANDLE hComm;
LPCWSTR pStr=L"COM4";
char lpOutbuffer[100];
DWORD dwbyte=100;
Mat srcImage,grayImage,tempImage1,tempImage,imageROI,grayprev;
int g_maxCornerNumber = 1;
double qualityLevel = 0.01;
double minDistance = 10;
int blockSize = 3;
double k = 0.04;
vector<Point2f> corners;
vector<Point2f> pre_corners;
vector<Point2f> counts;
vector<uchar> status;
vector<float> err;
Rect g_rectangle;
Rect g_temprectangle;
bool g_bDrawingBox = false;
 
 
int main( int argc, char** argv ) 
{
  Mat frame;
  Mat result;
 
  COMSTAT Comstat;
  DWORD dwError;
  BOOL bWritestat;
  hComm=CreateFile(pStr,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING, 0,NULL);
  if (hComm == INVALID_HANDLE_VALUE)
  {
    cout<<"FLASE";
    return -1;
  }
  else
  {
    cout<<"TURE";
  }
  DCB dcb;
  GetCommState(hComm,&dcb);
  dcb.BaudRate=9600;
  dcb.ByteSize=8;
  dcb.Parity=NOPARITY;
  dcb.StopBits=TWOSTOPBITS;
  bool set=SetCommState(hComm,&dcb);
  bool sup=SetupComm(hComm,1024,1024);
 
  VideoCapture capture(0);
 
  namedWindow( WINDOW_NAME );
  setMouseCallback(WINDOW_NAME,on_MouseHandle,(void*)&frame);
 
  while(1)
  {
    capture >> frame;
 
      if(!frame.empty())
      
        cvtColor(frame,grayImage,CV_RGB2GRAY);
        if( g_bDrawingBox ) 
        rectangle(frame,g_rectangle.tl(),g_rectangle.br(),Scalar(255,255,255));
        if (corners.size()!=0)
        {
          bool can=PurgeComm(hComm,PURGE_TXCLEAR);
          if (corners[0].x>(frame.cols/2+100))
          {
            lpOutbuffer[0]='a';
            bool ne=WriteFile(hComm,lpOutbuffer,dwbyte,&dwbyte,NULL);
          }
          else if (corners[0].x<(frame.cols/2-100))
          {
            lpOutbuffer[0]='b';
            bool ne=WriteFile(hComm,lpOutbuffer,dwbyte,&dwbyte,NULL);
          }
          tracking(frame,corners);
          rectangle(frame,Point(corners[0].x-10,corners[0].y-10),Point(corners[0].x+10,corners[0].y+10),Scalar(255,255,255));         
        }
        imshow( WINDOW_NAME, frame );
      }
      else
      
        printf(" --(!) No captured frame -- Break!");
        break;
      }
 
      int c = waitKey(50);
      if( (char)c == 27 )
      {
        break
      
  }
  return 0;
}
void on_MouseHandle(int event, int x, int y, int flags, void* param)
{
 
  Mat& image = *(cv::Mat*) param;
  switch( event)
  {
  case EVENT_MOUSEMOVE: 
    {
      if( g_bDrawingBox )
      {
        g_rectangle.width = x-g_rectangle.x;
        g_rectangle.height = y-g_rectangle.y;
      }
    }
    break;
 
  case EVENT_LBUTTONDOWN: 
    {
      g_bDrawingBox = true;
      g_rectangle =Rect( x, y, 0, 0 );
    }
    break;
 
  case EVENT_LBUTTONUP: 
    {
      g_bDrawingBox = false;
      if( g_rectangle.width < 0 )
      {
        g_rectangle.x += g_rectangle.width;
        g_rectangle.width *= -1;
      }
 
      if( g_rectangle.height < 0 ) 
      {
        g_rectangle.y += g_rectangle.height;
        g_rectangle.height *= -1;
      }
      imageROI=grayImage(g_rectangle);
      goodFeaturesToTrack( imageROI,corners,g_maxCornerNumber,qualityLevel,minDistance,Mat(),blockSize,false,k );
      for (int i = 0; i < corners.size(); i++)
      {
        corners[i].x=corners[i].x+g_rectangle.x;
        corners[i].y=corners[i].y+g_rectangle.y;
      }
    }
    break;
 
  }
}
 
void tracking(Mat &frame,vector<Point2f> temp)
{
  cvtColor(frame, tempImage1, COLOR_BGR2GRAY);
 
  if (grayprev.empty())
  {
    tempImage1.copyTo(grayprev);
  }
 
  calcOpticalFlowPyrLK(grayprev, tempImage1, temp, pre_corners, status, err);
 
  for (size_t i=0; i<pre_corners.size(); i++)
  {
    line(frame, temp[i], pre_corners[i], Scalar(0, 0, 255));
    circle(frame, pre_corners[i], 4, Scalar(0, 255, 0), -1,8,0);
  }
 
  swap(pre_corners, corners);
  swap(grayprev, tempImage1);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/wi162yyxq/article/details/52369484

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部