opencv_tutorials_t3
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
opencv_tutorials_t3 [2016/06/06 14:05] – created joaomatos | opencv_tutorials_t3 [2017/05/13 17:20] (current) – [Detecting Circles on Webcam] acater | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | Tutorial 3 | + | ===== Tutorial 3 ===== |
+ | |||
+ | On this tutorial you will learn how to detect circles on an image and video using a simple OpenCV Function and show the center location on the screen. | ||
+ | |||
+ | 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. | ||
+ | |||
+ | {{:: | ||
+ | |||
+ | {{:: | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Detecting Circles in Image ===== | ||
+ | |||
+ | {{ :: | ||
+ | |||
+ | < | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace cv; | ||
+ | using namespace std; | ||
+ | |||
+ | |||
+ | //Function to convert an integer number to a string. | ||
+ | String intToString(int number) | ||
+ | { | ||
+ | stringstream ss; | ||
+ | ss << number; | ||
+ | return ss.str(); | ||
+ | } | ||
+ | |||
+ | |||
+ | int main() | ||
+ | { | ||
+ | // | ||
+ | string radiusStr; | ||
+ | string xcenterStr; | ||
+ | string ycenterStr; | ||
+ | int Rvalue; | ||
+ | int Xvalue; | ||
+ | int Yvalue; | ||
+ | |||
+ | // Open the original colored image and convert to a grayscaled image | ||
+ | Mat coloredimage; | ||
+ | Mat grayimage; | ||
+ | |||
+ | |||
+ | |||
+ | //vector to store the center value ( x and y coordinates ) and the radius of each detected circle | ||
+ | vector< | ||
+ | |||
+ | //Open and resize the colored image. | ||
+ | coloredimage = imread(" | ||
+ | resize(coloredimage, | ||
+ | imshow(" | ||
+ | |||
+ | //convert colored image to gray scale | ||
+ | cvtColor(coloredimage, | ||
+ | |||
+ | // Apply blur to Reduce the noise to avoid false detections. | ||
+ | GaussianBlur(grayimage, | ||
+ | |||
+ | // Apply the Hough Transform to find the circles (use the gray scale image as input) | ||
+ | // | ||
+ | // 4: inverse ratio of resolution . 5 minimum distance between detected centers. 6: upper threshold for the internal canny edge detector | ||
+ | //7: threshold for center detection . 8: Minimum radius to be detected (0=unknown) . 9: maximum radius to be detected | ||
+ | |||
+ | |||
+ | HoughCircles(grayimage, | ||
+ | |||
+ | |||
+ | // Draw the circles detected | ||
+ | for (size_t i = 0; i < circles.size(); | ||
+ | { | ||
+ | //Get the informations from the circles vector generated by the function HoughCircles. | ||
+ | //X center coordinate is circles[i][0] | ||
+ | Point center(cvRound(circles[i][0]), | ||
+ | int radius = cvRound(circles[i][2]); | ||
+ | |||
+ | //Store these values into variables to be converted into string and displayed on the image | ||
+ | Rvalue = radius; | ||
+ | Xvalue = cvRound(circles[i][0]); | ||
+ | Yvalue = cvRound(circles[i][1]); | ||
+ | |||
+ | //DRAWING THE CENTER OF THE CIRCLE | ||
+ | //Use the circle function to draw the center of the detected circle | ||
+ | //Use the center coordinate and a radius of 3 to just draw a point on the center. | ||
+ | circle(coloredimage, | ||
+ | |||
+ | //DRAWING THE CIRCLE CONTOUR. | ||
+ | //Use the circle function to draw the detected circle on the image | ||
+ | //Use the center coordinate and the radius coordinate detected by the HoughCircles function | ||
+ | circle(coloredimage, | ||
+ | |||
+ | |||
+ | |||
+ | //Convert the integer Center point and radius values to string | ||
+ | radiusStr = intToString(Rvalue); | ||
+ | xcenterStr = intToString(Xvalue); | ||
+ | ycenterStr = intToString(Yvalue); | ||
+ | |||
+ | //Display on the colored image the center and radius values. | ||
+ | putText(coloredimage, | ||
+ | |||
+ | |||
+ | cout << " | ||
+ | } | ||
+ | |||
+ | // Show your results | ||
+ | namedWindow(" | ||
+ | imshow(" | ||
+ | |||
+ | waitKey(0); | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Understanding the Code ===== | ||
+ | |||
+ | < | ||
+ | //Function to convert an integer number to a string. | ||
+ | String intToString(int number) | ||
+ | { | ||
+ | stringstream ss; | ||
+ | ss << number; | ||
+ | return ss.str(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | We will need to display the center position on the screen , and in order to display a string on the image we will need to convert the X center and Y center position to strings , so it is convenient to create a simple function that will convert a given integer to string. This function will take an integer number as argument . We declare the variable ss as **" | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | // | ||
+ | string radiusStr; | ||
+ | string xcenterStr; | ||
+ | string ycenterStr; | ||
+ | int Rvalue; | ||
+ | int Xvalue; | ||
+ | int Yvalue; | ||
+ | |||
+ | // Open the original colored image and convert to a grayscaled image | ||
+ | Mat coloredimage; | ||
+ | Mat grayimage; | ||
+ | |||
+ | </ | ||
+ | |||
+ | We start defining the variables that will be used. Strings (**" | ||
+ | |||
+ | |||
+ | ---- | ||
+ | < | ||
+ | |||
+ | //vector to store the center value ( x and y coordinates ) and the radius of each detected circle | ||
+ | vector< | ||
+ | |||
+ | //Open and resize the colored image. | ||
+ | coloredimage = imread(" | ||
+ | resize(coloredimage, | ||
+ | imshow(" | ||
+ | |||
+ | //convert colored image to gray scale | ||
+ | cvtColor(coloredimage, | ||
+ | |||
+ | // Apply blur to Reduce the noise to avoid false detections. | ||
+ | GaussianBlur(grayimage, | ||
+ | |||
+ | // Apply the Hough Transform to find the circles (use the gray scale image as input) | ||
+ | // | ||
+ | // 4: inverse ratio of resolution . 5 minimum distance between detected centers. 6: upper threshold for the internal canny edge detector | ||
+ | //7: threshold for center detection . 8: Minimum radius to be detected (0=unknown) . 9: maximum radius to be detected | ||
+ | |||
+ | HoughCircles(grayimage, | ||
+ | |||
+ | </ | ||
+ | |||
+ | The function that will detect the circles will need a vector to store the X center , Y center and radius values from each detected circle , so we need to declare a vector to be used on that - **" | ||
+ | |||
+ | We will apply Hough Transform to find the circles. OpenCV has a function that already does all the procedures - **" | ||
+ | |||
+ | |||
+ | ---- | ||
+ | < | ||
+ | |||
+ | // Draw the circles detected | ||
+ | for (size_t i = 0; i < circles.size(); | ||
+ | { | ||
+ | //Get the informations from the circles vector generated by the function HoughCircles. | ||
+ | //Column coordinate is circles[i][0] | ||
+ | Point center(cvRound(circles[i][0]), | ||
+ | int radius = cvRound(circles[i][2]); | ||
+ | |||
+ | //Store these values into variables to be converted into string and displayed on the image | ||
+ | // X will be the column , Y will be the row. | ||
+ | Rvalue = radius; | ||
+ | Xvalue = cvRound(circles[i][0]); | ||
+ | Yvalue = cvRound(circles[i][1]); | ||
+ | |||
+ | //DRAWING THE CENTER OF THE CIRCLE | ||
+ | //Use the circle function to draw the center of the detected circle | ||
+ | //Use the center coordinate and a radius of 3 to just draw a point on the center. | ||
+ | circle(coloredimage, | ||
+ | |||
+ | //DRAWING THE CIRCLE CONTOUR. | ||
+ | //Use the circle function to draw the detected circle on the image | ||
+ | //Use the center coordinate and the radius coordinate detected by the HoughCircles function | ||
+ | circle(coloredimage, | ||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | The **" | ||
+ | |||
+ | To draw a circle on an image we use the **" | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | |||
+ | //Convert the integer Center point and radius values to string | ||
+ | radiusStr = intToString(Rvalue); | ||
+ | xcenterStr = intToString(Xvalue); | ||
+ | ycenterStr = intToString(Yvalue); | ||
+ | |||
+ | //Display on the colored image the center and radius values. | ||
+ | putText(coloredimage, | ||
+ | |||
+ | |||
+ | cout << " | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | To display the Center value on the image we use the **" | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | // Show your results | ||
+ | namedWindow(" | ||
+ | imshow(" | ||
+ | |||
+ | waitKey(0); | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Now we open a window and display the image with the draw on it. We put a different name from the first window so we open two different windows to display the original image and the image with the detected circle. | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Detecting Circles on Webcam ===== | ||
+ | |||
+ | {{ :: | ||
+ | |||
+ | |||
+ | < | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace cv; | ||
+ | using namespace std; | ||
+ | |||
+ | |||
+ | String intToString(int number) | ||
+ | { | ||
+ | |||
+ | |||
+ | std:: | ||
+ | ss << number; | ||
+ | return ss.str(); | ||
+ | } | ||
+ | |||
+ | |||
+ | int main() | ||
+ | { | ||
+ | //Varibles to store and show the detected circle properties | ||
+ | string radiusStr; | ||
+ | string xcenterStr; | ||
+ | string ycenterStr; | ||
+ | int Rvalue; | ||
+ | int Xvalue; | ||
+ | int Yvalue; | ||
+ | |||
+ | // | ||
+ | Mat coloredimage; | ||
+ | Mat grayimage; | ||
+ | |||
+ | //auxiliar variable to quit the loop and end the program | ||
+ | char key = 0; | ||
+ | |||
+ | //Open the default camera | ||
+ | VideoCapture capture(0); | ||
+ | |||
+ | //Check for Failure | ||
+ | if (!capture.isOpened()) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | //Set Capture device properties. | ||
+ | |||
+ | capture.set(CV_CAP_PROP_FRAME_WIDTH, | ||
+ | capture.set(CV_CAP_PROP_FRAME_HEIGHT, | ||
+ | |||
+ | |||
+ | //Loop will stop if " | ||
+ | while (key != ' | ||
+ | { | ||
+ | |||
+ | //Capture a frame of the webcam live video and store it on the image variable | ||
+ | capture >> coloredimage; | ||
+ | |||
+ | //Resize this frame and convert to gray scale | ||
+ | cvtColor(coloredimage, | ||
+ | |||
+ | // Apply blur to Reduce the noise so we avoid false circle detection | ||
+ | GaussianBlur(grayimage, | ||
+ | |||
+ | //create a vector to store the center value ( x and y coordinates ) and the radius of each detected circle | ||
+ | vector< | ||
+ | |||
+ | |||
+ | // Apply the Hough Transform to find the circles (use the gray scale image as input) | ||
+ | // | ||
+ | // 4: inverse ratio of resolution . 5 minimum distance between detected centers. 6: upper threshold for the internal canny edge detector | ||
+ | //7: threshold for center detection . 8: Minimum radius to be detected (0=unknown) . 9: maximum radius to be detected | ||
+ | |||
+ | HoughCircles(grayimage, | ||
+ | |||
+ | // Draw the circles detected | ||
+ | for (size_t i = 0; i < circles.size(); | ||
+ | { | ||
+ | |||
+ | //Get the informations from the circles vector generated by the function HoughCircles. | ||
+ | //Column is circles[i][0] | ||
+ | Point center(cvRound(circles[i][0]), | ||
+ | int radius = cvRound(circles[i][2]); | ||
+ | |||
+ | //Store these values into variables to be converted into string and displayed on the image | ||
+ | Rvalue = radius; | ||
+ | Xvalue = cvRound(circles[i][0]); | ||
+ | Yvalue = cvRound(circles[i][1]); | ||
+ | |||
+ | // | ||
+ | //Use the circle function to draw the center of the detected circle | ||
+ | //Use the center coordinate and a radius of 3 to just draw a point on the center. | ||
+ | circle(coloredimage, | ||
+ | |||
+ | // | ||
+ | //Use the circle function to draw the detected circle on the image | ||
+ | //Use the center coordinate and the radius coordinate detected by the HoughCircles function | ||
+ | circle(coloredimage, | ||
+ | |||
+ | // | ||
+ | radiusStr = intToString(Rvalue); | ||
+ | xcenterStr = intToString(Xvalue); | ||
+ | ycenterStr = intToString(Yvalue); | ||
+ | |||
+ | // | ||
+ | putText(coloredimage, | ||
+ | |||
+ | // | ||
+ | cout << " | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | // Show your results | ||
+ | namedWindow(" | ||
+ | imshow(" | ||
+ | |||
+ | |||
+ | key = waitKey(25); | ||
+ | |||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | The idea to change from an image to the webcam video is the same used on the Tutorial 1. We change the **" | ||
+ | \\ | ||
+ | \\ | ||
+ | Below is a video demonstrating the first program in real time. | ||
+ | {{youtube> | ||
+ | \\ | ||
+ | \\Below is a video demonstrating the second program in real time. | ||
+ | {{youtube> |
opencv_tutorials_t3.1465247127.txt.gz · Last modified: 2016/06/06 14:05 by joaomatos