Escolar Documentos
Profissional Documentos
Cultura Documentos
#include
#include
#include
"opencv2/imgproc/imgproc.hpp"
"opencv2/highgui/highgui.hpp"
<stdlib.h>
<stdio.h>
threshold_value = 0;
threshold_type = 0;
const max_value = 255;
const max_type = 2;
const max_BINARY_value = 255;
thresh = 0;
max_thresh = 255;
rng(12345);
largest_area=0, largest_area_index=0,largest_contour_index=0;
largest_area1=0;
largest_area2=10000000;
largest_area3=0;
bounding_rect, bounding_rect1;
size(800,481);
buffer [200]; // Para impresion de caracteres en ventana
bufferAmayor [200];
bufferAmenor [200];
bufferc1 [20];
bufferc2 [20];
bufferc3 [20];
buffersuma [20];
int lineType = 8;
Size textsize = getTextSize("OpenCV forever!", CV_FONT_HERSHEY_COMPLEX, 3, 5, 0)
;
/*//Variables para contador
int c1=0;
int c2=0;
int c3=0;*/
/*//Variables de Area
double Amayor=0;
double Amenor=0;
double Amedio=0;*/
//Colores
#define Azul CV_RGB(0,0,255)
#define Verde CV_RGB(0,255,0)
#define Rojo CV_RGB(255,0,0)
#define Amarillo CV_RGB(255,255,0)
#define Blanco CV_RGB(255,255,255)
#define Negro CV_RGB(0,0,0)
/**
* @function main
*/
int main( int argc, char** argv )
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
for(;;)
{
//Variables para contador
int c1=0;
int c2=0;
int c3=0;
//Variables de Area
double Amayor=0;
double Amenor=0;
double Amedio=0;
//Mat frame;
cap >> frame; // get a new frame from camera
/// Convert image to gray and blur it
cvtColor( frame, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
/// Create a window to display results
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
/// Create Trackbar to choose type of Threshold
createTrackbar( trackbar_type,
window_name, &threshold_type,
max_type);
createTrackbar( "Canny:", window_name,
&thresh, max_thresh);
//Deteccion de contornos por CANNY
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Detect edges using canny
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_TRE
E, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Draw contours
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 0), rng.uniform(2
55,255), rng.uniform(0,0) );
drawContours( drawing, contours, i, color, 1, 20, hiera
rchy, 0, Point() );
}
//Calculos de Areas
/// Get the moments
vector<Moments> mu(contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ mu[i] = moments( contours[i], false ); }
/// Obtiene el centro de masa:
vector<Point2f> mc( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu
[i].m00 ); }
/// Dibuja los Contornos y calculo de Areas
Mat drawing1 = Mat::zeros( canny_output.size(), CV_8UC3
);
for( int i = 0; i< contours.size(); i++ )
{
//Dibuja contornos
Scalar color = Scalar( rng.uniform(0, 255), rng.uni
form(0,255), rng.uniform(0,255) );
drawContours( drawing1, contours, i, color, 2, 8, h
ierarchy, 0, Point() );
circle( drawing1, mc[i], 4, color, -1, 8, 0 );
/*//Calculo Area mas Grande
double a=contourArea( contours[i],false); // F
ind the area of contour
//double b=contourArea( contours[i],false);
if(a>largest_area1)
{
largest_area1=a;
sprintf(bufferAmayor,"Amayor = %.2f",a);
Amayor=a;
}*/
//Calcular Area de cada contorno
//printf(" * Contour[%d] - Area (M_00) = %.2f - Are
a OpenCV: %.2f - Length: %.2f \n", i, mu[i].m00, contourArea(contours[i]), arcLe
//==========================================
if(threshold_type==0)
{
imshow( window_name, frame);
}
if(threshold_type==1)
{
imshow( window_name, drawing );
}
//========
if(waitKey(30) >= 0) break;
}
return 0;
}