bool ZStackBinarizer::binarize(Stack *stack) { if (stack == NULL) { return false; } int threshold = m_threshold; int *hist = NULL; Stack *refStack = C_Stack::clone(m_reference); if (refStack == NULL) { refStack = stack; } int low = m_lowerBound; int high = m_upperBound; switch (m_method) { case BM_RC_THRESHOLD: case BM_STABLE_POINT: case BM_TRIANGLE: hist = Stack_Hist(refStack); break; case BM_LOCMAX: hist = computeLocmaxHist(refStack); break; default: break; } if (hist != NULL) { if (m_lowerBound < 0) { low = Int_Histogram_Min(hist); } if (m_upperBound < 0) { high = Int_Histogram_Max(hist); } } if (low == high && hist != NULL) { BINARIZE_CLEAN return false; }
int* Int_Histogram_Equalize(const int *hist, int min, int max) { /* <map> allocated */ int *map = iarray_malloc(Int_Histogram_Length(hist) + 2); map[1] = Int_Histogram_Min(hist); int i; map[0] = Int_Histogram_Length(hist); const int *hist_array = Int_Histogram_Const_Array(hist); int cumsum = 0; int sum = iarray_sum(hist_array, map[0]); for (i = 0; i < map[0]; i++) { cumsum += hist_array[i]; map[i + 2] = min + (max - min) * (cumsum - hist_array[0]) / (sum - hist_array[0]); } /* <map> returned */ return map; }
int ZIntHistogram::getMinValue() const { return Int_Histogram_Min(m_hist); }
int* Int_Histogram_Equal_Info_Map(const int *hist, int nbin, int *map) { int length = Int_Histogram_Length(hist); int min = Int_Histogram_Min(hist); if (map == NULL) { map = iarray_malloc(length); } const int *hist_array = Int_Histogram_Const_Array(hist); /* alloc <info> */ double *info = darray_malloc(length); int i; for (i = 0; i < length; i++) { if (hist_array[i] == 0) { info[i] = 0; } else { if (min +i == 0) { info[i] = log(hist_array[i]); } else info[i] = log(hist_array[i]) + log(min + i); } } double total_number = darray_sum(info, length); int current_index = 0; double threshold = total_number / nbin; double count = info[0]; map[0] = current_index; for (i = 1; i < length; i++) { if (nbin - map[i-1] - 1 >= length - i) { int j; current_index = map[i-1] + 1; for (j = i; j < length; j++) { map[j] = current_index++; } break; } count += info[i]; if (count <= threshold) { map[i] = current_index; } else { total_number -= count; if (count - threshold >= threshold + info[i] - count) { total_number += info[i]; count = info[i]; map[i] = current_index + 1; } else { count = 0; map[i] = current_index; } current_index++; /* this case should not happen */ if (current_index > 255) { current_index = 255; } /* update threshold */ threshold = total_number / (nbin - current_index); } } /* free <info> */ free(info); return map; }