48 #include <visp3/core/vpHistogram.h>
49 #include <visp3/core/vpImageConvert.h>
50 #include <visp3/core/vpDisplay.h>
78 memcpy(histogram, h.histogram, size *
sizeof(
unsigned));
89 : histogram(NULL), size(256)
101 if (histogram != NULL) {
126 memcpy(histogram, h.histogram, size *
sizeof(
unsigned));
137 vpHistogram::init(
unsigned size_)
139 if (histogram != NULL) {
145 histogram =
new unsigned [size];
147 memset(histogram, 0, size *
sizeof(
unsigned));
163 if (histogram != NULL) {
168 size = nbins > 256 ? 256 : (nbins > 0 ? nbins : 256);
169 if(nbins > 256 || nbins == 0) {
170 std::cerr <<
"nbins=" << nbins <<
" , nbins should be between ]0 ; 256] ; use by default nbins=256" << std::endl;
172 histogram =
new unsigned int[size];
175 memset(histogram, 0, size *
sizeof(
unsigned));
177 unsigned int lut[256];
178 for(
unsigned int i = 0; i < 256; i++) {
179 lut[i] = (
unsigned int) (i * size / 256.0);
183 unsigned char *ptrStart = (
unsigned char*) I.
bitmap;
184 unsigned char *ptrEnd = ptrStart + size_;
185 unsigned char *ptrCurrent = ptrStart;
187 while(ptrCurrent != ptrEnd) {
188 histogram[ lut[ *ptrCurrent ] ] ++;
203 const unsigned int maxValue_) {
206 if(width < 36 || height < 36) {
207 std::cerr <<
"Image I must have at least width >= 36 && height >= 36 !" << std::endl;
211 unsigned int maxValue = maxValue_;
213 for(
unsigned int i = 0; i < size; i++) {
214 if(histogram[i] > maxValue) {
215 maxValue = histogram[i];
222 "Cannot display histogram; max value=0"));
225 unsigned int max_height = height-12;
226 double ratio_height = max_height / (double) maxValue;
227 double ratio_width = (width-1) / (
double) (size-1.0);
229 for(
unsigned int i = 1; i < size; i++) {
230 unsigned int value1 = histogram[i-1];
231 unsigned int value2 = histogram[i];
233 vpImagePoint startPt((height-1)-(value1*ratio_height), (i-1)*ratio_width);
234 vpImagePoint endPt((height-1)-(value2*ratio_height), (i*ratio_width));
262 if (histogram == NULL) {
265 "Histogram array not initialised")) ;
271 int hsize = (int)fsize / 2;
275 for (
unsigned i=0; i < size; i ++) {
278 for (
int j=-hsize; j <= hsize; j ++) {
280 if ( (i + (
unsigned int)j) < size ) {
281 sum += h.histogram[i + (
unsigned int)j];
285 histogram[i] = sum / nb;
306 if (histogram == NULL) {
309 "Histogram array not initialised")) ;
321 unsigned sum_level = 0;
325 for (
unsigned i = 0; i < size-1; i++) {
326 next_slope = (int)histogram[i+1] - (
int)histogram[i];
334 if ((prev_slope > 0) && (next_slope == 0) ) {
342 if ( (prev_slope > 0) && (next_slope < 0) ) {
346 unsigned int level = sum_level / cpt;
347 p.
set((
unsigned char)level, histogram[level]);
355 prev_slope = next_slope;
359 if (prev_slope > 0) {
360 p.
set((
unsigned char)size-1u, histogram[size-1]);
390 std::list<vpHistogramPeak> peaks;
403 peak1 = peaks.front();
410 peak1 = peaks.front();
411 for(std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++ it)
451 unsigned index_highest_peak;
452 unsigned index_second_peak;
455 unsigned int maxprof;
457 unsigned int sumindmini;
459 unsigned int nbpeaks;
465 peak =
new unsigned char [size];
470 for (
unsigned i = 0; i < size-1; i++) {
471 next_slope = (int)histogram[i+1] - (
int)histogram[i];
475 if ( (prev_slope > 0) && (next_slope < 0) )
476 peak[nbpeaks ++] = (
unsigned char)i;
478 prev_slope = next_slope;
481 peak[nbpeaks ++] = (
unsigned char)(size-1);
488 index_highest_peak = 0;
489 for (
unsigned int i=0; i < nbpeaks; i++) {
490 if (histogram[ peak[i] ] > histogram[ peak[index_highest_peak] ]) {
491 index_highest_peak = i;
500 index_second_peak=index_highest_peak;
503 for (
unsigned i = 0; i < index_highest_peak; i++) {
504 if (peak[index_highest_peak] - peak[i] > dist) {
506 for (
int j=peak[i]; j <= peak[index_highest_peak]-dist; j++)
507 if((histogram[peak[i]] - histogram[j]) > prof)
508 prof = histogram[peak[i]] - histogram[j];
510 if (prof > maxprof) {
512 index_second_peak = i;
518 for (
unsigned i = index_highest_peak+1; i < nbpeaks; i++) {
519 if (peak[i] - peak[index_highest_peak] > dist) {
521 for (
int j=peak[index_highest_peak]+dist; j <= peak[i]; j++)
522 if((histogram[peak[i]] - histogram[j]) > prof)
523 prof = histogram[peak[i]] - histogram[j];
525 if (prof > maxprof) {
527 index_second_peak = i;
536 if (peak[index_highest_peak] < peak[index_second_peak])
538 peakr.
set(peak[index_second_peak], histogram[ peak[index_second_peak] ]);
539 peakl.
set(peak[index_highest_peak], histogram[ peak[index_highest_peak] ]);
543 peakl.
set(peak[index_second_peak], histogram[ peak[index_second_peak] ]);
544 peakr.
set(peak[index_highest_peak], histogram[ peak[index_highest_peak]]);
547 if (peakl == peakr) {
559 if (histogram[i] < mini) {
565 if (histogram[i] == mini) {
583 mini = sumindmini/nbmini;
584 valey.
set((
unsigned char)mini, histogram[mini]);
605 if (histogram == NULL) {
608 "Histogram array not initialised")) ;
620 unsigned sum_level = 0;
624 for (
unsigned i = 0; i < size-1; i++) {
625 next_slope = (int)histogram[i+1] - (
int)histogram[i];
627 if ((prev_slope < 0) && (next_slope == 0) ) {
634 if ( (prev_slope < 0) && (next_slope > 0) ) {
638 unsigned int level = sum_level / cpt;
639 p.
set((
unsigned char)level, histogram[level]);
647 prev_slope = next_slope;
651 if (prev_slope < 0) {
652 p.
set((
unsigned char)size-1u, histogram[size-1]);
694 unsigned int sumindmini;
701 if (histogram[i] < mini) {
707 if (histogram[i] == mini) {
720 unsigned int minipos = sumindmini/nbmini;
722 valey.
set((
unsigned char)minipos, histogram[minipos]);
750 unsigned int ret = 0x11;
752 unsigned int sumindmini;
755 std::list<vpHistogramPeak> peaks;
763 valeyr.
set((
unsigned char)(size-1), 0);
786 std::list<vpHistogramPeak>::const_iterator it;
788 for (it = peaks.begin(); it != peaks.end(); ++ it)
805 std::list<vpHistogramPeak>::const_iterator lit;
806 for(lit = peaks.begin(); lit != it; ++ lit)
808 if (abs((*lit).getLevel() - peak.
getLevel()) > dist) {
823 if (histogram[i] < mini) {
829 if (histogram[i] == mini) {
839 unsigned int minipos = sumindmini/nbmini;
840 valeyl.
set((
unsigned char)minipos, histogram[minipos]);
851 std::list<vpHistogramPeak>::const_iterator it;
853 for (it = peaks.begin(); it != peaks.end(); ++ it)
864 std::list<vpHistogramPeak>::const_iterator rit;
865 for(rit = it; rit != peaks.end(); ++ rit)
867 if (abs((*rit).getLevel() - peak.
getLevel()) > dist) {
875 peakr.
set((
unsigned char)(size-1),0);
881 for (
unsigned i=(
unsigned int)peak.
getLevel()+1; i <= (
unsigned int)peakr.
getLevel(); i++) {
882 if (histogram[i] < mini) {
888 if (histogram[i] == mini) {
894 valeyr.
set((
unsigned char)(size-1), 0);
898 unsigned int minipos = sumindmini/nbmini;
899 valeyr.
set((
unsigned char)minipos, histogram[minipos]);
918 if ( peaks.empty() ) {
922 peaks.sort(compare_vpHistogramPeak);
924 return (
unsigned int)peaks.size();
942 return ( this->
write(filename.c_str()) );
962 FILE *fd = fopen(filename,
"w");
965 for (
unsigned i=0; i < size; i ++)
966 fprintf(fd,
"%d %d\n", i, histogram[i]);
unsigned getPeaks(std::list< vpHistogramPeak > &peaks)
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::white, const unsigned int thickness=2, const unsigned int maxValue_=0)
unsigned int getWidth() const
unsigned getValue() const
Type * bitmap
points toward the bitmap
vpHistogram & operator=(const vpHistogram &h)
Class to define colors available for display functionnalities.
error that can be emited by ViSP classes.
Class to compute a gray level image histogram.
Error that can be emited by the vpImage class and its derivates.
Declaration of the peak (maximum value) in a gray level image histogram.
void smooth(const unsigned int fsize=3)
void set(unsigned char lvl, unsigned val)
void set(unsigned char lvl, unsigned val)
unsigned sort(std::list< vpHistogramPeak > &peaks)
unsigned char getLevel() const
bool write(const std::string &filename)
unsigned int getHeight() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
Declaration of the valey (minimum value) in a gray level image histogram.
unsigned getValey(std::list< vpHistogramValey > &valey)
void calculate(const vpImage< unsigned char > &I, const unsigned int nbins=256)