You are on page 1of 5

Thresholding

Thresholding is the simplest method of


image segmentation.

From a grayscale image, thresholding can


be used to create binary images i.e. image
with only black or white colors.

It is usually used for feature extraction


where required features of image are
converted to white and everything else to
black. (or viceversa!
"ifferent Image Types
#$%$& ' (&)*+#)%, ' T-&,+-$%"
I.)(,
Thresholding

+teps / )lgorithm

Traverse through entire input image array.

&ead individual pixel color value (01bit! and


convert it into grayscale.

#alculate the binary output pixel value (black


or white! based on current threshold.

+tore the new value at same location in


output image.
Thresholding %ogic
gs = (r+g+b) / 3; // grayscale
if(gs < th) {
pix = 0; // pure black
}else {
pix = 0xFFFFFF; // pure white
}
2seudo #ode
int input[][];
int output[][];
int r, g, b, gs, pix, x, y;
int th = 128; // th value can be accepted ro! user or can be pre"set on users needs
or#y=$;y%h;y&&' (
or#x=$;x%);x&&' (
pix = input[y][x]; // read input pixel
b = pix * $x++; // extract blue
g = #pix ,, 8' * $x++; // extract green
r = #pix ,, 1-' * $x++; // extract red
gs = #r & g & b' / .; // calculate grayscale co!ponent
i#gs % th' (
pix = $;
/else (
pix = $x++++++;
/
output[y][x] = pix; // store to output i!age
/
/

You might also like