MATLAB image processing

From Wikipedia, the free encyclopedia

MATLAB

MATLAB 6.5 being used to manipulate a bitmap image.
Developer: The MathWorks
Latest release: R2006b / September 1, 2006
OS: Cross-platform (list)
Use: Technical computing
License: Proprietary
Website: MATLAB product page

MATLAB provides a suitable environment for image processing. Although MATLAB is slower than some languages (such as C), its built in functions and syntax makes it a more versatile and faster programming environment for image processing. Once an algorithm is finalized in MATLAB, the programmer can change it to C (or another faster language) to make the program run faster.

MATLAB does not have the easy to use interfaces of Adobe Photoshop. MATLAB is used to test and tweak new image processing techniques and algorithms. Almost everything in MATLAB is done through programming and manipulation of raw image data and not a user interface. The effects and filters in Photoshop (or any other image editing software) are actually algorithms. With MATLAB, the user can create these complex algorithms that are applied in Photoshop.

Contents

[edit] Image Matrices

MATLAB handles images as matrices. This involves breaking each pixel of an image down into the elements of a matrix. MATLAB distinguishes between color and grayscale images and therefore their resulting image matrices differ slightly.

[edit] Color Images

A color is a composite of some basic colors. MATLAB therefore breaks each individual pixel of a color image (termed ‘truecolor’) down into Red, Green and Blue (RGB) values. What we get as a result, for the entire image, are 3 matrices, one representing each color. The three matrices are stacked next to each other creating a 3 dimensional m by n by 3 matrix.

For an image which has a height of 5 pixels and width of 10 pixels the resulting in MATLAB would would be a 5 by 10 by 3 matrix for a truecolor image.

Image:matlRGB.jpg

[edit] Grayscale Images

A grayscale image is a mixture of black and white colors. These colors, or as some may term as ‘shades’, are not composed of Red, Green or Blue colors. But instead they contain various increments of colors between white and black. Therefore to represent this one range, only one color channel is needed. Thus we only need a 2 dimensional matrix, m by n by 1. MATLAB terms this type of matrix as an Intensity Matrix, because the values of such a matrix represent intensities of one color.

For an image which as height of 5 pixels and width of 10 pixels the resulting matrix would be a 5 by 10 matrix for grayscale image.

Image:matlGray.jpg

[edit] Colormaps

MATLAB also allows the use of colormaps. Basically a colormap is a m by 3 matrix representation of all the possible colors in the image/matrix. Modifying, creating or applying one of MATLAB’s predefined colormaps will effectively change the color range of an image.

Image:Colormap.jpg

imread(‘imageName’) returns a matrix representation of an image. MATLAB can infer automatically whether to use a m by n by 3 (for color) or simply a m by n (for graycale) matrix.

[edit] Pixel values

MATLAB, by default, will use integer values (which MATLAB terms as uint8) that have a range of integers from 0 to 255 to represent a pixel value. 0 stands for the lightest color and 255 stands for the darkest color possible. This applies to the RGB (Red Green Blue) color channels. In the case of the Red channel, lower numbers will produce lighter (pink) values for red, and higher numbers near 255 will produce darker (maroon) values of red. For the intensity matrix (m by n by 1) this scale applies for colors between white and black (or depends on the colormap being used).

The second type of pixel values used for images are called double which are floating point (decimal) numbers between 0 and 1. This range is proportional to the uint8 range and therefore multiplying each double pixel value by 255 will yield a uint8 pixel value. Similarly conversion from uint8 to double is done by dividing the uint8 value by 255.

MATLAB does have casting functions uint8() and double(). But these only change the data type and does not scale the values. Scaling must be done manually.

The reason MATLAB has two formats is because uint8 values take less storage. But in many older versions of MATLAB (version 6.0) direct arithmetic operations on uint8 values is not possible because of accuracy issues. Therefore to perform arithmetic operations, the pixel values must be converted to double first. In version 2006a, this is not an issue as MATLAB simply changes uint8 to double first, does the operations, and then changes the values back to uint8.

[edit] Image Processing Toolbox

MATLAB does have extensions which contain functions geared towards image processing. They are grouped under the ‘Image Processing Toolbox’. In some versions it is an optional extra which users have to pay for, while in others it comes packaged with the software. This toolbox is especially helpful for applying numerous filters (such as linear and deblur) and also includes algorithms which can detect lines and edges in an image. A programmer can write almost all the features in this extension. Otherwise the command ‘edit commandName” usually allows a user to see/modify lines of the built-in functions that MATLAB and its Image Processing Toolbox provide.

[edit] Basic Tasks

Converting a color image to grayscale
  1. Since we know that an RGB is a m-n-3 matrix and a grayscale image is a m-n-1 matrix we can make our new grayscale intensity matrix from either one of the red, green or blue channels. In this way we are accounting for only one channel values for our grayscale image.
  2. Another common way is to combine elements of the red, green and blue channel with a weight by using, weightR * RedChannel + weightG*GreenChannel + weightB*BlueChannel. Where, the weights, weightR + weightG + weightB = 1. This method allows us to mix each channel selectively to get a grayscale image.
  3. More recent versions of MATLAB have a function called rgb2gray() -which turns an RGB matrix to a grayscale matrix. This function is an implementation of the option listed above.
Converting a grayscale image to color
Unfortunately this is hard to do accurately with basic MATLAB functions. Part of the ongoing research is how to do this accurately. But very crude colorization is possible.
It is possible to attach a color filter to a grayscale image to make it more colorful. Images can also be colored in small patches, with different filters.
The coloring of a grayscale image is often achieved simply by applying a different colormap.
Converting from uint8 to double pixel values
Simply divide the uint8 pixel value by 255 (scaling the value) to get double representation between 0 and 1. Then cast as a double. Only casting will not perform the required scaling. Version 2006a has a function im2double(), which performs the scaling.
Converting from double to uint8 pixel values
Simply multiply the double pixel value by 255 (scaling the value) to get uint8 representation between 0 and 255. Then cast the value. Only casting will not perform the scaling. The latest release, version 2006a, does have an im2uint8() function which does perform the required scaling.

[edit] See also

[edit] External links & References