51 #include <visp/vpHistogram.h>
79 memcpy(histogram, h.histogram, size *
sizeof(
unsigned));
90 : histogram(NULL), size(256)
102 if (histogram != NULL) {
127 memcpy(histogram, h.histogram, size *
sizeof(
unsigned));
138 vpHistogram::init(
unsigned size_)
140 if (histogram != NULL) {
146 histogram =
new unsigned [size];
148 memset(histogram, 0, size *
sizeof(
unsigned));
163 memset(histogram, 0, size *
sizeof(
unsigned));
165 for (
unsigned i=0; i < I.
getHeight(); i ++) {
166 for (
unsigned j=0; j < I.
getWidth(); j ++) {
167 histogram[ I[i][j] ] ++;
195 if (histogram == NULL) {
198 "Histogram array not initialised")) ;
204 int hsize = (int)fsize / 2;
208 for (
unsigned i=0; i < size; i ++) {
211 for (
int j=-hsize; j <= hsize; j ++) {
213 if ( (i + (
unsigned int)j) < size ) {
214 sum += h.histogram[i + (
unsigned int)j];
218 histogram[i] = sum / nb;
239 if (histogram == NULL) {
242 "Histogram array not initialised")) ;
254 unsigned sum_level = 0;
258 for (
unsigned i = 0; i < size-1; i++) {
259 next_slope = (int)histogram[i+1] - (
int)histogram[i];
267 if ((prev_slope > 0) && (next_slope == 0) ) {
275 if ( (prev_slope > 0) && (next_slope < 0) ) {
279 unsigned int level = sum_level / cpt;
280 p.
set((
unsigned char)level, histogram[level]);
288 prev_slope = next_slope;
292 if (prev_slope > 0) {
293 p.
set((
unsigned char)size-1u, histogram[size-1]);
323 std::list<vpHistogramPeak> peaks;
336 peak1 = peaks.front();
343 peak1 = peaks.front();
344 for(std::list<vpHistogramPeak>::const_iterator it = peaks.begin(); it != peaks.end(); ++ it)
384 unsigned index_highest_peak;
385 unsigned index_second_peak;
388 unsigned int maxprof;
390 unsigned int sumindmini;
392 unsigned int nbpeaks;
398 peak =
new unsigned char [size];
403 for (
unsigned i = 0; i < size-1; i++) {
404 next_slope = (int)histogram[i+1] - (
int)histogram[i];
408 if ( (prev_slope > 0) && (next_slope < 0) )
409 peak[nbpeaks ++] = (
unsigned char)i;
411 prev_slope = next_slope;
414 peak[nbpeaks ++] = (
unsigned char)(size-1);
421 index_highest_peak = 0;
422 for (
unsigned int i=0; i < nbpeaks; i++) {
423 if (histogram[ peak[i] ] > histogram[ peak[index_highest_peak] ]) {
424 index_highest_peak = i;
433 index_second_peak=index_highest_peak;
436 for (
unsigned i = 0; i < index_highest_peak; i++) {
437 if (peak[index_highest_peak] - peak[i] > dist) {
439 for (
int j=peak[i]; j <= peak[index_highest_peak]-dist; j++)
440 if((histogram[peak[i]] - histogram[j]) > prof)
441 prof = histogram[peak[i]] - histogram[j];
443 if (prof > maxprof) {
445 index_second_peak = i;
451 for (
unsigned i = index_highest_peak+1; i < nbpeaks; i++) {
452 if (peak[i] - peak[index_highest_peak] > dist) {
454 for (
int j=peak[index_highest_peak]+dist; j <= peak[i]; j++)
455 if((histogram[peak[i]] - histogram[j]) > prof)
456 prof = histogram[peak[i]] - histogram[j];
458 if (prof > maxprof) {
460 index_second_peak = i;
469 if (peak[index_highest_peak] < peak[index_second_peak])
471 peakr.
set(peak[index_second_peak], histogram[ peak[index_second_peak] ]);
472 peakl.
set(peak[index_highest_peak], histogram[ peak[index_highest_peak] ]);
476 peakl.
set(peak[index_second_peak], histogram[ peak[index_second_peak] ]);
477 peakr.
set(peak[index_highest_peak], histogram[ peak[index_highest_peak]]);
480 if (peakl == peakr) {
492 if (histogram[i] < mini) {
498 if (histogram[i] == mini) {
516 mini = sumindmini/nbmini;
517 valey.
set((
unsigned char)mini, histogram[mini]);
538 if (histogram == NULL) {
541 "Histogram array not initialised")) ;
553 unsigned sum_level = 0;
557 for (
unsigned i = 0; i < size-1; i++) {
558 next_slope = (int)histogram[i+1] - (
int)histogram[i];
560 if ((prev_slope < 0) && (next_slope == 0) ) {
567 if ( (prev_slope < 0) && (next_slope > 0) ) {
571 unsigned int level = sum_level / cpt;
572 p.
set((
unsigned char)level, histogram[level]);
580 prev_slope = next_slope;
584 if (prev_slope < 0) {
585 p.
set((
unsigned char)size-1u, histogram[size-1]);
627 unsigned int sumindmini;
634 if (histogram[i] < mini) {
640 if (histogram[i] == mini) {
653 unsigned int minipos = sumindmini/nbmini;
655 valey.
set((
unsigned char)minipos, histogram[minipos]);
683 unsigned int ret = 0x11;
685 unsigned int sumindmini;
688 std::list<vpHistogramPeak> peaks;
696 valeyr.
set((
unsigned char)(size-1), 0);
719 std::list<vpHistogramPeak>::const_iterator it;
721 for (it = peaks.begin(); it != peaks.end(); ++ it)
738 std::list<vpHistogramPeak>::const_iterator lit;
739 for(lit = peaks.begin(); lit != it; ++ lit)
741 if (abs((*lit).getLevel() - peak.
getLevel()) > dist) {
756 if (histogram[i] < mini) {
762 if (histogram[i] == mini) {
772 unsigned int minipos = sumindmini/nbmini;
773 valeyl.
set((
unsigned char)minipos, histogram[minipos]);
784 std::list<vpHistogramPeak>::const_iterator it;
786 for (it = peaks.begin(); it != peaks.end(); ++ it)
797 std::list<vpHistogramPeak>::const_iterator rit;
798 for(rit = it; rit != peaks.end(); ++ rit)
800 if (abs((*rit).getLevel() - peak.
getLevel()) > dist) {
808 peakr.
set((
unsigned char)(size-1),0);
814 for (
unsigned i=(
unsigned int)peak.
getLevel()+1; i <= (
unsigned int)peakr.
getLevel(); i++) {
815 if (histogram[i] < mini) {
821 if (histogram[i] == mini) {
827 valeyr.
set((
unsigned char)(size-1), 0);
831 unsigned int minipos = sumindmini/nbmini;
832 valeyr.
set((
unsigned char)minipos, histogram[minipos]);
851 if ( peaks.empty() ) {
855 peaks.sort(compare_vpHistogramPeak);
857 return (
unsigned int)peaks.size();
875 return ( this->
write(filename.c_str()) );
895 FILE *fd = fopen(filename,
"w");
898 for (
unsigned i=0; i < size; i ++)
899 fprintf(fd,
"%d %d\n", i, histogram[i]);
905 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
924 if (histogram == NULL) {
927 "Histogram array not initialised")) ;
935 if ( ! peaks.
empty() )
942 unsigned sum_level = 0;
946 for (
unsigned i = 0; i < size-1; i++) {
947 next_slope = (int)histogram[i+1] - (
int)histogram[i];
955 if ((prev_slope > 0) && (next_slope == 0) ) {
963 if ( (prev_slope > 0) && (next_slope < 0) ) {
967 unsigned int level = sum_level / cpt;
968 p.
set((
unsigned char)level, histogram[level]);
976 prev_slope = next_slope;
980 if (prev_slope > 0) {
981 p.
set((
unsigned char)size-1u, histogram[size-1]);
1005 if (histogram == NULL) {
1008 "Histogram array not initialised")) ;
1016 if ( ! valey.
empty() )
1023 unsigned sum_level = 0;
1027 for (
unsigned i = 0; i < size-1; i++) {
1028 next_slope = (int)histogram[i+1] - (
int)histogram[i];
1030 if ((prev_slope < 0) && (next_slope == 0) ) {
1037 if ( (prev_slope < 0) && (next_slope > 0) ) {
1041 unsigned int level = sum_level / cpt;
1042 p.
set((
unsigned char)level, histogram[level]);
1050 prev_slope = next_slope;
1054 if (prev_slope < 0) {
1055 p.
set((
unsigned char)size-1u, histogram[size-1]);
1079 unsigned nbpeaks = 0;
1081 if ( peaks.
empty() ) {
1090 _p = _peaks.
value();
1097 _p = _peaks.
value();
1120 #endif // VISP_BUILD_DEPRECATED_FUNCTIONS
void calculate(const vpImage< unsigned char > &I)
unsigned getPeaks(std::list< vpHistogramPeak > &peaks)
bool outside(void) const
Test if the current element is outside the list (on the virtual element)
unsigned int getWidth() const
Provide simple list management.
unsigned getValue() const
vpHistogram & operator=(const vpHistogram &h)
Class to compute a gray level image histogram.
Error that can be emited by the vpImage class and its derivates.
void kill()
Destroy the list.
Declaration of the peak (maximum value) in a gray level image histogram.
void next(void)
position the current element on the next one
void addLeft(const type &el)
add a new element in the list, at the left of the current one
void smooth(const unsigned int fsize=3)
void set(unsigned char lvl, unsigned val)
void front(void)
Position the current element on the first element of the list.
type & value(void)
return the value of the current element
void addRight(const type &el)
add a new element in the list, at the right of the current one
void set(unsigned char lvl, unsigned val)
unsigned sort(std::list< vpHistogramPeak > &peaks)
unsigned char getLevel() const
bool nextOutside(void) const
Test if the next element is outside the list (ie if the current element is the last one) ...
bool write(const std::string &filename)
unsigned int getHeight() const
Declaration of the valey (minimum value) in a gray level image histogram.
unsigned getValey(std::list< vpHistogramValey > &valey)
bool empty(void) const
Test if the list is empty.