Sibnz
Sibnz

Reputation: 63

On which basis block size value is given in adaptive threshold?

I am not really understanding the purpose of using block size in adaptive threshold. And also want to know on which basis a value is assigned as block size. Consider the code :

gaussian=cv2.adaptiveThreshold(grayscaledImage,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,115,1)

Here I am trying to get the adaptive gaussian threshold of a Gray Scaled Image. When I am assigning the block size as 115, like the above code, the code is running well .

But if , I assign the block size as 114 or 116 like :

gaussian=cv2.adaptiveThreshold(grayscaledImage,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,116,1)

This code is getting error. Now my main question is, how can I understand which value I should give as block size?

Upvotes: 1

Views: 7785

Answers (2)

Mehmet Kilinc
Mehmet Kilinc

Reputation: 41

Adaptive thresholding essentially makes an image out of every single pixel and computes a threshold value for each one of these pixels. The block size determines how big these individual images are e.g. how many of the neighboring pixels are included in these individual images.

If we have a block size of 3. This means the individual image area is a 3x3 pixel array where the pixel of interest is the center of this image. So the other 8 pixels surround this centered pixel. A block size of 1 is simply the pixel of interest. A block size of 5 creates a 5x5 pixel array, once again with the pixel interest in the center. The program fails when you place an even number because the original pixel is not the, nor can be the, centered pixel. So a block size of 2 creates a 2x2 pixel array which has no center. This is what is causing your error.

Upvotes: 4

Piglet
Piglet

Reputation: 28974

From the documentation:

https://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html

blockSize – Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.

A block is centered around a pixel so it has to be an odd number to make any sense. 3,5,7 and so on means odd numbers > 1

Please read documentations. They tell you how to use stuff properly.

Upvotes: 6

Related Questions