Image Processing using Matlab/Octave — Part 2

Adesh Gautam
The Startup
Published in
5 min readApr 12, 2019


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)

RGB Color Model

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)

CMY Color Model

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).

Additional functions in Matlab/Octave

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


                  img = imread(“lena.png”);


                  imwrite(img, “Lena.png”);



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);


                   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 variables
close all; % close all figures
clc; % clear command window
% import image package
pkg load image;
% read image
img = imread(“lena.png”);
% convert image into gray and then from uint8 to double
grayscale_img = rgb2gray(img);
% show grayscale image
title(“grayscale image”);
imwrite(grayscale_img, “original.jpg”);
# calculate negative of the image
output = 255 — grayscale_img;
% show output image
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.


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.


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.


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 X
b = 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:

Github LinkedIn Twitter Instagram

This story is published in The Startup, Medium’s largest entrepreneurship publication followed by +442,678 people.

Subscribe to receive our top stories here.