当前位置:   article > 正文

opencv+arduino进行物体点追踪_opencv和arduino结合

opencv和arduino结合

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

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

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


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/128810
推荐阅读
相关标签
  

闽ICP备14008679号