Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1.  
  2.  
  3. #include <sstream>
  4. #include <string>
  5. #include <iostream>
  6. #include "opencv2/imgproc.hpp"
  7. #include "opencv2/highgui.hpp"
  8. #include "opencv2/core.hpp"
  9.  
  10. using namespace std;
  11. using namespace cv;
  12. //initial min and max HSV filter values.
  13. //these will be changed using trackbars
  14. int H_MIN = 0;
  15. int H_MAX = 256;
  16. int S_MIN = 0;
  17. int S_MAX = 256;
  18. int V_MIN = 0;
  19. int V_MAX = 256;
  20. //default capture width and height
  21. const int FRAME_WIDTH = 640;
  22. const int FRAME_HEIGHT = 480;
  23. //max number of objects to be detected in frame
  24. const int MAX_NUM_OBJECTS=50;
  25. //minimum and maximum object area
  26. const int MIN_OBJECT_AREA = 20*20;
  27. const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH/1.5;
  28. //names that will appear at the top of each window
  29. const std::string windowName = "Original Image";
  30. const std::string windowName1 = "HSV Image";
  31. const std::string windowName2 = "Thresholded Image";
  32. const std::string windowName3 = "After Morphological Operations";
  33. const std::string trackbarWindowName = "Trackbars";
  34.  
  35. int xCoord=0, yCoord=0;
  36. void on_trackbar( int, void* )
  37. {//This function gets called whenever a
  38. // trackbar position is changed
  39.  
  40.  
  41.  
  42.  
  43.  
  44. }
  45. std::string intToString(int number){
  46.  
  47.  
  48. std::stringstream ss;
  49. ss << number;
  50. return ss.str();
  51. }
  52. void createTrackbars(){
  53. //create window for trackbars
  54.  
  55.  
  56. namedWindow(trackbarWindowName,0);
  57. //create memory to store trackbar name on window
  58. char TrackbarName[50];
  59. sprintf( TrackbarName, "H_MIN", H_MIN);
  60. sprintf( TrackbarName, "H_MAX", H_MAX);
  61. sprintf( TrackbarName, "S_MIN", S_MIN);
  62. sprintf( TrackbarName, "S_MAX", S_MAX);
  63. sprintf( TrackbarName, "V_MIN", V_MIN);
  64. sprintf( TrackbarName, "V_MAX", V_MAX);
  65. //create trackbars and insert them into window
  66. //3 parameters are: the address of the variable that is changing when the trackbar is moved(eg.H_LOW),
  67. //the max value the trackbar can move (eg. H_HIGH),
  68. //and the function that is called whenever the trackbar is moved(eg. on_trackbar)
  69. // ----> ----> ---->
  70. createTrackbar( "H_MIN", trackbarWindowName, &H_MIN, H_MAX, on_trackbar );
  71. createTrackbar( "H_MAX", trackbarWindowName, &H_MAX, H_MAX, on_trackbar );
  72. createTrackbar( "S_MIN", trackbarWindowName, &S_MIN, S_MAX, on_trackbar );
  73. createTrackbar( "S_MAX", trackbarWindowName, &S_MAX, S_MAX, on_trackbar );
  74. createTrackbar( "V_MIN", trackbarWindowName, &V_MIN, V_MAX, on_trackbar );
  75. createTrackbar( "V_MAX", trackbarWindowName, &V_MAX, V_MAX, on_trackbar );
  76.  
  77.  
  78. }
  79. void drawObject(int x, int y,Mat &frame){
  80.  
  81. //use some of the openCV drawing functions to draw crosshairs
  82. //on your tracked image!
  83.  
  84. //UPDATE:JUNE 18TH, 2013
  85. //added 'if' and 'else' statements to prevent
  86. //memory errors from writing off the screen (ie. (-25,-25) is not within the window!)
  87.  
  88. cv::circle(frame,Point(x,y),20,Scalar(0,255,0),2);
  89. if(y-25>0)
  90. cv::line(frame,Point(x,y),Point(x,y-25),Scalar(0,255,0),2);
  91. else line(frame,Point(x,y),Point(x,0),Scalar(0,255,0),2);
  92. if(y+25<FRAME_HEIGHT)
  93. cv::line(frame,Point(x,y),Point(x,y+25),Scalar(0,255,0),2);
  94. else line(frame,Point(x,y),Point(x,FRAME_HEIGHT),Scalar(0,255,0),2);
  95. if(x-25>0)
  96. cv::line(frame,Point(x,y),Point(x-25,y),Scalar(0,255,0),2);
  97. else line(frame,Point(x,y),Point(0,y),Scalar(0,255,0),2);
  98. if(x+25<FRAME_WIDTH)
  99. cv::line(frame,Point(x,y),Point(x+25,y),Scalar(0,255,0),2);
  100. else cv::line(frame,Point(x,y),Point(FRAME_WIDTH,y),Scalar(0,255,0),2);
  101.  
  102. putText(frame,intToString(x)+","+intToString(y),Point(x,y+30),1,1,Scalar(0,255,0),2);
  103.  
  104. }
  105. void morphOps(Mat &thresh){
  106.  
  107. //create structuring element that will be used to "dilate" and "erode" image.
  108. //the element chosen here is a 3px by 3px rectangle
  109.  
  110. Mat erodeElement = getStructuringElement( MORPH_ELLIPSE,Size(5,5));
  111. //dilate with larger element so make sure object is nicely visible
  112. Mat dilateElement = getStructuringElement( MORPH_ELLIPSE,Size(5,5));
  113.  
  114. erode(thresh,thresh,erodeElement);
  115.  
  116. dilate(thresh,thresh,dilateElement);
  117. dilate(thresh,thresh,dilateElement);
  118.  
  119. erode(thresh,thresh,erodeElement);
  120.  
  121.  
  122. }
  123.  
  124. void trackFilteredObject(int &x, int &y, Mat threshold, Mat &cameraFeed){
  125.  
  126. Mat temp;
  127. threshold.copyTo(temp);
  128. //these two vectors needed for output of findContours
  129. std::vector< std::vector<Point> > contours;
  130. std::vector<Vec4i> hierarchy;
  131. //find contours of filtered image using openCV findContours function
  132. findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );
  133. //use moments method to find our filtered object
  134. double refArea = 0;
  135. bool objectFound = false;
  136. if (hierarchy.size() > 0) {
  137. int numObjects = hierarchy.size();
  138. //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
  139. if(numObjects<MAX_NUM_OBJECTS){
  140. for (int index = 0; index >= 0; index = hierarchy[index][0]) {
  141.  
  142. Moments moment = moments((cv::Mat)contours[index]);
  143. double area = moment.m00;
  144.  
  145. //if the area is less than 20 px by 20px then it is probably just noise
  146. //if the area is the same as the 3/2 of the image size, probably just a bad filter
  147. //we only want the object with the largest area so we safe a reference area each
  148. //iteration and compare it to the area in the next iteration.
  149. if(area>MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea){
  150. x = moment.m10/area;
  151. y = moment.m01/area;
  152. xCoord=x;
  153. yCoord=y;
  154. objectFound = true;
  155. refArea = area;
  156. std::cout<<area<<std::endl;
  157.  
  158. //Distanza
  159. double dist;
  160. if (refArea==0) {
  161. cout << "\nArea uguale a 0" << endl;}
  162. else if (refArea>0 && refArea<=295){
  163. dist = 50;
  164. cout << "\nLa distanza dall'oggetto รจ circa di " << dist << " cm\n" <<endl;
  165. }
  166. else if (refArea>295 && refArea<=320) {
  167. dist = 65;
  168. cout << "\nLa distanza dall'oggetto รจ circa di " << dist << " cm\n" <<endl;
  169. }
  170. else if (refArea>320 && refArea<=335){
  171. dist = 95;
  172. cout << "\nLa distanza dall'oggetto รจ circa di " << dist << " cm\n" <<endl;
  173. }
  174. else
  175. cout << "\nDistanza non determinata" <<endl;
  176. //fine distanza
  177. }else objectFound = false;
  178.  
  179.  
  180. }
  181. //let user know you found an object
  182. if(objectFound ==true){
  183. putText(cameraFeed,"Tracking Object",Point(0,50),2,1,Scalar(0,255,0),2);
  184. //draw object location on screen
  185. drawObject(x,y,cameraFeed);}
  186.  
  187. }else putText(cameraFeed,"TOO MUCH NOISE! ADJUST FILTER",Point(0,50),1,2,Scalar(0,0,255),2);
  188. }
  189. }
  190.  
  191. int main(int argc, char* argv[])
  192. {
  193. //some boolean variables for different functionality within this
  194. //program
  195. bool trackObjects = true;
  196. bool useMorphOps = true;
  197. //Matrix to store each frame of the webcam feed
  198. Mat cameraFeed;
  199. //matrix storage for HSV image
  200. Mat HSV;
  201. //matrix storage for binary threshold image
  202. Mat threshold;
  203. //x and y values for the location of the object
  204. int x=0, y=0;
  205. //create slider bars for HSV filtering
  206. createTrackbars();
  207. //video capture object to acquire webcam feed
  208. VideoCapture capture;
  209. //open capture object at location zero (default location for webcam)
  210. capture.open(0);
  211. //set height and width of capture frame
  212. capture.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);
  213. capture.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);
  214. //start an infinite loop where webcam feed is copied to cameraFeed matrix
  215. //all of our operations will be performed within this loop
  216. while(1){
  217. //store image to matrix
  218. capture.read(cameraFeed);
  219. //convert frame from BGR to HSV colorspace
  220. cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
  221. //filter HSV image between values and store filtered image to
  222. //threshold matrix
  223. inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold);
  224. //perform morphological operations on thresholded image to eliminate noise
  225. //and emphasize the filtered object(s)
  226. if(useMorphOps)
  227. morphOps(threshold);
  228. //pass in thresholded frame to our object tracking function
  229. //this function will return the x and y coordinates of the
  230. //filtered object
  231. if(trackObjects){
  232. trackFilteredObject(x,y,threshold,cameraFeed);
  233.  
  234. }//;
  235. // distance(area);
  236.  
  237. //show frames
  238. imshow(windowName2,threshold);
  239. imshow(windowName,cameraFeed);
  240. imshow(windowName1,HSV);
  241.  
  242. //delay 30ms so that screen can refresh.
  243. //image will not appear without this waitKey() command
  244. waitKey(30);
  245. }
  246. //10.5 cm
  247. return 0;
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement