pythonopencvimage-processingimage-segmentationadaptive-threshold

Intuition behind adjusting adaptive thresholding parameters


While following this guide and using the adaptive thresholding function, I was confused about the intuition with adjusting the blocksize and C to find an ideal value.

What approach should one take when adjusting these parameters instead of doing a completely randomized guess and check process? Is there a better technique for learning these parameters based on an image's color histrogram?

cv2.adaptiveThreshold(img, maxValue, adaptiveMethod, thresholdType, blockSize, C)

Solution

  • The block size should be chosen such that a block always sees both foreground and background. If the block is too small, a block that is completely inside the foreground or background will not see the actual contrast in the region, it will only see the noise. Consequently, for that block, the thresholded result will not separate background and foreground, but noise within the single phase.

    The threshold value C can be zero if every block sees a good amount of both phases.

    If the block size cannot be chosen large enough, and some blocks see only background, then the C value can be set large enough for these blocks to result in only background. Two times the standard deviation of the noise in the background is a good start value.

    Likewise, if it is the foreground phase that is larger, set C to a negative value such that a block completely in the foreground results in only foreground.