otsu: Use double for weighted sums

My phone camera produces 12M-pixel images.
They can easily overflow an int.
This commit is contained in:
YAMAMOTO Takashi 2021-04-08 13:24:32 +09:00
parent 9c0f555acb
commit de68db7b69

View file

@ -190,14 +190,14 @@ static uint8_t otsu(const struct quirc *q)
} }
// Calculate weighted sum of histogram values // Calculate weighted sum of histogram values
unsigned int sum = 0; double sum = 0;
unsigned int i = 0; unsigned int i = 0;
for (i = 0; i <= UINT8_MAX; ++i) { for (i = 0; i <= UINT8_MAX; ++i) {
sum += i * histogram[i]; sum += i * histogram[i];
} }
// Compute threshold // Compute threshold
int sumB = 0; double sumB = 0;
int q1 = 0; int q1 = 0;
double max = 0; double max = 0;
uint8_t threshold = 0; uint8_t threshold = 0;
@ -213,8 +213,8 @@ static uint8_t otsu(const struct quirc *q)
break; break;
sumB += i * histogram[i]; sumB += i * histogram[i];
const double m1 = (double)sumB / q1; const double m1 = sumB / q1;
const double m2 = ((double)sum - sumB) / q2; const double m2 = (sum - sumB) / q2;
const double m1m2 = m1 - m2; const double m1m2 = m1 - m2;
const double variance = m1m2 * m1m2 * q1 * q2; const double variance = m1m2 * m1m2 * q1 * q2;
if (variance >= max) { if (variance >= max) {