opencv_tutorials_t3
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
opencv_tutorials_t3 [2016/06/06 16:18] – joaomatos | opencv_tutorials_t3 [2017/05/13 17:20] (current) – [Detecting Circles on Webcam] acater | ||
---|---|---|---|
Line 6: | Line 6: | ||
{{:: | {{:: | ||
+ | |||
+ | {{:: | ||
+ | |||
- | {{ :: | ||
---- | ---- | ||
- | ===== Code ===== | + | ===== Detecting Circles in Image ===== |
+ | |||
+ | {{ :: | ||
< | < | ||
Line 268: | Line 272: | ||
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. | 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.1465255109.txt.gz · Last modified: 2016/06/06 16:18 by joaomatos