Intensity Transformation and Spatial Filtering in MATLAB
MATLAB provides an inbuilt function imresize for rescaling an image. Let’s give it a shot!
The syntax is: B = imresize(A, scale)
Where, B = output image (scale * sizeof image A)
A = input image
Without using imresize: pixel replication and deletion
In case if you don’t want to use the inbuilt function imresize, you can resize the image using your own code. Image dimensions can be increased using pixel replication. This is used in zooming the image as it helps in maintaining the clarity of image to some extent. One can also use interpolation to increase the dimensions of image.
In case of zooming out of the image, a common technique that is followed is pixel deletion in which alternate columns and rows of the image are removed, thereby reducing the size of the image. Now, let’s check out the code for pixel replication and deletion. As a practice exercise, write a code for binary interpolation and compare the results obtained using it and pixel replication. Which one gives a better image?
We can adjust the image intensity values or color map using the inbuilt function imadjust.
Img = imread(‘pout.tif’); Out_img = imadjust(Img); figure; subplot(121); imshow(Img); title(‘Original Image’); subplot(122); imshow(Out_img); title(‘Image after abdjustment’);
Changing the brightness of image
surf(membrane) beta = .7; brighten(beta)
imadd add two images or add constant to image. Z = imadd(X,Y) adds each element in array X with the corresponding element in array Y and returns the sum in the corresponding element of the output array Z.
I = imread('rice.png'); J = imread('cameraman.tif'); K = imadd(I,J,'uint16'); imshow(K,)
imsubtract subtract one image from another or subtract constant from image Z = imsubtract(X,Y) subtracts each element in array Y from the corresponding element in array X and returns the difference in the corresponding element of the output array Z.
I = imread('rice.png'); background = imopen(I,strel('disk',15)); J = imsubtract(I,background); imshow(I) figure imshow(J)
Generating image histogram
An image histogram basically tells how many pixels are there having a specific grayscale value. This can be used to understand the distribution of grayscale values, coming up with threshold value, increasing the contrast of the image, distributing the grayscale values among the pixels, etc.
Image histogram can be generated using the inbuilt function imhist.
Img = imread(‘pout.tif’); Out_img = imadjust(Img); figure; subplot(121); imhist(Img); subplot(122); imhist(Out_img);
Histogram equalization is the process for distributing the grayscale values among the pixels equally. In case of discrete values (like in the case of digital images), obtaining a completely uniformly distributed histogram is not possible. Histogram equalization helps in obtaining a high contrast image and makes sure that the image is neither too dark nor too light.
Histogram equalization can be carried out using the inbuilt function histeq.
Img = imread(‘pout.tif’); Out_img = histeq(Img); figure; subplot(221); imhist(Img); subplot(222); imhist(Out_img); subplot(223); imshow(Img); subplot(224); imshow(Out_img);
Changing the contrast of image
Contrast of Image can be changed using imadjust Command in MATLAB
C=imread(‘football.jpg’); imshow(C ); C_Change=imadjust(C,[0.2 0.3 0;0.6 0.7 1],); figure(2); imshow(C_Change);