# Accessing and Analysing Multiple GrayScale Images

by 4hathacker  |  in Image Processing at  5:20 PM

Hello Everyone!!!

This article deals with the analysis of grayscale images, working with thresholding, and introduction to histogram and histogram equalization.

During a technical interview for an internship, I was asked how to access multiple images and do the analysis for each image at the same time. I never have tried for this before the interview, so shooting an arrow in the dark, replied to use a 'for loop' for the purpose. The interviewer asked again and again yet I failed to give him the perfect explanation. But I got selected for the internship.

In this article, the images from a particular directory accessed by MATLAB have been analysed for thresholding and histograms. Thresholding refers to the process of creating binary images using a certain threshold value such that the pixel information greater than the threshold is white and rest is black. This may be local, global or region based depending upon the requirements. Here we have covered only global thresholding. Automatic Thresholding by Otsu's method is preferred over manual thresholding for global one. Histogram, we have studied since fifth grade, were the same. But here they deal with the distribution of the pixel intensitities in a pre-defined number of bins(ranges of intensitites), spanning from minimum to maximum intensity. The point to be noted is the default value of bins for grayscale images is 256. But the user can provide extra input for number of bins. The advantage of histogram lies in optimal threshold detection as well as image enhancement. This could be seen with the help of our example.

clc; clear all; close all;
filelocat = dir('D:\all_MATLAB\final year pro\mydir2\*.jpg');
for i = 1:length(filelocat)
filename = strcat('D:\all_MATLAB\final year pro\mydir2\',filelocat(i).name);
I = imread(filename);
my_gray = I;                                                % because my file is already in grayscale
% if not the case write rgb2gray(I)
thresh = graythresh(I);                                % automatic thresholding by Otsu's Method
img = im2bw(I,thresh);                               % binary image obtained by thresholding
img_eq = histeq(my_gray);                         % Histogram Equalization
img_adj = imadjust(my_gray);                    % Contrasting Enhancement using imadjust()
img_adhisteq = adapthisteq(my_gray);       % Adaptive Histogram Equalization

imadjust() is a function in MATLAB which alters the original pixel values ensuring a small percentage values to be saturated at low and high intensities providing a smoother transformation in enhancement.

While, the adapthisteq() is a special method for adjusting contrast of local histograms by splitting the image into small rectangular regions. This is also known as Contrast Limited Adaptive Histogram Equalization (CLAHE) (Zuiderveld, Karel. Contrast Limited Adaptive Histogram Equalization. Graphic Gems IVSan Diego: Academic Press Professional, 474-485, 1994).

figure
subplot(3,2,1), imshow(I), title('Original Image');
subplot(3,2,2), imshow(img), title('Thresholded Image');
subplot(3,2,3), imshow(my_gray), title('Grayscale');
subplot(3,2,4), imshow(img_eq), title('Eq_Im_His');
subplot(3,2,5), imshow(img_adj), title('Adj_Im_His');
subplot(3,2,6), imshow(img_adhisteq), title('CLAHE');

figure
subplot(2,2,1),imhist(my_gray),title('Histogram');
subplot(2,2,2),imhist(img_eq,64),title('Eq_Im_Hist');
subplot(2,2,3),imhist(img_adj,64),title('Adj_Im_Hist');
subplot(2,2,4),imhist(img_adhisteq),title('CLAHE');

imtool(img_adhisteq), title('CLAHE');
end

Finally, plot the results using suitable plot and subplot functions in figure. The so obtained results for a test image are shown below. The test image present in my directory is a standard X-Ray image taken from online databases of Clinical Research in Lung Cancer for Opacity Detection.

The image contrast enhancement can also be inspected using imcontrast() function for which the last line of the script is meant for which indicates CLAHE image in imtool(). In the toolbar opened, the fourth icon,which shows "Adjust Contrast" is for adjusting contrast level of images. This contrast can be used for setting up the contrast limits for an image within a certain range using histogram.

## Youtube

Proudly Designed by 4hathacker.