After a couple of test images, it seems that automatically finding the threshold value is not easy. The histogram sometimes has two bumps, but usually one large bump with a smaller side lobe. As it is unclear where the threshold should lie I decided to take a different route: what if we take the histogram of a grayscale image?
Instead of counting white or black pixels, we just add all grayscale values in one line. After all, a high intensity value should weigh more than low intensity values. There are different ways to weigh, but why not use the values because looking at the image gives us the info we need anyway, maybe this is a good way to encode it.
Looking at the figure above, you’ll see that this looks similar to earlier debug image. The image is not thresholded (as explained above), the histograms are smoothed a bit differently and a histogram is added (bottom left, white). In the histogram you’ll also see a red line, this is the threshold chosen by the auto-threshold algorithm (which, in this case is pretty good, but is mostly off). Not thresholding saves us the problem of finding the threshold and finding peaks according to a complex algorithm.
The top and base lines are now found by selecting the top and bottom local maximum above a threshold in the smoothed density (smoothed twice by a window of 11). After that, a local maximum is searched for in the derived density histogram below the top value and a local minimum above the bottom value. This seems to select the correct top and base lines in all my test images.
Note that the vertical density histogram (top, red) seems to have more distinct peaks, but this is mostly due to a different scale. Anyway, I’m very happy with the new digit separation, next will be preprocessing of the extracted digits.
Due to the circular light, with a sort of radial fade-out, the digits will have to be thresholded separately anyway. A different (more complex) idea is to pre-calculate an anti fade-out and mask the image with that, so the brightness is corrected. Quick testing shows that thresholding digits separately gives pretty good results, so maybe that isn’t necessary. More later!
