Advertisement
Guest User

Untitled

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