DIGITAL IMAGE PROCESSING TUTORIAL PART 1 : HOW TO READ IMAGE

In this tutorial blog i will show you how to read an image in Matlab. The procedure is very simple. The persons who have never used Matlab, i will request them to see my blog post on Basic Matlab tutorial or any other tutorial on the same topic from different authors.

 

I prefer to write all my codes in the “m file” of Matlab. See the pictures below to show you how to make an “m file”

FIG 1 : CLICK HERE TO CREATE “m FILE”

 

Fig 2: save the file with a valid file name

 

Fig 3: Write the codes in the “m File”

 

THE CODE TO READ IMAGE AND DISPLAY IT.

% Code 1. read image
rgb1=imread('paper flower.jpg');
figure, imshow(rgb1)

Any statement written with “%” symbol are commented out by Matlab. These are not complied . They are used to explain the steps taking place for the user.

“paper flower.jpg” is the name of my image which is present in my current directory. Change it to the name of your own picture.

Fig 4: run the code

 

” imshow( ) ” is used to display the image.

Even if the image is grayscale image , “imread()” function is smart enough to understand that.

The image is read as a matrix. Thats why i have written

rgb1=imread('paper flower.jpg');

where “rgb1” is the matrix on which the image data has been saved. You can give any name to the variable but remember:

1.Donot start variable name with number

2.Donot give space in between the name. Underscore is allowed but not space. Eg. “my image is rgb” , this variable name is NOT VALID.

but, “my_name_is_rgb” is a valid name.

Try to give such type of variable name which are relevant to the operation you are performing.

In my next blog i will show you how can you read an index image. Its little different from the normal method.

As a reference i will encourage you to study  regarding what is RGB image , Grayscale image and Index image.

Almost all my blog will deal with RGB and/or Grayscale image still its important that you know what is an “indexed image”.

 

Thank you for reading.

BASIC MATLAB TUTORIAL: PART 3

Its the part 3 of my BASIC MATLAB TUTORIAL.

1.How to display any statements ?  For that we will use the ” disp() “function. Anything we write on the display fuction gets displayed. Eg.

>> disp('My name is Mousam Choudhury')
My name is Mousam Choudhury
>> a=magic(2);
>> disp(a)
1      3
4      2

2.How to see a particular Row of matrix or a particular column of a matrix? For it we will use the colon symbol : ” .

if we want to see all the row values of a particular matrix , then we put colon on the row part and the column no. on the column part. See below

>> a=magic(4)

a =

16     2     3     13
5     11    10      8
9      7     6     12
4     14    15      1

>> disp('see 2nd column and all row values')
see 2nd column and all row values
>> a(:,2) # 1st part is row and 2nd part is column

ans =

2
11
7
14
>> disp('see 4th column and all row values')
see 4th column and all row values
>> a(:,4)

ans =

13
8
12
1

To see all the column values of a particular row , then put colon on the column part and the row no. on the row part

>> disp('see 3rd row and all column values')
see 3rd row and all column values
>> a(3,:)

ans =

9   7   6   12

To see a particular value of a row and column , then just replace the row and column part with that value. Eg.

>> disp('see 3rd row and 4th column values')
see 3rd row and 4th column values
>> a(3,4)

ans =

12

>> disp('see 2nd row and 2nd column values')
see 2nd row and 2nd column values
>> a(2,2)

ans =

11

3.How to distribute a range in step? For this we need the minimum value , the step size and the maximum value. See the below examples:

>> disp('DISTRIBUTE A RANGE BETWEEN 1 TO 4 IN STEP SIZE 0.5')
DISTRIBUTE A RANGE BETWEEN 1 TO 4 IN STEP SIZE 0.5
>> distribute_a_range=1:0.5:4

distribute_a_range =

1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000
>> disp('DISTRIBUTE A RANGE BETWEEN 11 TO 14 IN STEP SIZE 1')
DISTRIBUTE A RANGE BETWEEN 11 TO 14 IN STEP SIZE 1
>> distribute_a_range=11:1:14

distribute_a_range =

11   12   13   14

So by changing the step size

4.How to make a column matrix from a normal (row x column) matrix ?  Suppose i want to make a 3*3 matrix into a column matrix having the values from the 3*3 matrix, we can use the following method. Its basically using the “:” symbol again. See the below examples.

>> a=[1 2 3;4 5 6;7 8 9]

a =

1   2   3
4   5   6
7   8   9

>> a_column=a(:)

a_column =

1
4
7
2
5
8
3
6
9

As you can see the value were taken column after column i.e. 2nd columnwas added after 1st coulmn and the 3rd column after the second.

In order to get the values row wise, we have to use the following tweak. See below.

>> a_RowWise_column=reshape(a.',[],1)

a_RowWise_column =

1
2
3
4
5
6
7
8
9

Note: Use the variable names in such a way that they represent the operation which you are doing. like the above one, “a_RowWise_column” , because i am taking the values row wise but the output is a column matrix of those values.

5.Can we get back the original matrix from the column matrix? Yes , we can. Just see the code to do so below.

>> a_original=reshape(a_column,3,3)

a_original =

1   2   3
4   5   6
7   8   9

NOTE: “reshape” is an in built function. Read more about it in the MATLAB help section. Since the original matrix was 3*3 , so i wrote 3,3 in the parenthesis.

LET US SEE ANOTHER EXAMPLE

MAKE A 4*4 MATRIX

>> a=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]

a =

1    2    3    4
5    6    7    8
9   10   11   12
13  14   15   16

MAKE A COLUMN MATRIX OF THE ABOVE 4*4 MATRIX

>> a_column=a(:)

a_column =

1
5
9
13
2
6
10
14
3
7
11
15
4
8
12
16

MAKE THE ORIGINAL MATRIX FROM THE ABOVE COLUMN MATRIX

>> a_original=reshape(a_column,4,4)

a_original =

1    2    3    4
5    6    7    8
9   10   11   12
13  14   15   16

 

THANK YOU FOR READING AND ENCOURAGING ME TO CONTRIBUTE THE KNOWLEDGE THAT I HAVE.