Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include "opencv2/imgproc/imgproc.

hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include"opencv2/imgproc/imgproc_c.h"
#include <opencv2/core/core.hpp>
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include<opencv2/highgui/highgui_c.h>
#include <opencv2/core/core.hpp>
#include<opencv2/core/core_c.h>
#include<opencv2/imgproc/imgproc.hpp>

using namespace std;


using namespace cv;

//VARIABLES GLOBALES
int p1 = 200;
int p2 = 100;
int p1_max = 255;
int p2_max = 255;
// Create a VideoCapture object and open the input file
// If the input is the web camera, pass 0 instead of the video file name
//VideoCapture cap("C:\\Users\\Alex\\Documents\\VISION\\videos\\moneda.mp4");
VideoCapture cap(0);

Mat src, src_gray, detected_edges, dst;


float area;

int main(){

// Check if camera opened successfully


if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}

namedWindow( "Video", CV_WINDOW_AUTOSIZE);


createTrackbar( "P 1:", "Video", &p1, p1_max);
createTrackbar( "P 2:", "Video", &p2, p2_max);

while(1){

// Capture frame-by-frame

cap >> src;


//resize(src, src, Size(640*1.2, 360*1.2), 0, 0, INTER_CUBIC);

// If the frame is empty, break immediately


if (src.empty())
break;

/// Convert it to gray


cvtColor( src, src_gray, CV_BGR2GRAY );

/// Reduce the noise so we avoid false circle detection


GaussianBlur( src_gray, src_gray, Size(3, 3), 2, 2 );

vector<Vec3f> circles;

/// Apply the Hough Transform to find the circles


HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, p1,
p2, 0, 0 );

/// Draw the circles detected


for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
// circle center
//circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
area = 3.1415*pow(radius,2);
putText(src, format("[%d]",i), center, FONT_HERSHEY_DUPLEX, 0.4,
Scalar(255,255,255), 1.6);
putText(src, format("A: %.2f",area), Point(cvRound(circles[i][0])-30,
cvRound(circles[i][1])+15), FONT_HERSHEY_DUPLEX, 0.4, Scalar(255,255,255), 1.6);
// circle outline
circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
}

/// Show your results


imshow( "Video", src );

// Press ESC on keyboard to exit


char c=(char)waitKey(25);
if(c==27)
break;

// When everything done, release the video capture object


cap.release();

// Closes all the frames


destroyAllWindows();

return 0;
}

You might also like