# Image Processing using Matlab/Octave — Part 2

Published in

--

In the previous part we saw what is Image Processing, how it works and some basics of Matlab/Octave. In this part we’ll see color model, some additional functions in Matlab/Octave and how to do basic image enhancement.

# What is Color Model ?

Color model is a mathematical way to represent colors as combination of three or four numbers in a tuple.

Let’s see some of the color models.

RGB (Red Green Blue)

It is an additive color model in which you can see that the mixture of all primary colors Red, Green and Blue gives white and also you can see Cyan, Magenta and Yellow.

CMY (Cyan Magenta Yellow)

Here mixing all the three of them gives black. This model is widely used in printing but as this black is not pure black so K(Key) is added to complete it as CMYK, where K stands for black.

Another color model HSV(Hue Saturation Value) is also used.

Here, you can see that the deeper in the cone towards bottom, the darker is the color(value), on the edges of the cone the color is the brightest (Saturation) and about the circumference you can see different colors (Hue).

First, we’ll see how to read/write images in Matlab and display them.

`                  img = imread(“lena.png”);`

Writing:

`                  imwrite(img, “Lena.png”);`

Displaying:

`                         imshow(img); `

Image Conversion

For converting between images there are built-in functions in Matlab, such as:

RGB → Gray:

`                  gray_image = rgb2gray(rgb_image);`

RGB →Binary:

`                     binary = im2bw(rgb_image);`

RGB → HSV:

`                   hsv_image = rgb2hsv(rgb_image);`

# Image Enhancement

The motive of enhancement is to process an image so that the result is more suitable than the original image for a specific application. Image enhancement approaches fall into two broad categories: Spatial domain and Frequency domain.

In spatial domain the image manipulation is done directly on the pixels of an image. It can be denoted as:

`                    G(x, y) = T[F(x, y)]`

where, (x, y) are the coordinates of the pixels, F is the input image, G is the enhanced image and T is an operator on F.

Now let’s see these operators:

1. Image Negative

The negative of an image is just the subtraction of pixel values from 255 for a gray image. It is of the form:

`                           N = (L - 1) - I`

where, L-1 = 255, for gray image and I is the image pixels.

Now let’s implement this in Matlab:

`clear all; % clear all variablesclose all; % close all figuresclc;       % clear command window% import image packagepkg load image;% read imageimg = imread(“lena.png”);% convert image into gray and then from uint8 to doublegrayscale_img = rgb2gray(img);% show grayscale imagefigureimshow(grayscale_img);title(“grayscale image”);imwrite(grayscale_img, “original.jpg”);# calculate negative of the imageoutput = 255 — grayscale_img;% show output imagefigureimshow(uint8(output));title(“output image”);imwrite(uint8(output), “negative_tansformation.jpg”);`

The first 3 lines clears the variables in memory, closes all opened windows and clears the terminal.

Then, we import the image package. If you don’t know how to do this, go here and download the package and follow this tutorial.

Now image is read and converted to grayscale. After that a new window is created, image is displayed with title “grayscale image” and written as “original.jpg”.

“output” is the negative of the input image and the same displaying and writing is done.

2. Log Transformation:

It is done using the following operations:

`                           s = c log(1+r)`

where c is a constant.

It expands the values of dark pixels(near 0) and compresses the higher level values(near 255) i.e., it compresses dynamic range of an image and the image looks washed out.

Code:

`c = 45;output = c * log(1 + grayscale_img);`

3. Power Law Transformation

It is also called as Gamma correction. It has the basic form:

`                             s = c * r^𝛄`

where c and 𝛄 are constants and r is the input image.

This is a plot of s versus 𝛄, where 𝛄 < 1 results in dark values and 𝛄 >1 results in bright values.

Code:

`r = double(img)/255;c = 1;gamma = 0.6;s = c * (r) .^ gamma;`

4. Contrast Stretching

Contrast is a measure of the “range” of an image; i.e. how spread its intensities are.

`            Contrast = ( Imax — Imin ) / ( Imax + Imin )`

It maps minimum intensity in the image to the minimum value in the range (0–100) and maximum intensity in the image to the maximum values (200–255). Contrast stretching is all about increasing the difference between the maximum intensity value in an image and the minimum one. All the rest of the intensity values are spread out between this range.

Code:

It can be done in just one line of code using imadjust.

`cs = imadjust(gray_img, stretchlim(gray_img),[]);`

or doing it using the formula:

`a      = min(img(:));            %minimum pixel of image Xb      = max(img(:));            %maximum pixel of image X cs_img = (img - a) .* (255 / (b - a));`

Next, we’ll see image filtering using MATLAB/Octave.

Please click on the 👏 button if you liked the post and hold it for giving more love.

If you wish to connect:

--

--