Nothing Special   »   [go: up one dir, main page]

DIP - 2025 - Matlab-123

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

29/01/1446

Biomedical Image Processing


(BIP)
Matlab Lab
By: Mohammed Faisal

Matlab

1
29/01/1446

Basic Operations on Digital Images Using


MATLAB
 Reading an Image
 To import an image from any supported graphics image file format, in any of the
supported bit depths, use the imread function.
 Syntax
A = imread (filename.fmt)
 Description
 A = imread(filename.fmt) reads a greyscale or color image from the file specified
by the string filename, where the string fmt specifies the format of the file.

Basic Operations on Digital Images Using MATLAB

 Display an Image
 To display image, use the imshow function.
 Syntax
imshow(A)
 Description
 imshow(A) displays the image stored in array A.
 Writing Image Data
 Imwrite write image to graphics file
 Syntax
imwrite(A,filename,fmt)

2
29/01/1446

Basic Operations on Digital Images Using MATLAB

 Example of Images Read:


 S=imread('cameraman.tif');%Read in intensity image
 imshow(S); %display image using imshow

Basic Operations on Digital Images Using MATLAB


Read a Colour Images in Matlab
 Matlab handles 24-bit RGB images in much the same way as greyscale. We
can save the colour values to a matrix and view the result: it is called a colour
model.
 Example1
 a=imread('autumn.tif');
 figure,
 imshow(a)
 figure, imagesc(a); %display image using imagesc, the data is scaled to use the full
colormap

3
29/01/1446

Basic Operations on Digital Images Using MATLAB


Read a Colour Images in Matlab
d=imread('C:\Users\faisal\Desktop\mohamed.jpg','jpg');
figure
image(d) • Display the image in the figure window using image().
• title (‘’) Place a title at the top.
title('string')
• Put a label under the x-axis using xlabel().
xlabel(' string ') • ylabel() puts a label to the left of the yaxis, oriented 90°
truesize from horizontal

• truesize makes the figure window fit the image


– unless the images is too large for the display in
which case it is resized to fit but with the correct
aspect ratio.
7

Information about graphics file


 A great deal of information can be obtained with the imfinfo function.
 For example:

 imfinfo('cameraman.tif')
Exampe 2
 h=imshow('bag.png');
 imageinfo(h)

4
29/01/1446

Pixel Information Tool


 impixelinfo function use to creates a pixel information tool in the current figure.
 Example1
 imshow('cameraman.tif');
 impixelinfo;
 Example2
 B=imread('spine.tif');
 imtool (B);

Accessing the Pixel Data


 There is a one-to-one correspondence between pixel coordinates and the
coordinates MATLAB uses for matrix subscripting.
 This correspondence makes the relationship between an image's data matrix
and the way the image is displayed easy to understand.
 For example, the data for the pixel in the fifth row, second
column is stored in the matrix element (5,2).
 You use normal MATLAB matrix subscripting to access
values of individual pixels.
 For example, the MATLAB code
A(2,15)

10

5
29/01/1446

Accessing the Pixel Data


 B=imread('spine.tif');
 B(25,50) %Print pixel value at location (25,50)

 B(25,50)=255; %Set pixel value at (25,50) to white

11

Accessing the Pixel Data

 D=imread('onion.png');
 imshow(B); %View resulting changes in image
 D(25,50,:) %Print RGB pixel value at location (25,50)
 D(25,50, 1) %Print only the red value at (25,50)

 D(25,50,:)= (255); %Set pixel value to RGB white


 figure,
 imshow(D); %View resulting changes in imag

12

6
29/01/1446

RGB colour image


Example
 C=imread('onion.png'); %Read in 8 bit RGB colour image.
 Cred=C(:,:,1); %Extract red channel (first channel)
 Cgreen=C(:,:,2); %Extract green channel (second channel)
 Cblue=C(:,:,3); %Extract blue channel (third channel)
 subplot(2,2,1); %Display all in 2 x 2 plot
 imshow(C); axis image;
 subplot(2,2,2); imshow(Cred); title('red'); %Display and label
 subplot(2,2,3); imshow(Cgreen); title('green');
 subplot(2,2,4); imshow(Cblue); title('blue');
13

Homework
 Using the examples presented for displaying an image in Matlab
together with those for accessing pixel locations,
 investigate adding and subtracting a scalar value from an
individual location, i.e. I(i; j) = I(i; j)+ 25 or I(i; j) = I(i; j)-25.
Start by using the grey-scale example image and pixel
location(100; 20).
 What is the effect to n the greyscale colour of adding and
subtracting?
14

7
29/01/1446

Basic operations on digital images(cont’)


Crop the Image
d=imread('C:\Users\faisal\Desktop\mohamed.jpg','jpg');
figure
image(d)
title('string')
xlabel(' string ')
truesize
t=d(400:2000,1000:2800,:);
figure • t = d(first row:last row, first column:last column,
all bands) ‘:’ alone means ‘all bands’ here. In the
image(t) 1st position, ‘:’ means ‘all rows’; in the 2nd it
truesize means ‘all columns’

15

 Crop the Image


 Syntax:
 J=imcrop(I);
 Example:
 I = imread('circuit.tif');
 imshow (I) ;
 j = imcrop(I);
 imshow(j);

16

8
29/01/1446

Crop the Image


 Example2:
 I = imread('circuit.tif');
 imshow (I) ;
 H=imcrop (I, [60, 40, 100, 90]);
 imshow(H);

17

Basic operations on digital images


Converting Image Types
 Matlab also contains built in functions for converting different image types.
 Here, we examine conversion to grey scale and the display of individual RGB
colour channels from an image.
 Example
 D=imread('onion.png'); %Read in 8 bit RGB colour image
 Dgray=rgb2gray(D); %Convert it to a grey scale image
 subplot(2,1,1); imshow(D); %Display both side by side
 subplot(2,1,2); imshow(Dgray);

18

9
29/01/1446

Basic operations on digital images


Converting Image Types
 Example
 x=imread('C:\Users\faisal\Desktop\mohamed.jpg');
 imshow(x);
 y=im2bw(x,0.4);
 imshow(y);

19

Basic operations on digital images


Data types and conversions
 c=imread('onion.png');
 cd=double(c);
 imshow(c),
 figure,imshow(cd)

21

10
29/01/1446

Basic operations on digital images


Data types and conversions

 c=imread('onion.png');
 cd=double(c);
 imshow(c),
 figure,imshow(cd/255)

22

Basic operations on digital images


 Mirror Image Generation:
Horizontally flipping an image is called mirroring an image. This can be done
using the following function:
 Syntax:
fliplr(Image)
 Or flip(Image,2)
The resultant image can be allocated to a variable.
 Inverting an Image:
 Vertically flipping an image is called inverting an image. This can be done using
the following function:
Syntax:
flip(Image)
 The resultant image can be allocated to a variable.

23

11
29/01/1446

Image Rotation
 Rotating an Image:
Imrotate function used to rotate a grey scale or RGB Image.
Syntax:
B = imrotate(A,angle)
The value of angle is given in degrees and the image rotates in anti-clockwise
direction according to the angle.
 % Rotation
 I=imread('onion.png');
 K=imrotate(I,60);
 subplot(2,1,1); imshow(K); title('Rotated Image 60deg');
 R=imrotate(I,45);
 subplot(2,1,2); imshow(R); title('Rotated Image 45deg');

24

Resized images by different methods


 IMRESIZE(A, [NUMROWS NUMCOLS], METHOD),
 METHOD can be a string naming a general interpolation method:
 'nearest' - nearest-neighbor interpolation
 'bilinear' - bilinear interpolation
 'bicubic' - cubic interpolation; the default method
 A = imresize(I, [128,128],'nearest');
 B = imresize(I, [16,16],'nearest');
 Example:
 I = imread('circuit.tif');
 J = imresize(I, [128,128],'nearest'); figure,
 imshow(I); figure,
 imshow(J)
25

12
29/01/1446

Scaling
 Example of image scaling
 I = imread(‘circuit.tif’);
 J = imresize(I,1.25);
 figure,
 imshow(I);
 figure,
 imshow(J)

26

Implementation of Relationships between Pixels


Neighbour of 4,8 and Diagonal point
% To find Neighbour of a given Pixe
 a=magic(5);
 disp('a='); disp(a);
 b=input('Enter the number of row < ');
 c=input(' Enter the number of Column < ');
 disp('Element'); disp(a(b,c));
 % 4 Point Neighbour
 N4=[a(b+1,c), a(b-1,c), a(b,c+1), a(b,c-1)];
 disp('N4='); disp(N4);
 %8 Point Neighbour
 N8=[a(b+1,c), a(b-1,c), a(b,c+1), a(b,c-1), a(b+1,c+1), a(b+1,c-1), a(b-1,c-1), a(b-1,c+1)];
 disp('N8='); disp(N8);
 %Diagonal Neighbour
 ND=[ a(b+1,c+1), a(b+1,c-1), a(b-1,c-1), a(b-1,c+1)];
 disp('ND='); disp(ND); 30

13
29/01/1446

Spatial and Intensity Resolution of Images


 Spatial resolution Spatial resolution can be defined as the smallest discernible
detail in an image. In other way spatial resolution is defined as the number of
independent pixels values per inch. Since the spatial resolution refers to clarity,
so for different devices, different measure has been made to measure it.
 For example
 Dots per inch Dots per inch or DPI is usually used in monitors.
 Lines per inch Lines per inch or LPI is usually used in laser printers.
 Pixel per inch Pixel per inch or PPI is measure for different devices such as tablets
, Mobile phones e.t.c.

31

Spatial and Intensity Resolution of Images


 PROGRAM-Spatial Resolution
 z=imread('cameraman.tif');
 z=imresize(z,[1024,1024]);
 [r c]=size(z);
 l=1;
 for i=1:2:r
 k=1;
 for j=1:2:c
 a(l,k)=z(i,j);
 k=k+1;
 end
 l=l+1;
 end
32

14
29/01/1446

Spatial Resolution of Images


 l=1;
 for i=1:4:r
 k=1;
 for j=1:4:c
 b(l,k)=z(i,j);
 k=k+1;
 end
 l=l+1;
 end
 l=1;
 for i=1:8:r
 k=1;
 for j=1:8:c
 e(l,k)=z(i,j);
 k=k+1;
 end
 l=l+1;
 end 33

Spatial Resolution of Images


 l=1;
 for i=1:16:r
 k=1;
 for j=1:16:c
 d(l,k)=z(i,j);
 k=k+1;
 end
 l=l+1;
 end
 subplot(2,2,1),imshow(a)
 subplot(2,2,2),imshow(b)
 subplot(2,2,3),imshow(e)
 subplot(2,2,4),imshow(d) 34

15

You might also like