Está en la página 1de 6

#include

#include
#include
#include

"opencv2/imgproc/imgproc.hpp"
"opencv2/highgui/highgui.hpp"
<stdlib.h>
<stdio.h>

using namespace cv;


using namespace std;
/// Variables Globales
int
int
int
int
int

threshold_value = 0;
threshold_type = 0;
const max_value = 255;
const max_type = 2;
const max_BINARY_value = 255;

Mat src_gray, dst,frame,canny_output;


char* window_name = "Threshold Demo";
char* trackbar_type = "Type: \n 0: Source \n 1: Canny \n 2: Contornos \n 3: Area
s";
char* trackbar_value = "Value";
int
int
RNG
int
int
int
int
Rect
Size
char
char
char
char
char
char
char

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

ngth( contours[i], true ) );


sprintf(buffer,"A%d=%.1f",i,contourArea(contours[i]
));
///Imprime Texto de Area de cada contorno
if(threshold_type==2)
{
putText(drawing1, buffer, Point(mu[i].m1
0/mu[i].m00 , 100), /*FONT_HERSHEY_SIMPLEX*/FONT_HERSHEY_COMPLEX, 0.5, Scalar(0,
255, 0), 0.5);
putText(drawing1, bufferAmayor, Point(20
, 30), /*FONT_HERSHEY_SIMPLEX*/FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0), 0.5
);
}
}
//Calculo del area mas grande.
for( int i = 0; i< contours.size(); i++ )
{
double a=contourArea( contours[i],false);
if(a>Amayor)
{
Amayor=a;
sprintf(bufferAmayor,"Amayor = %.2f",a);
//Amayor=a;
}
}
//Calculo de area mas pequea.
for( int i = 0; i< contours.size(); i++ )
{
double b=contourArea( contours[i],false);
if(b<Amenor)
{
/*largest_area2=b;
sprintf(bufferAmenor,"Amenor = %.2f",b);
Amenor=b;*/
Amenor=b;
sprintf(bufferAmenor,"Amenor = %.2f",Amenor)
;
}
}
//Imprime area Menor
if(threshold_type==2)
{
putText(drawing1, bufferAmenor, Point(20, 50
), /*FONT_HERSHEY_SIMPLEX*/FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0), 0.5);
}
//Contador de Areas
for( int i = 0; i< contours.size(); i++ )
{

double ac=contourArea( contours[i],false);


if(ac<Amayor+50 && ac>Amayor-50)
{
c1=c1+1;
sprintf(bufferc1,"%d",c1);
}
if(ac<Amenor+50 && ac>Amenor-50)
{
c2=c2+1;
sprintf(bufferc2,"%d",c2);
}
if(ac<Amayor-50 && ac>Amenor+50)
{
c3=c3+1;
sprintf(bufferc3,"%d",c3);
}
}
double suma=(c1*0.50)+(c2*0.10)+(c3*0.25);
sprintf(buffersuma,"Usted tiene %.2f de dolar",suma);
/// Show in a window
if(threshold_type==2)
{
namedWindow( "Contours1", CV_WINDOW_AUTOSIZE );
putText(drawing1, "50 Centavos", Point(550, 400)
, /*FONT_HERSHEY_SIMPLEX*/FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0), 0.5);
putText(drawing1, bufferc1, Point(600, 450), /*F
ONT_HERSHEY_SIMPLEX*/FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0), 0.5);
putText(drawing1, "25 Centavos", Point(300, 400)
, /*FONT_HERSHEY_SIMPLEX*/FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0), 0.5);
putText(drawing1, bufferc3, Point(350, 450), /*F
ONT_HERSHEY_SIMPLEX*/FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0), 0.5);
putText(drawing1, "10 Centavos", Point(50, 400),
/*FONT_HERSHEY_SIMPLEX*/FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0), 0.5);
putText(drawing1, bufferc2, Point(60, 450), /*FO
NT_HERSHEY_SIMPLEX*/FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0), 0.5);
putText(drawing1, buffersuma, Point(400, 20), /*
FONT_HERSHEY_SIMPLEX*/FONT_HERSHEY_COMPLEX, 0.5, Blanco, 0.5);
imshow( "Contours1", drawing1 );
}

//==========================================

if(threshold_type==0)
{
imshow( window_name, frame);
}
if(threshold_type==1)
{
imshow( window_name, drawing );
}

//========
if(waitKey(30) >= 0) break;
}
return 0;
}