Use const struct quirc parameter in otsu() function and fix tabs
This commit is contained in:
parent
96a5a380fb
commit
766f79ce7c
1 changed files with 54 additions and 54 deletions
108
lib/identify.c
108
lib/identify.c
|
@ -174,55 +174,55 @@ static void flood_fill_seed(struct quirc *q, int x, int y, int from, int to,
|
||||||
* Adaptive thresholding
|
* Adaptive thresholding
|
||||||
*/
|
*/
|
||||||
|
|
||||||
uint8_t otsu(struct quirc *q)
|
uint8_t otsu(const struct quirc *q)
|
||||||
{
|
{
|
||||||
int numPixels = q->w * q->h;
|
int numPixels = q->w * q->h;
|
||||||
|
|
||||||
// Calculate histogram
|
// Calculate histogram
|
||||||
const int HISTOGRAM_SIZE = 256;
|
const int HISTOGRAM_SIZE = 256;
|
||||||
unsigned int histogram[HISTOGRAM_SIZE];
|
unsigned int histogram[HISTOGRAM_SIZE];
|
||||||
memset(histogram, 0, (HISTOGRAM_SIZE) * sizeof(unsigned int));
|
memset(histogram, 0, (HISTOGRAM_SIZE) * sizeof(unsigned int));
|
||||||
uint8_t* ptr = q->image;
|
uint8_t* ptr = q->image;
|
||||||
int length = numPixels;
|
int length = numPixels;
|
||||||
while (length--) {
|
while (length--) {
|
||||||
uint8_t value = *ptr++;
|
uint8_t value = *ptr++;
|
||||||
histogram[value]++;
|
histogram[value]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate weighted sum of histogram values
|
// Calculate weighted sum of histogram values
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (int i = 0; i < HISTOGRAM_SIZE; ++i) {
|
for (int i = 0; i < HISTOGRAM_SIZE; ++i) {
|
||||||
sum += i * histogram[i];
|
sum += i * histogram[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute threshold
|
// Compute threshold
|
||||||
int sumB = 0;
|
int sumB = 0;
|
||||||
int q1 = 0;
|
int q1 = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
uint8_t threshold = 0;
|
uint8_t threshold = 0;
|
||||||
for (int i = 0; i < HISTOGRAM_SIZE; ++i) {
|
for (int i = 0; i < HISTOGRAM_SIZE; ++i) {
|
||||||
// Weighted background
|
// Weighted background
|
||||||
q1 += histogram[i];
|
q1 += histogram[i];
|
||||||
if (q1 == 0)
|
if (q1 == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Weighted foreground
|
// Weighted foreground
|
||||||
const int q2 = numPixels - q1;
|
const int q2 = numPixels - q1;
|
||||||
if (q2 == 0)
|
if (q2 == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
sumB += i * histogram[i];
|
sumB += i * histogram[i];
|
||||||
const double m1 = (double)sumB / q1;
|
const double m1 = (double)sumB / q1;
|
||||||
const double m2 = ((double)sum - sumB) / q2;
|
const double m2 = ((double)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) {
|
||||||
threshold = i;
|
threshold = i;
|
||||||
max = variance;
|
max = variance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return threshold;
|
return threshold;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void area_count(void *user_data, int y, int left, int right)
|
static void area_count(void *user_data, int y, int left, int right)
|
||||||
|
@ -1071,17 +1071,17 @@ static void test_grouping(struct quirc *q, int i)
|
||||||
|
|
||||||
static void pixels_setup(struct quirc *q, uint8_t threshold)
|
static void pixels_setup(struct quirc *q, uint8_t threshold)
|
||||||
{
|
{
|
||||||
if (sizeof(*q->image) == sizeof(*q->pixels)) {
|
if (sizeof(*q->image) == sizeof(*q->pixels)) {
|
||||||
q->pixels = (quirc_pixel_t *)q->image;
|
q->pixels = (quirc_pixel_t *)q->image;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* source = q->image;
|
uint8_t* source = q->image;
|
||||||
quirc_pixel_t* dest = q->pixels;
|
quirc_pixel_t* dest = q->pixels;
|
||||||
int length = q->w * q->h;
|
int length = q->w * q->h;
|
||||||
while (length--) {
|
while (length--) {
|
||||||
uint8_t value = *source++;
|
uint8_t value = *source++;
|
||||||
*dest++ = (value < threshold) ? QUIRC_PIXEL_BLACK : QUIRC_PIXEL_WHITE;
|
*dest++ = (value < threshold) ? QUIRC_PIXEL_BLACK : QUIRC_PIXEL_WHITE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *quirc_begin(struct quirc *q, int *w, int *h)
|
uint8_t *quirc_begin(struct quirc *q, int *w, int *h)
|
||||||
|
@ -1102,8 +1102,8 @@ void quirc_end(struct quirc *q)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
uint8_t threshold = otsu(q);
|
uint8_t threshold = otsu(q);
|
||||||
pixels_setup(q, threshold);
|
pixels_setup(q, threshold);
|
||||||
|
|
||||||
for (i = 0; i < q->h; i++)
|
for (i = 0; i < q->h; i++)
|
||||||
finder_scan(q, i);
|
finder_scan(q, i);
|
||||||
|
|
Loading…
Reference in a new issue