User Tools

Site Tools


opencv_tutorials_t5

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_t5 [2016/06/06 18:08] joaomatosopencv_tutorials_t5 [2017/05/13 17:44] (current) – [Understanding the code] acater
Line 1: Line 1:
 ===== Tutorial 5 ====== ===== Tutorial 5 ======
  
- On this tutorial you will learn the principles of color thresholding. It is a very useful tool to filter objects in an image by its color . Can be used together with tracking algorithm or Edge/Contours detectors to filter a specific object in an image.+ On this tutorial you will learn the principles of color threshold. It is a very useful tool to filter objects in an image by its color . Can be used together with tracking algorithm or Edge/Contours detectors to filter a specific object in an image
 + 
 + Doing the threshold is very simple , just adjust the slider bars created on the trackbar window to filter the color that you want , move the bars to make the object that you want as white , and all the other undesired object as black
  
  I recommend you to type the code on your own to get familiarized with the program language. If you have trouble , the original code is attached bellow ( Running on Visual Studio 2015 + OpenCV 3.1 ) * Check the installation guide to make sure that you linked all the OpenCV modules to your Visual Studio.  I recommend you to type the code on your own to get familiarized with the program language. If you have trouble , the original code is attached bellow ( Running on Visual Studio 2015 + OpenCV 3.1 ) * Check the installation guide to make sure that you linked all the OpenCV modules to your Visual Studio.
  
-{{::thresholding.rar| Thresholding }}+{{::thresholding.rar| Color Threshold }}
  
 ---- ----
Line 47: Line 49:
 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 74: Line 76:
  //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 183: Line 185:
 ---- ----
 <Code:c++ linenums:35> <Code:c++ linenums:35>
-//Function to create a window with the Trackbars to apply the Thresholding.+//Function to create a window with the Trackbars to apply the Threshol.
    
 void createTrackbars() { void createTrackbars() {
Line 205: Line 207:
 </Code> </Code>
  
-Our first function is the function that will create a window to display the slider bars to adjust the HSV Threshold. First we create a new window using **"namedWindow"** to display the slider bars. Each slider bar is created using the command **"cvCreateTrackbar"** . The first argument is the name of the slider bar , the second argument is where it will be displayed (put the same name as the window that you opened using the **namedWindow** ).The third argument is the value that this track bar will store , and the fourth argument is the maximum value that can be assigned.  We need to create six of these sliders, because our thresholding will be made between a minimum and a maximum value of each H,S,V values. So we create slider bars for minimum and maximum Hue , minimum and maximum Saturation and minimum and maximum Value.+Our first function is the function that will create a window to display the slider bars to adjust the HSV Threshold. First we create a new window using **"namedWindow"** to display the slider bars. Each slider bar is created using the command **"cvCreateTrackbar"** . The first argument is the name of the slider bar , the second argument is where it will be displayed (put the same name as the window that you opened using the **namedWindow** ).The third argument is the value that this track bar will store , and the fourth argument is the maximum value that can be assigned.  We need to create six of these sliders, because our threshold will be made between a minimum and a maximum value of each H,S,V values. So we create slider bars for minimum and maximum Hue , minimum and maximum Saturation and minimum and maximum Value.
  
  
Line 216: Line 218:
  //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 325: Line 327:
  
  Finally , we show the original image , thresholded image and HSV image in three different windows using **"imshow"** . We update the key value whenever any key is pressed.  Finally , we show the original image , thresholded image and HSV image in three different windows using **"imshow"** . We update the key value whenever any key is pressed.
 +\\
 +\\
 +Below is a video demonstrating the program in real time.
 +{{youtube>UXVKAUHEVjI?medium}}
opencv_tutorials_t5.1465261737.txt.gz · Last modified: by joaomatos