0% found this document useful (0 votes)
30 views

Dip 03

This document discusses digital image processing in MATLAB. It provides instructions on how to [1] read images into MATLAB, [2] display images using functions like imshow and subplot, and [3] write images out of MATLAB. Key functions covered include imread to read images, imshow to display single or multiple images, and imwrite to write images. Pixels can be accessed and manipulated individually using matrix subscripting. Lab tasks involve building a matrix to represent an image and writing code to read an image and display it using various commands.

Uploaded by

Noor-Ul Ain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Dip 03

This document discusses digital image processing in MATLAB. It provides instructions on how to [1] read images into MATLAB, [2] display images using functions like imshow and subplot, and [3] write images out of MATLAB. Key functions covered include imread to read images, imshow to display single or multiple images, and imwrite to write images. Pixels can be accessed and manipulated individually using matrix subscripting. Lab tasks involve building a matrix to represent an image and writing code to read an image and display it using various commands.

Uploaded by

Noor-Ul Ain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

DIGITAL IMAGE PROCESSING

Experiment 3
Introduction to Digital Image Processing with MATLAB

CLO 1: Construct the experiments/projects of varying complexities

CLO 2: Use modern tools and languages.

CLO 3: Demonstrate an original solution of problem under discussion.

CLO 4: Work individually as well as in teams.


LAB OBJECTIVE:

The objective of this lab is to understand.

1. How to read images in MATLAB

2. How to write images in MATLAB

3. How to display images in MATLAB

4. How to get no. of rows and columns of image

1. Reading Image Data

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. If the file is not in the current directory
or in a directory in the MATLAB path, specify the full pathname of the location on your system.

Example 1 :
% Read a sample image.
A = imread('ngc6543a.jpg');
% Display the image.
image(A)
2. Displaying Multiple Images in the Same Figure
You can use the imshow function with the MATLAB subplot function or the MATLAB subimage
function to display multiple images in a single figure window.
Dividing a Figure Window into Multiple Display Regions.
subplot divides a figure into multiple display regions. The syntax of subplot is
subplot(m,n,p)
This syntax divides the figure into an m-by-n matrix of display regions and makes the pth display
region active.

Note When you use subplot to display multiple color images in one figure window, the images must
share the colormap of the last image displayed. In some cases, as illustrated by the following example,
the display results can be unacceptable. As an alternative, you can use the subimage function,

For example, you can use this syntax to display two images side by side.
X1=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1),imshow(X1);
subplot(1,2,2),imshow(X2);
In the figure, note how the first image displayed, X1, appears dark after the second image is displayed.
Two Images in Same Figure Using the Same Colormap

Using the subimage Function to Display Multiple Images.


subimage converts images to truecolor before displaying them and therefore circumvents the colormap
sharing problem. This example uses subimage to display the forest and the trees images with better
results.
 [X1,map1]=imread('forest.tif');
 [X2,map2]=imread('trees.tif');
 subplot(1,2,1), subimage(X1,map1)
 subplot(1,2,2), subimage(X2,map2)
Image Display
Image create and display image object
Imagesc scale and display as image
Imshow display image
Colorbar display colorbar
Getimage get image data from axes
Truesize adjust display size of image
Zoom zoom in and zoom out of 2D plot

2. Writing Image Data


Imwrite
Write image to graphics file
Syntax
imwrite(A,filename,fmt)
Writes image data A to the file specified by filename, and the
format specified by fmt.
imwrite creates the new file in your current folder.
writes the image in the format specified by fmt
Example1:
A = rand(50); % returns an n-by-n matrix of random numbers.
imwrite(A,'myGray.png')
%Write a 50-by-50 array of grayscale values to a PNG file in the current folder.
imshow('myGray.png')

Example 2:
a=imread('pout.tif');
imwrite(a,gray(256),'b.bmp');%write image image data of a to image b.bmp
imshow('b.bmp') % imshow is used to display image

3. How to build a matrix (or image)?


Example1 :
row = 256;
col = 256;
img = zeros(row, col);
img(100:105, :) = 0.5;
img(:, 100:105) = 1;
figure;imshow(img);
3.How to get no. of rows and columns of image
Function size gives the rows and columns dimension of image
[r,c]=size(a)

r=

291

c=

240

4. Image Display
imshow(f,g) % f is an image array & g is no of intensity levels used to display it
imshow('pout.tif') % here by default intensity level is 256
pixval % displays the Euclidean distance between the initial and current cursor conditions

imshow(f,[low high])
Displays as black all values less than or equal to low and as white all values greater than or equal to
high
Example::
I = imread('cameraman.tif');
h = imshow(I,[0 80]);
5. 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) %returns the value of the pixel at row 2, column 15 of the image A.

Lab tasks:

1: Build a matric/image of the shape given below

2- Write a MATLAB code that will do the following


 Read any gray scale/rgb image.
 Display that image using the commands given below
i imagesc
ii imshow
iii colorbar
iv getimage
v truesize
vi zoom

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy