User Tools

Site Tools


opencv_tutorials_t3

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_t3 [2016/06/06 16:14] joaomatosopencv_tutorials_t3 [2017/05/13 17:20] (current) – [Detecting Circles on Webcam] acater
Line 4: Line 4:
  
  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.
 +
 +{{::detecting_circle_image.rar|Detecting Circles on Image}}
 +
 +{{::detecting_circles_on_webcam.rar|Detecting Circles on Webcam}}
 +
 +
  
  
 ---- ----
  
-===== Code =====+===== Detecting Circles in Image ===== 
 + 
 +{{ ::circledetec.jpg?direct |}}
  
 <Code:c++ linenums:1> <Code:c++ linenums:1>
Line 107: Line 115:
  
  
- cout << "radius:" << radiusStr << "    Xc:" << xcenterStr << "    Yc:" << ycenterStr;+ cout << "radius:" << radiusStr << "    Column:" << xcenterStr << "    Row:" << ycenterStr;
 } }
  
Line 247: Line 255:
 </Code> </Code>
  
-To display the Center value on the image we use the **"putText"** function. It will put the text on the first argument image. The text to be displayed is defined on the second argument. The third argument is the point where it will be displayed. To be more visible we can draw on the X center column but on the Y center -20 pixels row ( so the center point drawn be us can be more visible ). We also display these values on the CMD window using the **"cout <<"** command.+To display the Center value on the image we use the **"putText"** function. It will put the text on the first argument image. The text to be displayed is defined on the second argument. The third argument is the point where it will be displayed. To be more visible we can draw on the X center column but on the Y center -20 pixels row ( so the center point drawn be us can be more visible ). Subtracting pixels from the column will make the text to move upward. We also display these values on the CMD window using the **"cout <<"** command.
  
  
Line 264: 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 =====
 +
 +{{ ::circle_webcam.jpg?direct |}}
 +
 +
 +<Code:c++ linenums:1>
 +
 +#include <sstream>
 +#include <string>
 +#include <iostream>
 +#include <opencv/highgui.h>
 +#include <opencv/cv.h>
 +#include <opencv2/core/core.hpp>
 +#include <opencv2/highgui/highgui.hpp>
 +#include <opencv2/imgproc/imgproc.hpp>
 +
 +using namespace cv;
 +using namespace std;
 +
 +
 +String intToString(int number)
 +{
 +
 +
 + std::stringstream ss;
 + 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;
 +
 + //Variables to store the webcam video , and a converted version of the video
 + 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("Failed to open the webcam");
 + }
 +
 + //Set Capture device properties.
 +
 + capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
 + capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
 +
 +
 + //Loop will stop if "q" is pressed in the keyboard
 + while (key != 'q')
 + {
 +
 + //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, grayimage, CV_BGR2GRAY);
 +
 + // Apply blur to Reduce the noise so we avoid false circle detection
 + GaussianBlur(grayimage, grayimage, Size(9, 9), 2, 2);
 +
 + //create a vector to store the center value ( x and y coordinates ) and the radius of each detected circle
 + vector<Vec3f> circles;
 +
 +
 + // Apply the Hough Transform to find the circles (use the gray scale image as input)
 + //Arguments: 1: input image( grayscaled ) . 2: vector to tore the circle parameters .3: detection method,
 + // 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, circles, CV_HOUGH_GRADIENT, 1, 30, 200, 50, 0, 0);
 +
 + // Draw the circles detected
 + for (size_t i = 0; i < circles.size(); i++)
 + {
 +
 + //Get the informations from the circles vector generated by the function HoughCircles. 
 + //Column is circles[i][0]  , Row is circles[i][1] , radius is circles[i][2] 
 + Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
 + 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, center, 3, Scalar(0, 255, 0), -1, 8, 0);
 +
 + //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, center, radius, Scalar(0, 0, 255), 3, 8, 0);
 +
 + //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, "(" + xcenterStr + "," + ycenterStr + ")", Point(Xvalue, Yvalue-20), 1, 1, Scalar(0, 255, 0), 2);
 +
 + //Display the values also on the cmd window
 + cout << "center : " << center << "\nradius : " << radius << endl;
 +
 + }
 +
 +
 + // Show your results
 + namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE);
 + imshow("Hough Circle Transform Demo", coloredimage);
 +
 +
 + key = waitKey(25);
 +
 + }
 + return 0;
 +}
 +
 +</Code>
 +
 +The idea to change from an image to the webcam video is the same used on the Tutorial 1. We change the **"imread"** for the **"VideoCapture**" and change the **"resize"** for the **"capture.set"**.  We use a loop until the q key is pressed to display the frames on the window.Our colored image is set using **"capture >> coloredimage;"** and all the following procedure to detect the circle is the same from the image code ( because we will analyze frame by frame ).
 +\\
 +\\
 +Below is a video demonstrating the first program in real time. 
 +{{youtube>doq8hqEDhY0?medium}}
 +\\
 +\\Below is a video demonstrating the second program in real time.
 +{{youtube>htymt5vIA3M?medium}}
opencv_tutorials_t3.1465254882.txt.gz · Last modified: 2016/06/06 16:14 by joaomatos