opencv_tutorials_t5
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
opencv_tutorials_t5 [2016/06/06 14:05] – created joaomatos | opencv_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 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/ | ||
+ | |||
+ | 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. | ||
+ | |||
+ | {{:: | ||
+ | |||
+ | ---- | ||
+ | ===== Color Threshold ===== | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | < | ||
+ | |||
+ | // | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | |||
+ | #define _CRT_SECURE_NO_WARNINGS | ||
+ | |||
+ | //defining the namespace used ( no need to use cv:: or std:: before the API' | ||
+ | using namespace cv; | ||
+ | using namespace std; | ||
+ | |||
+ | |||
+ | //Values to be used on the HSV trackbar | ||
+ | //these will be changed using trackbars | ||
+ | int iLowH = 0; | ||
+ | int iHighH = 179; | ||
+ | |||
+ | int iLowS = 0; | ||
+ | int iHighS = 255; | ||
+ | |||
+ | int iLowV = 0; | ||
+ | int iHighV = 255; | ||
+ | |||
+ | //default capture width and height | ||
+ | const int FRAME_WIDTH = 640; | ||
+ | const int FRAME_HEIGHT = 480; | ||
+ | |||
+ | //Function to create a window with the Trackbars to apply the Threshold. | ||
+ | |||
+ | void createTrackbars() { | ||
+ | |||
+ | //Open the window to display the Trackbars | ||
+ | namedWindow(" | ||
+ | |||
+ | //Hue values (0 - 179) | ||
+ | cvCreateTrackbar(" | ||
+ | cvCreateTrackbar(" | ||
+ | |||
+ | // | ||
+ | cvCreateTrackbar(" | ||
+ | cvCreateTrackbar(" | ||
+ | |||
+ | //Value (0-255) | ||
+ | cvCreateTrackbar(" | ||
+ | cvCreateTrackbar(" | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | //Function to apply the erode and dilate features. | ||
+ | void DilateAndErode(Mat &image) { | ||
+ | |||
+ | //Defining the erode and dilate properties | ||
+ | //the erode element chosen here is a 3x3 piexels rectangle. | ||
+ | //Change the Size argument to optimize your threshold. | ||
+ | //dilate with 8x8 size element to make the threshold object more visible | ||
+ | |||
+ | Mat erodeElement = getStructuringElement(MORPH_RECT, | ||
+ | Mat dilateElement = getStructuringElement(MORPH_RECT, | ||
+ | |||
+ | //Apply erode and dilate | ||
+ | erode(image, | ||
+ | dilate(image, | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | int main(int argc, char* argv[]) | ||
+ | { | ||
+ | //auxiliar variable to control the actions of the program | ||
+ | int key; | ||
+ | bool useFeatures = true; | ||
+ | |||
+ | //Matrix to store the webcam image, the HSV image and the Threshold image | ||
+ | Mat colorimage; | ||
+ | Mat HSV; | ||
+ | Mat threshold; | ||
+ | |||
+ | //Call the function to create the window with the trackbars | ||
+ | createTrackbars(); | ||
+ | // | ||
+ | VideoCapture capture; | ||
+ | capture.open(0); | ||
+ | |||
+ | //set height and width of capture frame | ||
+ | capture.set(CV_CAP_PROP_FRAME_WIDTH, | ||
+ | capture.set(CV_CAP_PROP_FRAME_HEIGHT, | ||
+ | |||
+ | char key = 0; | ||
+ | //Until the User press q the loop will run | ||
+ | //Get the image from the webcam -> convert to HSV -> Threshold the image | ||
+ | //using the HSV max and min set on the Trackbar window. | ||
+ | // When m is pressed the morphological transform will be applied. | ||
+ | while (key != ' | ||
+ | { | ||
+ | |||
+ | if (key == ' | ||
+ | { | ||
+ | useFeatures = !useFeatures; | ||
+ | } | ||
+ | |||
+ | //Get the image from the webcam | ||
+ | capture >> colorimage | ||
+ | //Convert the frame from BGR (RGB) to HSV | ||
+ | cvtColor(colorimage, | ||
+ | |||
+ | |||
+ | //filter the HSV image using the minimum and maximum values set on the | ||
+ | // | ||
+ | inRange(HSV, | ||
+ | |||
+ | |||
+ | //If the key M is pressed the dilate and erode features will be activated. | ||
+ | if (useFeatures) | ||
+ | { | ||
+ | DilateAndErode(threshold); | ||
+ | } | ||
+ | |||
+ | |||
+ | //show Threshold , Webcam and HSV images. | ||
+ | imshow(" | ||
+ | imshow(" | ||
+ | imshow(" | ||
+ | |||
+ | key = waitKey(25); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Understanding the code ===== | ||
+ | |||
+ | < | ||
+ | //Values to be used on the HSV trackbar | ||
+ | //these will be changed using trackbars | ||
+ | int iLowH = 0; | ||
+ | int iHighH = 179; | ||
+ | |||
+ | int iLowS = 0; | ||
+ | int iHighS = 255; | ||
+ | |||
+ | int iLowV = 0; | ||
+ | int iHighV = 255; | ||
+ | |||
+ | //default capture width and height | ||
+ | const int FRAME_WIDTH = 640; | ||
+ | const int FRAME_HEIGHT = 480; | ||
+ | |||
+ | </ | ||
+ | |||
+ | Some variables will be used inside the functions and inside the main program , so is convenient to set them as global variables (defined outside all ). We will create slider bars to adjust the Hue, Saturation and Value to Threshold our image , so we define the minimum and maximum value that each of this variables can get . Also we initialize the size of the captured image from our webcam as 640x480 pixels. | ||
+ | |||
+ | |||
+ | ---- | ||
+ | < | ||
+ | //Function to create a window with the Trackbars to apply the Threshol. | ||
+ | |||
+ | void createTrackbars() { | ||
+ | |||
+ | //Open the window to display the Trackbars | ||
+ | namedWindow(" | ||
+ | |||
+ | //Hue values (0 - 179) | ||
+ | cvCreateTrackbar(" | ||
+ | cvCreateTrackbar(" | ||
+ | |||
+ | // | ||
+ | cvCreateTrackbar(" | ||
+ | cvCreateTrackbar(" | ||
+ | |||
+ | //Value (0-255) | ||
+ | cvCreateTrackbar(" | ||
+ | cvCreateTrackbar(" | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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 **" | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | //Function to apply the erode and dilate features. | ||
+ | void DilateAndErode(Mat &image) { | ||
+ | |||
+ | //Defining the erode and dilate properties | ||
+ | //the erode element chosen here is a 3x3 piexels rectangle. | ||
+ | //Change the Size argument to optimize your threshold. | ||
+ | //dilate with 8x8 size element to make the threshold object more visible | ||
+ | |||
+ | Mat erodeElement = getStructuringElement(MORPH_RECT, | ||
+ | Mat dilateElement = getStructuringElement(MORPH_RECT, | ||
+ | |||
+ | //Apply erode and dilate | ||
+ | erode(image, | ||
+ | dilate(image, | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | This function will be used to apply the morphological transformations erode and dilate. Both are used to make the threshold object more visible . Erode is useful to reject small noises in our threshold image (the noise is all the white little points that are thresholded together with the object but is not part of the object). Dilate is useful to be used after the erosion, because it will make the threshold object more visible , and will not amplify the noise because all the noise was already filtered by the erosion. Read more about morphological transformations [[http:// | ||
+ | |||
+ | We first use the **" | ||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | int main(int argc, char* argv[]) | ||
+ | { | ||
+ | //auxiliar variable to control the actions of the program | ||
+ | bool useFeatures = true; | ||
+ | |||
+ | //Matrix to store the webcam image, the HSV image and the Threshold image | ||
+ | Mat cameraFeed; | ||
+ | Mat HSV; | ||
+ | Mat threshold; | ||
+ | |||
+ | //Call the function to create the window with the trackbars | ||
+ | createTrackbars(); | ||
+ | // | ||
+ | cv:: | ||
+ | capture.open(0); | ||
+ | |||
+ | //set height and width of capture frame | ||
+ | capture.set(CV_CAP_PROP_FRAME_WIDTH, | ||
+ | capture.set(CV_CAP_PROP_FRAME_HEIGHT, | ||
+ | |||
+ | </ | ||
+ | |||
+ | Our main program will start defining the variables to be used to store the web camera video , the HSV version of the web camera video and the threshold version of the web camera video.Also a boolean variable will be declared to control the on/off use of the morphological transformations. We call the function that we made to create a window to display the slider bars to apply the threshold using **" | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | < | ||
+ | char key = 0; | ||
+ | //Until the User press q the loop will run | ||
+ | //Get the image from the webcam -> convert to HSV -> Threshold the image | ||
+ | //using the HSV max and min set on the Trackbar window. | ||
+ | // When m is pressed the morphological transform will be applied. | ||
+ | while (key != ' | ||
+ | { | ||
+ | |||
+ | if (key == ' | ||
+ | { | ||
+ | useFeatures = !useFeatures; | ||
+ | } | ||
+ | |||
+ | //Get the image from the webcam | ||
+ | capture >> colorimage | ||
+ | //Convert the frame from BGR (RGB) to HSV | ||
+ | cvtColor(colorimage, | ||
+ | |||
+ | </ | ||
+ | |||
+ | Now we start our loop to capture frames from the webcam and work with these frames. The loop will run until the " | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | //filter the HSV image using the minimum and maximum values set on the | ||
+ | // | ||
+ | inRange(HSV, | ||
+ | |||
+ | |||
+ | //If the key M is pressed the dilate and erode features will be activated. | ||
+ | if (useFeatures) | ||
+ | { | ||
+ | DilateAndErode(threshold); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | We will threshold the HSV version because the colors are easily detected on the HSV version. We will threshold the image sliding the HSV bars on the Trackbars window , change the upper and lower values until only the object that you want to filter is visible ( it will be all black and the object white ). The threshold creates a binary image , where 0 is black and 1 is white - all the 1's will represent our object. Now with this binary image is easier to apply object tracking algorithm and find edges and contours. | ||
+ | |||
+ | The threshold is done by the **" | ||
+ | |||
+ | A conditional statement is introduced , whenever the boolean " | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | //show Threshold , Webcam and HSV images. | ||
+ | imshow(" | ||
+ | imshow(" | ||
+ | imshow(" | ||
+ | |||
+ | key = waitKey(25); | ||
+ | </ | ||
+ | |||
+ | | ||
+ | \\ | ||
+ | \\ | ||
+ | Below is a video demonstrating the program in real time. | ||
+ | {{youtube> |
opencv_tutorials_t5.1465247147.txt.gz · Last modified: by joaomatos