Most of the images acquired are in color or RGB format, which would need more processing power, memory and time to process. In a lot of cases, images could be converted to gray scale or binary for processing, which require less computing power.
In this tutorial, we are going to learn how to convert the color image to grayscale and binary image prior to the processing.
Converting from Color to Grayscale image
- Now let’s use the command we’ve learn to import and visualize another color image taken by handphone camera.
–>S = imread(‘measure.jpg’);
- Convert the color image to grayscale by using “rgb2gray”command.
–>Sgray = rgb2gray(S);
- Convert the grayscale image to binary image by using “im2bw”.
–>Sbin = im2bw(Sgray,0.5);
The second argument of the “im2bw” command is the threshold value which is used to decide the level of intensity value to be converted to white and black. Different values would give different results. Some results are shown in the following figures:
Choosing Threshold Value
As seen in previous example, choosing a right threshold value is important to get a good binary image for further processing. First we try a manual way to select the threshold value by looking into the image histogram.
- Use “imhist” command to compute and show the image histogram.
Since the background of the image is in darker color, the pixel value should be lower than the objects (coins and alphabets). From the histogram, we could roughly select the threshold value to differentiate background from objects.
- Choose the value 90, as the threshold, normalisation is required to get the threshold value for the “im2bw” function.
- Use the new threshold value for the conversion to binary image.
–>Sbin = im2bw(Sgray,0.353);
- We could also use the function from the Scilab Image Processing module to find the threshold value automatically.
–>th = imgraythresh(Sgray)
–>Sbin = im2bw(Sgray,th);