/** * @brief Calcula el histograma asociado a un objeto (región) y lo normaliza en el rango [0,1] * @param img La región de la imágen a la que se le calculará su histograma * @param region Las coordenadas x e y del rectángulo que delimita la región del objeto * @param histo Donde se almacenará el histograma creado */ void calc_histogram( IplImage *img, CvRect *region, histogram *histo ) { float sum = 0, inv_sum; int Forigen, Corigen, Ffinal, Cfinal; memset( histo->histo, 0, histo->n * sizeof(float) ); Forigen = region[0].y < 0 ? 0:region[0].y; Corigen = region[0].x < 0 ? 0:region[0].x; Ffinal = Forigen + region[0].height > img->height ? img->height:Forigen + region[0].height; Cfinal = Corigen + region[0].width > img->width ? img->width:Corigen + region[0].width; // Incrementa el apropiado bin del histograma para cada píxel de la región for ( int r = Forigen; r < Ffinal; ++r ) { float *ptr = (float*) ( img->imageData + img->widthStep*r ); for( int c = Corigen; c < Cfinal; ++c ) { int desp = 3*c; int bin = histo_bin( ptr[desp], ptr[desp + 1], ptr[desp + 2] ); histo->histo[bin]++; } } // Se calcula la inversa de la sumatoria y se multiplica cada bin por ella for( int i = 0; i < histo->n; ++i ) sum += histo->histo[i]; inv_sum = 1.0f / sum; for( int i = 0; i < histo->n; ++i ) histo->histo[i] *= inv_sum; }
histogram* calc_histogram_a( PointCloudT::Ptr& cloud ) { histogram* histo; float* hist; int bin; float h = 0, s = 0, v = 0; histo = (histogram*)malloc( sizeof(histogram) ); histo->n = NH*NS + NV; hist = histo->histo; memset( hist, 0, histo->n * sizeof(float) ); for( int i = 0; i < cloud->points.size(); i++ ) { rgb2hsv( (int)cloud->points[i].r, (int)cloud->points[i].g, (int)cloud->points[i].b, h, s, v ); bin = histo_bin( h, s, v ); hist[bin] += 1; } //normalize_histogram( histo ); return histo; }