User Tools

Site Tools


opencv_tutorials_t6

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
opencv_tutorials_t6 [2016/06/07 13:29] joaomatosopencv_tutorials_t6 [2017/05/13 17:53] (current) – [Understanding the code] acater
Line 44: Line 44:
 const int FRAME_HEIGHT = 480; const int FRAME_HEIGHT = 480;
  
-//Function to create a window with the Trackbars to apply the Thresholding.+//Function to create a window with the Trackbars to apply the Threshold.
  
 void createTrackbars() { void createTrackbars() {
Line 71: Line 71:
  //Defining the erode and dilate properties  //Defining the erode and dilate properties
  //the erode element chosen here is a 3x3 piexels rectangle.  //the erode element chosen here is a 3x3 piexels rectangle.
- //Change the Size argument to optimize your thresholding.  + //Change the Size argument to optimize your threshold.  
- //dilate with 8x8 size element to make the thresholding object more visible+ //dilate with 8x8 size element to make the threshold object more visible
  
  Mat erodeElement = getStructuringElement(MORPH_RECT, Size(3, 3));  Mat erodeElement = getStructuringElement(MORPH_RECT, Size(3, 3));
Line 119: Line 119:
  capture.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);  capture.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
  
- //Until the User press Escape the loop will run+ //Until the User press 'q' the loop will run
  //Get the image from the webcam -> convert to HSV -> Threshold the image  //Get the image from the webcam -> convert to HSV -> Threshold the image
  //using the HSV max and min set on the Trackbar window.  //using the HSV max and min set on the Trackbar window.
Line 220: Line 220:
 const int FRAME_HEIGHT = 480; const int FRAME_HEIGHT = 480;
    
-//Function to create a window with the Trackbars to apply the Thresholding.+//Function to create a window with the Trackbars to apply the Threshold.
    
 void createTrackbars() { void createTrackbars() {
Line 243: Line 243:
 </Code> </Code>
  
- The code begins with the same idea of image thresholding. Declaring the variables to be used on the Trackbars windows and defining the function to create the slider bars. You can see the commentaries  [[http://www.daslhub.org/unlv/wiki/doku.php?id=opencv_tutorials_t5|here]]+ The code begins with the same idea of image threshold. Declaring the variables to be used on the Trackbars windows and defining the function to create the slider bars. You can see the commentaries  [[http://www.daslhub.org/unlv/wiki/doku.php?id=opencv_tutorials_t5|here]]
  
  
Line 254: Line 254:
  //Defining the erode and dilate properties  //Defining the erode and dilate properties
  //the erode element chosen here is a 3x3 piexels rectangle.  //the erode element chosen here is a 3x3 piexels rectangle.
- //Change the Size argument to optimize your thresholding.  + //Change the Size argument to optimize your threshold.  
- //dilate with 8x8 size element to make the thresholding object more visible+ //dilate with 8x8 size element to make the threshold object more visible
    
  Mat erodeElement = getStructuringElement(MORPH_RECT, Size(3, 3));  Mat erodeElement = getStructuringElement(MORPH_RECT, Size(3, 3));
Line 268: Line 268:
 </Code> </Code>
  
- Again the same idea of the image thresholding code. To track an object first we need to obtain a binary image with only the object visible - and the morphological transform helps to optimaze the visibility of the thresholded object in the binary image by filtering the noise and expanding the object contour.After obtaining the binary image , we can apply the edge and contour detector and we can track the object by its contour - because the object contour will be the only contour shown in the binary image.+ Again the same idea of the image threshold code. To track an object first we need to obtain a binary image with only the object visible - and the morphological transform helps to optimaze the visibility of the thresholded object in the binary image by filtering the noise and expanding the object contour.After obtaining the binary image , we can apply the edge and contour detector and we can track the object by its contour - because the object contour will be the only contour shown in the binary image.
  
  
Line 323: Line 323:
  
 <Code:c++ linenums:108> <Code:c++ linenums:108>
-//Until the User press Escape the loop will run+//Until the User press 'q' the loop will run
  //Get the image from the webcam -> convert to HSV -> Threshold the image  //Get the image from the webcam -> convert to HSV -> Threshold the image
  //using the HSV max and min set on the Trackbar window.  //using the HSV max and min set on the Trackbar window.
Line 396: Line 396:
 </Code> </Code>
  
- To use the function to find the contours "findContours" we need to declare first a vector that will store all the contours points , and a vector to represent the hierarchy argument. For the most basic applications the other arguments will not be changed , if you want to understand more you can read [[http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours|Here]].+ To use the function to find the contours "findContours" we need to declare first a vector that will store all the contours points , and a vector to represent the hierarchy argument - the hierarchy vector contain as many element as the number of contours found (if we know its size , we know how many contours were found) , and contains information about the image topology . For the most basic applications the other arguments will not be changed , if you want to understand more you can read [[http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours|Here]].
  
  The "findContours" function will take the image with the edge detected as input and will give a vector with the contours points stored on the 'contours' variable. Now we need to draw these contours found in our image.  The "findContours" function will take the image with the edge detected as input and will give a vector with the contours points stored on the 'contours' variable. Now we need to draw these contours found in our image.
Line 440: Line 440:
  
  We show the image with the web camera frame and the contour drawn using the **"imshow"** function and update the key variable value to check if the loop will continue / the morphological transformations will be used or not.  We show the image with the web camera frame and the contour drawn using the **"imshow"** function and update the key variable value to check if the loop will continue / the morphological transformations will be used or not.
 +\\
 +\\
 +The video below demonstrates the program running in real time.
 +{{youtube>h3T0-a4YmPs?medium}}
opencv_tutorials_t6.1465331358.txt.gz · Last modified: by joaomatos