# Digital Image Processing

## What is an Image???

Image is the combination of pixels and Pixel is nothing but a part of Picture. An image is nothing more than a two dimensional signal. It is defined by the mathematical function f(x, y) where x and y are the two co-ordinates horizontally and vertically. The value of f(x, y) at any point is gives the pixel value at that point of an image.

The side figure is an example of digital image that we are now viewing on your computer screen. But actually , this image is nothing but a two dimensional array of numbers ranging between 0 and 255.

128 30 123
232 123 321
123 77 89
80 255 255

Each number represents the value of the function f(x, y) at any point. In this case the value 128 , 230, 123 each represents an individual pixel value. The dimensions of the picture is actually the dimensions of this two dimensional array.

## What is Digital image processing

Digital image processing is the use of computer algorithms to perform image processing on digital images. As a subcategory or field of digital signal processing, digital image processing has many advantages over analog image processing.

## How a digital image is formed

Since capturing an image from a camera is a physical process. The sunlight is used as a source of energy. A sensor array is used for the acquisition of the image. So when the sunlight falls upon the object, then the amount of light reflected by that object is sensed by the sensors, and a continuous voltage signal is generated by the amount of sensed data.

In order to create a digital image , we need to convert this data into a digital form. This involves sampling and quantization. The result of sampling and quantization results in an two dimensional array or matrix of numbers which are nothing but a digital image.

## PIXEL RESOLUTION

Before we define pixel resolution, it is necessary to define a pixel.

## Pixel

• We have already defined a pixel in our tutorial of concept of pixel, in which we define a pixel as the smallest element of an image.
• We also defined that a pixel can store a value proportional to the light intensity at that particular location. Now since we have defined a pixel, we are going to define what is resolution.

## Resolution

• The resolution can be defined in many ways. Such as pixel resolution , spatial resolution , temporal resolution , spectral resolution. Out of which we are going to discuss pixel resolution.
• We have probably seen that in our own computer settings , we have monitor resolution of 800 x
• 600 , 640 x 480 e.t.c
• In pixel resolution , the term resolution refers to the total number of count of pixels in an digital
• image. For example. If an image has M rows and N columns , then its resolution can be defined as
• M X N.
• If we define resolution as the total number of pixels , then pixel resolution can be defined with set of two numbers. The first number the width of the picture , or the pixels across columns , and the second number is height of the picture , or the pixels across its width.
• We can say that the higher is the pixel resolution , the higher is the quality of the image. We can define pixel resolution of an image as 4500 X 5500.

## Megapixels

We can calculate mega pixels of a camera using pixel resolution.
Column pixels width X row pixels height / 1 Million.
The size of an image can be defined by its pixel resolution.
Size = pixel resolution X bpp (bits per pixel)

## Calculating the mega pixels of the camera

Lets say we have an image of dimension: 2500 X 3192.
Its pixel resolution = 2500 * 3192 = 7982350 bytes.
Dividing it by 1 million = 7.9 = 8 mega pixel approximately.

## Aspect ratio

Another important concept with the pixel resolution is aspect ratio.
Aspect ratio is the ratio between width of an image and the height of an image. It is commonly
explained as two numbers separated by a colon 8: 9. This ratio differs in different images , and in
different screens. The common aspect ratios are:
1.33:1, 1.37:1, 1.43:1, 1.50:1, 1.56:1, 1.66:1, 1.75:1, 1.78:1, 1.85:1, 2.00:1, e.t.c

Aspect ratio maintains a balance between the appearance of an image on the screen , means it
maintains a ratio between horizontal and vertical pixels. It does not let the image to get distorted
when aspect ratio is increased.

## Zooming

Zooming simply means enlarging a picture in a sense that the details in the image became more visible and clear. Zooming an image has many wide applications ranging from zooming through a camera lens , to zoom an image on internet etc.

## Optical Zoom vs digital Zoom

These two types of zoom are supported by the cameras.

## Optical Zoom:

The optical zoom is achieved using the movement of the lens of your camera. An optical zoom is actually a true zoom. The result of the optical zoom is far better then that of digital zoom. In optical zoom , an image is magnified by the lens in such a way that the objects in the image appear to be closer to the camera. In optical zoom the lens is physically extend to zoom or magnify an object.

## Digital Zoom:

Digital zoom is basically image processing within a camera. During a digital zoom , the center of
the image is magnified and the edges of the picture got crop out. Due to magnified center , it looks like that the object is closer to you.
During a digital zoom , the pixels got expand , due to which the quality of the image is
compromised.
The same effect of digital zoom can be seen after the image is taken through your computer by
using an image processing toolbox / software, such as Photoshop.

## Zooming methods:

Although there are many methods that does this job , but we are going to discuss the most
common of them are here. They are listed below.

• Pixel replication or Nearest neighbor interpolation
• Zero order hold method
• Zooming K times

## Introduction:

It is also known as Nearest neighbor interpolation. As its name suggest , in this method , we just
replicate the neighboring pixels. As we have already discussed in the tutorial of Sampling , that
zooming is nothing but increase amount of sample or pixels. This algorithm works on the same
principle.

## Working:

In this method we create new pixels form the already given pixels. Each pixel is replicated in this
method n times row wise and column wise and you got a zoomed image. Its as simple as that.

## For example:

if you have an image of 2 rows and 2 columns and you want to zoom it twice or 2 times using pixel
replication, here how it can be done. For a better understanding , the image has been taken in the form of matrix with the pixel values of the image.

1 2
3 4 image has two rows and two columns, we will first zoom it row wise.

## Row wise zooming:

When we zoom it row wise , we will just simple copy the rows pixels to its adjacent new cell.
Here how it would be done.
1 1 2 2
3 3 4 4
As you can that in the above matrix , each pixel is replicated twice in the rows.

## Column size zooming:

The next step is to replicate each of the pixel column wise, that we will simply copy the column
pixel to its adjacent new column or simply below it, Here how it would be done.
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4

## New image size:

As it can be seen from the above example , that an original image of 2 rows and 2 columns has
been converted into 4 rows and 4 columns after zooming. That means the new image has a
dimensions of: Original image rows ∗ zooming factor, Original image cols ∗ zooming factor

One of the advantage of this zooming technique is , it is very simple. You just have to copy the
pixels and nothing else.
The disadvantage of this technique is that image got zoomed but the output is very blurry. And as
the zooming factor increased , the image got more and more blurred. That would eventually result
in fully blurred image.

## Introduction

Zero order hold method is another method of zooming. It is also known as zoom twice. Because it
can only zoom twice. We will see in the below example that why it does that.

## Working

In zero order hold method , we pick two adjacent elements from the rows respectively and then we
add them and divide the result by two, and place their result in between those two elements. We
first do this row wise and then we do this column wise.

## For example

Lets take an image of the dimensions of 2 rows and 2 columns and zoom it twice using zero order
hold.
1 2
3 4
First we will zoom it row wise and then column wise.

## Row wise zooming

1 1 2
3 3 4
As we take the first two numbers : 2 + 1 = 3 and then we divide it by 2, we get 1.5 which is
approximated to 1. The same method is applied in the row 2.

## Column wise zooming

1 1 2
2 2 3
3 3 4 We take two adjacent column pixel values which are 1 and 3. We add them and got 4. 4 is then
divided by 2 and we get 2 which is placed in between them. The same method is applied in all the
columns.

## New image size

As you can see that the dimensions of the new image are 3 x 3 where the original image
dimensions are 2 x 2. So it means that the dimensions of the new image are based on the following
formula
2(number of rows – 1) X 2(number of columns – 1)

One of the advantage of this zooming technique , that it does not create as blurry picture as
compare to the nearest neighbor interpolation method. But it also has a disadvantage that it can
only run on the power of 2. It can be demonstrated here.

## Reason behind twice zooming:

Consider the above image of 2 rows and 2 columns. If we have to zoom it 6 times , using zero order
hold method , we can not do it. As the formula shows us this. It could only zoom in the power of 2 2,4,8,16,32 and so on.
Even if we try to zoom it, we can not. Because at first when we will zoom it two times, and the
result would be same as shown in the column wise zooming with dimensions equal to 3×3. Then
you will zoom it again and you will get dimensions equal to 5 x 5. Now if we will do it again, we will
get dimensions equal to 9 x 9.
Whereas according to the formula of yours the answer should be 11×11. As 6(2 minus 1) X 6(2
minus 1) gives 11 x 11.

## Introduction:

K times is the third zooming method we are going to discuss. It is one of the most perfect zooming
algorithm discussed so far. It caters the challenges of both twice zooming and pixel replication. K
in this zooming algorithm stands for zooming factor.

## Working:

It works like this way. First of all , you have to take two adjacent pixels as you did in the zooming twice. Then you have to subtract the smaller from the greater one. We call this output OP.
Divide the output OP with the zooming factor K. Now you have to add the result to the smaller value
and put the result in between those two values.
Add the value OP again to the value you just put and place it again next to the previous putted
value. You have to do it till you place k-1 values in it.
Repeat the same step for all the rows and the columns , and you get a zoomed images.

## For example:

Suppose you have an image of 2 rows and 3 columns , which is given below. And you have to zoom
it thrice or three times.
15 30 15
30 15 30
K in this case is 3. K = 3.
The number of values that should be inserted is k-1 = 3-1 = 2.

## Row wise zooming

Take the first two adjacent pixels. Which are 15 and 30.
Subtract 15 from 30. 30-15 = 15.
Divide 15 by k. 15/k = 15/3 = 5. We call it OP. where op is just a name
Add OP to lower number. 15 + OP = 15 + 5 = 20.

Add OP to 20 again. 20 + OP = 20 + 5 = 25.
We do that 2 times because we have to insert k-1 values.
Now repeat this step for the next two adjacent pixels. It is shown in the first table.
After inserting the values , you have to sort the inserted values in ascending order, so there
remains a symmetry between them.
It is shown in the second table

Table 1.
15 20 25 30 20 25 15
30 20 25 15 20 25 30
Table 2.

## Column wise zooming

The same procedure has to be performed column wise. The procedure include taking the two
adjacent pixel values, and then subtracting the smaller from the bigger one. Then after that , you
have to divide it by k. Store the result as OP. Add OP to smaller one, and then again add OP to the
value that comes in first addition of OP. Insert the new values.
Here what you got after all that.
15 20 25 30 25 20 15
20 21 21 25 21 21 20
25 22 22 20 22 22 25
30 25 20 15 20 25 30

## New image size

The best way to calculate the formula for the dimensions of a new image is to compare the
dimensions of the original image and the final image. The dimensions of the original image were 2
X 3. And the dimensions of the new image are 4 x 7.

The formula thus is:
K(number of rows minus 1 + 1) X K(number of cols minus 1 + 1)

The one of the clear advantage that k time zooming algorithm has that it is able to compute zoom
of any factor which was the power of pixel replication algorithm , also it gives improved result
Less blurry which was the power of zero order hold method. So hence It comprises the power of the
two algorithms.
The only difficulty this algorithm has that it has to be sort in the end , which is an additional step ,
and thus increases the cost of computation.

## Pixels per inch.

Pixel density or Pixels per inch is a measure of spatial resolution for different devices that includes
tablets , mobile phones.
The higher is the PPI , the higher is the quality. In order to more understand it, that how it
calculated. Lets calculate the PPI of a mobile phone.

## Calculating pixels per inch PPI

If PPI or pixel density of 441. But how does it is calculated?
First of all we will Pythagoras theorem to calculate the diagonal resolution in pixels. It can be given as:
Where a and b are the height and width resolutions in pixel and c is the diagonal resolution in pixels.
If , it is 1080 x 1920 pixels.
So putting those values in the equation gives the result: C = 2202.90717

Now we will calculate PPI
PPI = c / diagonal size in inches
If the diagonal size in inches is 5.0 inches , which can be confirmed from
anywhere.
PPI = 2202.90717/5.0
PPI = 440.58
PPI = 441 approx
That means that the pixel density is 441 PPI.

## Dots per inch.

The dpi is often relate to PPI , whereas there is a difference between these two. DPI or dots per inch
is a measure of spatial resolution of printers. In case of printers , dpi means that how many dots of
ink are printed per inch when an image get printed out from the printer.
Remember , it is not necessary that each Pixel per inch is printed by one dot per inch. There may
be many dots per inch used for printing one pixel. The reason behind this that most of the color
printers uses CMYK model. The colors are limited. Printer has to choose from these colors to make
the color of the pixel whereas within pc , we have hundreds of thousands of colors.
The higher is the dpi of the printer , the higher is the quality of the printed document or image on
Paper. Usually some of the laser printers have dpi of 300 and some have 600 or more.

## Lines per inch.

When dpi refers to dots per inch, liner per inch refers to lines of dots per inch. The resolution of
halftone screen is measured in lines per inch.
The following table shows some of the lines per inch capacity of the printers.

## Application of Digital image processing

• Image sharpening and restoration
• Medical field
• Remote sensing
• Transmission and encoding
• Machine/Robot vision
• Color processing
• Pattern recognition
• Video processing
• Microscopic Imaging & Others.

## How to Read an Image

If we want to read an Image, which is Present in Matlab directory.

```I = imread('coins.png');
imshow(I);

```

If user wants to read an Image which is not Present in Matlab directory.

``` [filename,pathname]=uigetfile(‘*.*’);
I=strcat(pathname,filename);
imshow(I);

```

## How to Write an Image

```I = imread('coins.png'); %Portable Network Graphic

imwrite(I,’coin.tif’); %this command will write the Image in Tagged Image File Format in  current directory

imwrite(I,’coin.jpg’); %this command will write the Image in Joint Photographic Experts Group format in current directory

imwrite(I,’coin.bmp’);%this command will write the Image in bitmap format in current directory

```