Introduction to Image Processing | MATLAB Tutorial
“An image is worth a thousand words”. We rely heavily on images for interpreting large data sets and many-a-times, to make sense out of the world around us. In such a case, it becomes really important to understand the concept of Image Processing. Before jumping into details, let’s get a brief idea of what images and image processing mean when we are dealing with MATLAB.
2 Basic terms
2.1 Digital Image
When working in MATLAB, Image is just an array (or matrix in MATLAB) of values, which are more formally known as pixels. Each pixel represents some information about a particular area of image. Digital image differs from a photo in that the coordinates of pixels and their values (intensity) are all discrete.
2.2 Image processing
Image processing in simple terms is changing the nature of an image for two basic tasks:
1. Improve the image so that it can become more suitable for human interpretation.
2. Change the image in such a manner that it can be processed by a machine in an efficient and accurate manner.
3 Reading, displaying and saving the image
3.1 Reading the image
The very first step in any image processing program is to actually read the image. For this, MATLAB has an inbuilt function imread.
>> img = imread(‘MATLABHelper.jpg’);
Notice that in the workspace, the variable img gets stored in the format uint8 (8 – bit unsigned integers). This is the default data type for image storage in MATLAB.
3.2 Displaying the image
Once we are done with step – 1, we can actually display the image in a figure window using imshow (inbuilt) command.
>> figure, imshow(img);
Figure 1: Image displayed in a new figure window using imshow.
There is another command which gives a very similar result – imagesc. The additional advantage of using imagesc over imshow is that it displays the image on scaled axes with the minimum value as black and the maximum value as white.
Figure 2: Displaying the image using imagesc. Notice the axes.
3.3 Writing (or saving) the image
It’s always a good habit of saving the final processed image. So, when you have to see the result image, you can directly open the saved image instead of running the program again and seeing the image. For this we use the command imwrite.
>> imwrite(img, ‘new_img.png’);
MATLAB supports a limited set of image types – jpeg, jpg, gif, png, tif are some of the commonly used ones. Make sure that you save the image in these formats only so that if required, you can read these images and further process them.
4 Image Information
Information about an image like the absolute filepath, size of the image, width and height of image, colortype (grayscale or RGB) can be found out using the following command:
>> imfinfo ‘MATLABHelper.jpg’
Figure 3: Result of imfinfo ‘MATLABHelper.jpg’
There is another command which is used for a lot similar purpose – size.
Note: There is a crucial difference between the size of colored images (RGB) and grayscale images. Try to read a colored image and check its size. You will find that instead of just height and width of the image, the digit 3 is also displayed. This signifies the 3 channels – Red, Green and Blue.
5 Image data types
As we discussed earlier, uint8 is the default data type for storing images in MATLAB. There are some other types also, which are frequently used while processing the Image – uint16, uint32, int8, double, etc. Image data type can be converted from one type to another based on the type of accuracy that is required. For example, when using power law transformation (which helps in adjusting the contrast of the image), it is a good habit to use double instead of uint8. In such a scenario, we can change the data type of image using the following technique:
>> img = double(img) % Converts the image type to double
>> img = uint8(img) %Converts image type back to uint8
There is one big disadvantage with this method. This can result in loss of resolution as values are truncated based on the range permitted by a specific data type. To prevent this loss, there are specific functions for images which rescale the values based on range of the type.
For example, im2double converts input image to double type. Similarly, im2bw converts the image to binary (or logical) type. This type of black and white image helps in faster processing of image as there are only 2 values present in the image – 255 (for white) and 0 (for black).
Figure 4: Result of converting an image to logical type using im2bw
6 Extracting RGB components of image
An RGB image consists of values taken from 3 channels – Red, Green and Blue. These 3 channels can be separated out to display image having only Red components, or a combination of only red and green components, etc.
% reading the image img = imread('lena.jpg'); figure; subplot(221); % displaying original image using imagesc % try replacing imagesc with imshow and compare the difference imagesc(img); title('RGB Image'); subplot(222); imagesc(img(:,:,1)); title('Red'); subplot(223); imagesc(img(:,:,2)); title('Green'); subplot(224); imagesc(img(:,:,3)); title('Blue');
Figure 5: Displaying the Red, Green and Blue components of Image
7 Applications of Image Processing
Image processing plays a really important role in real life. Some of the common applications of Image Processing are:
1. Medical field – Magnetic Resonance Imaging, X Ray Imaging, Gamma ray scanning, etc. In these techniques, the scanned images are first processed and then displayed.
Figure 6: Image Processing of MRI Scan to extract data from the images
2. Checking manufactured goods to check if they clear the minimum quality standards. Some of these products include circuit board controller, packaged pills, bottles, bubbles in clear plastic bottles, etc. This comes real handy when one has to check each and every product manufactured like in FMCGs. Even a small mistake cannot be afforded. In such cases, image processing plays an important role by identifying the presence of a marking feature on the product which is then used to check whether the product is damaged or not.
3. Automated license plate reading – In places like USA, the roadside scanners are used to take pictures of number plates of moving cars and then using image processing, the registration number is extracted from the image. This can be used for keeping a track of each and every car across the area.
Figure 7: Number plate extraction using Image Processing. It is a complex procedure involving pre-processing of images, image segmentation and optical character recognition. (Source: http://www.platerecognition.info/)
4. Fingerprint matching – Using image processing, one can extract the information about minutiae from the image of a fingerprint scan which can then be used to check whether a match exists in the database or not.
Figure 8: Fingerprint recognition procedure. (Source: http://www.bioelectronix.com/)
5. Iris matching – Just like fingerprints, iris scans are also being used extensively for much more secure method of identity verification. This involves steps like pre-processing (noise removal), extraction of region of interest, apply filters to extract information, matching the data extracted from data present in database and so on.
Figure 9: Automatic iris detection in eye scan using Circle detection techniques
6. Object detection and recognition is a major step in various types of projects or in guidance and manoeuvring of aircraft or missile system. This is also being used in driverless cars to identify whether there is a person or a car in front of the moving vehicle. Similarly, this is employed in obstacle detection modules in robots.
Figure 10: Monocular object detection using 3D geometric primitives. (Source: http://disneyresearch.com/)