You are on page 1of 2

#include<opencv2\opencv.

hpp>
#include<opencv2\highgui\highgui.hpp>
#include <iostream>

void main()
{
int hupper = 50;
int hlower = 25;
int sat = 90;
int brillo = 90;
int M00 = 0, M10 = 0, M01 = 0;
float cdgx, cdgy;
IplImage *frame, *hsv_image, *mono_image;
int height, width, step, channels;
int step_mono, channels_mono;
uchar *data_hsv, *data_mono;
cvNamedWindow("imagen1", 0);
cvNamedWindow("imagen2",0);
frame = cvLoadImage("fruits2.jpg");
cvShowImage("imagen1", frame);

hsv_image = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);


mono_image = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
cvZero(mono_image);
cvCvtColor(frame, hsv_image, CV_BGR2HSV);
height = hsv_image->height;
width = hsv_image->width;
step = hsv_image->widthStep / sizeof(uchar);
channels = hsv_image->nChannels;

step_mono = mono_image->widthStep / sizeof(uchar);


channels_mono = mono_image->nChannels;

data_hsv = (uchar*)hsv_image->imageData;
data_mono = (uchar*)mono_image->imageData;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
if ((data_hsv[i * step + j * channels+0] >= hlower) &&
(data_hsv[i * step + j * channels+0] < hupper))
if (data_hsv[i * step + j * channels + 1] >= sat)
if (data_hsv[i * step + j * channels + 2] >= brillo)
data_mono[i*step_mono + j*channels_mono] = 255;
cvDilate(mono_image, mono_image, 0, 3);
cvErode(mono_image, mono_image, 0, 4);
//////////
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
if (data_mono[i * step_mono + j * channels_mono] == 255)
{
M00 = M00 + 1;
M10 = M10 + j;
M01 = M01 + i;
}
cdgx = M10 / M00;
cdgy = M01 / M00;
printf("%.2f %.f\n", cdgx, cdgy);
////
cvShowImage("imagen2", mono_image);
cvWaitKey(0);
cvReleaseImage(&mono_image);
cvWaitKey(0);
cvReleaseImage(&frame);
}

You might also like