DIP Final Manual-2014

Embed Size (px)

Citation preview

  • CONTENTS

    DIGITAL IMAGE PROCESSING LAB EE-792D

    Credit: 2 Contact: 3P

    1. READ AND DISPLAY DIGITAL IMAGES

    2. CONTRAST STRETCHING, FLIPPED IMAGE AND NEGETIVE IMAGE

    3. IMAGE ARITHMATIC OPERATIONS

    4. HISTOGRAM EQUALIZATION

    5. LINEAR AND NONLINEAR FILTERING

    6. EDGE DETECTION OF AN IMAGE USING OPERATORS

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    INTRODUCTION

    An image may be defined as a two-dimensional function, ( , )f x y , where x and y are spatial coordinates,

    and the amplitude of f at any pair of coordinates ( , )x y is called the intensity or gray level of the image at that

    point. When ,x y and the amplitude values of f are all finite, discrete quantities, we call the image a digital image.

    The field of digital image processing refers to processing digital images by means of a digital computer. Note that a

    digital image is composed of a finite number of elements, each of which has a particular location and value. These

    elements are referred to as pixels.

    A digital image can be represented naturally as a matrix:

    (1,1) (1,2) ... (1, )

    (2,1) (2,2) ... (2, )

    ... ... ... ...

    ( ,1) ( , 2) ... ( , )

    f f f N

    f f f Nf

    f M f M f M N

    We use letters M and N , respectively, to denote the number of rows and columns in a matrix.

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    EXPERIMENT 01:

    TITLE: READ AND DISPLAY DIGITAL IMAGES

    OBJECTIVE: To read and display digital images using matlab

    1.1 THEORY:

    The imread function reads an image from any supported graphics image file format. The syntax is:

    imread(filename);

    Here filename is a string containing the complete name of the image file.

    Example 1.1:

    f = imread('moon.tif') ; %reads the JPEG image moon into image array f.

    imshow(f) ; %display the image in a matlab figure window.

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    1.2 Read and display images in medical file format:

    To read image data from a DICOM file, use the dicomread function. To view the image data imported

    from a DICOM file, use one of the toolbox image display functions imshow or imtool. Note, however, that

    because the image data in this DICOM file is signed 16-bitdata, you must use the auto scaling syntax with

    either display function to make the image viewable.

    Example 1.2:

    1.3 Read and display Colour images:

    An RGB Colour image is an M x N x 3 array of Colour pixels, where each Colour pixel is a triplet

    corresponding to the red, green and blue components of an RGB image at a specific spatial location. An

    RGB image may be viewed as a stack of three gray scale images that, when fed into the red, green, and

    blue inputs of a Colour monitor, produce a Colour image on the screen. By convention, the three images

    forming an RGB Colour image are referred to as the red, green, and blue component images. The RGB

    I = dicomread('CT-MONO2-16-ankle.dcm'); %reads the .dcm image into image array I.

    imshow(I,'DisplayRange',[]); %display the image in a matlab figure window.

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Colour space usually is shown graphically as an RGB Colour cube, as depicted in figure. The vertices of

    the cube are the primary (red, green and blue) and secondary (cyan, magenta and yellow) Colours of light.

    Colour images can also be read by imread function.

    Example 1.3:

    1.4 Converting Colour images into gray scale images:

    Colour images can be converted into gray scale images by function rgb2gray.

    I = imread('football.jpg'); %reading the colour image

    Imshow(I); %showing the image

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Example 1.4:

    1.4 Converting colour images into binary images:

    Colour images can be converted into binary images by function im2bw.

    Example 1.4:

    clear all; close all; clc;

    I = imread('football.jpg'); grayfootballimage=rgb2gray(I); subplot(1,2,1),imshow(I),title('original Colour image'); subplot(1,2,2),imshow(grayfootballimage),title('gray image');

    clear all; close all; clc;

    I = imread('football.jpg'); bwfootballimage=im2bw(I); subplot(1,2,1),imshow(I),title('original gray image'); subplot(1,2,2),imshow(I),title('binary image');

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    1.5 Image resizing:

    Image resizing can be done by the following command:

    imresize(image,[parameters]);

    Example 1.5:

    Assignment 1.1:

    Read the image football.jpg. Find size of the image. Resize it to 150x150.

    clear all; close all; clc;

    I=imread('rice.png'); Isize=size(I); Iresize=imresize(I,[150 150]); figure,imshow(I),title('original image'); figure,imshow(Iresize),title('resize image');

    original image

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    EXPERIMENT 2:

    TITLE: CONTRAST STRETCHING, FLIPPED IMAGE AND NEGETIVE IMAGE

    OBJECTIVE: To write and execute image processing methods to obtain negative image, flip image.

    2.1 THEORY:

    In threshold operation, pixel value greater than threshold value is made white and pixel value less than

    threshold value is made black. If we consider image having gray levels r and if grey levels in output image is

    s then,

    0s if r m

    255s if r m , where m is threshold.

    Negative image can be obtained by subtracting each pixel value from 255.

    Contrast stretching means darkening level below threshold value and whitening level above threshold value. This

    technique will enhance contrast of a given image. Threshold is example of extreme contrast stretching.

    Example 2.1: Thresholding (Extreme contrast stretching)

    % threshold operation close all; clear all; clc; y=imread('football.jpg');%read original image if ndims(y)==3 z=rgb2gray(y);%if the image is a Colour image transform it to gray end T=input('enter threshold value between 0 to 255:');%enter threshold [m,n]=size(z);%size of gray image for i=1:m for j=1:n if z(i,j)>T x(i,j)=255; else x(i,j)=0; end end end figure,imshow(y),title('original image');%display original image figure, imshow(z),title('gray scale image');%display gray image figure,imshow(x),title('threshold image');%display threshold image

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Assignment 2.1:

    Read the image cameraman.tif.Run the above program for different threshold values and find out optimum threshold value for which you are getting better result.

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Example 2.2: flip the given image horizontally

    %flip image horizontally clear all; close all; clc; z=imread('cameraman.tif');%read original image [m,n]=size(z);%size of gray image for i=1:m for j=1:n horizontal_flipped_image(i,j)=z(i,n+1-j);%horizontal flip image end end subplot(1,2,1),imshow(z),title('original image'); subplot(1,2,2),imshow(horizontal_flipped_image),title('horizontal flipped image');

    original image horizontal flipped image

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Assignment 2.2: Read the image football.jpg and modify the program for getting vertical flipping.

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Example 2.3: Obtain negative image

    % negative image clear all; close all; clc; z=imread('cameraman.tif');%read original image [m,n]=size(z);%size of gray image for i=1:m for j=1:n negative_image(i,j)=255-z(i,j);%generate horizontal flip image end end subplot(1,2,1),imshow(z),title('original image'); subplot(1,2,2),imshow(negative_image),title('negative image');

    original image negative image

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Assignment 2.3: Read the image football.jpg and obtain the negative image.

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Example 2.4: Contrast stretching using three slopes and two threshold values T1 and T2

    %contrast stretching using three slopes and two threshold values close all; clear all; clc; z=imread('cameraman.tif');%read original image T1=input('enter threshold value between 0 to 255:');%enter threshold T2=input('enter threshold value between 0 to 255:');%enter threshold [m,n]=size(z);%size of gray image for i=1:m for j=1:n if z(i,j)T1&&z(i,j)T2 newimage(i,j)=z(i,j); end end end subplot(1,2,1), imshow(z),title('original image');%display gray image subplot(1,2,2),imshow(newimage),title(contrast stretching');%contrast image

    original image contrast stretching

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    1. Read the image cameraman.tif. Apply contrast stretching using three slopes and two suitable threshold values T1 and T2.

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    EXPERIMENT 3:

    TITLE: IMAGE ARITHMATIC OPERATIONS

    OBJECTIVE: To write and execute programs for image arithmetic operations like addition, subtraction

    and complement

    3.1 THEORY: If there are two images 1I and 2I then addition of image can be given by:

    1 2( , ) ( , ) ( , )I x y I x y I x y

    Where ( , )I x y is resultant image due to addition of two images. ,x y are coordinates of image. Image addition is

    pixel to pixel. Value of pixel should not cross maximum permissible value that is 255 for grey scale image. When it

    exceeds value 255, it should be clipped to 255.

    Exercise 3.1: Image addition and subtraction

    clear all;close all;clc; x=imread('cameraman.tif');%read original image y=imread('rice.png'); addition_image=imadd(x,y); subtraction_image=imsubtract(x,y); subplot(2,2,1),imshow(x),title('original image I1'); subplot(2,2,2),imshow(y),title('original image I2'); subplot(2,2,3),imshow(addition_image),title('addition of image I1+I2'); subplot(2,2,4),imshow(subtraction_image),title('subtraction of image I1-I2');

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Assignment 3.1: Add and subtract two images 'coins.png' and rice.png without using standard matlab

    functions.

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    EXPERIMENT 4:

    TITLE: HISTOGRAM EQUALIZATION

    OBJECTIVE: To write programs for generating and plotting image histograms and equalizations

    4.1 THEORY: The histogram of a digital image with L total possible intensity levels in the range [0, ]G is

    defined as the discrete function ( )k kh r n where, kr is the kth intensity level in the interval [0, ]G and kn is the

    number of pixels in the image whose intensity level is kr . The value of G is 255 for images of class unit8, 65535

    for images of class unit16, and 1.0 for images of class double. Keep in mind that indices in MATLAB cannot be 0,

    so 1r corresponds to intensity level 0, 2r corresponds to intensity level 1, and so on, with Lr corresponding to level

    G . Note also that, 1G L for images of class unit8 and unit16.

    Histogram equalization is a technique for adjusting image intensities to enhance contrast. Let ( ), 1,2,..., ,r jp r j L

    denote the histogram associated with the intensity levels of a given image. For discrete quantities we work with

    summations, and the equalization transformation becomes

    1 1

    ( ) ( )k k

    jk k r j

    j j

    ns T r p r

    n

    for 1,2,...,k L where ks is the intensity value in the output image

    corresponding to value kr in the input image.

    Example 4.1: Histogram equalization

    %histogram equalization

    close all;clear all;clc; z=imread('football.jpg');%read original image y=rgb2gray(z);%gray scale image new_image=histeq(y);%histogram equalized image subplot(2,2,1),imhist(y),title('histogram of original image'); subplot(2,2,2),imhist(new_image),title('histogram of new image'); subplot(2,2,3),imshow(y),title('original image'); subplot(2,2,4),imshow(new_image),title('histogram equalized image');

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Assignment 4.1: Read the image cameraman.tif'. If the image is a color image change it to a gray image.

    Obtain histogram of the gray image using and without using standard MATLAB functions. Obtain

    histogram equalization using & without using standard MATLAB functions.

    0 100 200

    0

    500

    1000

    histogram of original image

    0 100 200

    0

    500

    1000

    1500

    histogram of new image

    original image histogram equalized image

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    EXPERIMENT 5:

    TITLE: LINEAR AND NONLINEAR FILTERING

    OBJECTIVE: To write programs for generating linear and nonlinear filtered image

    5.1 THEORY:

    Linear Filtering: Linear operations consist of multiplying each pixel in the neighbourhood by a

    corresponding coefficient and summing the results to obtain the response at each point ( , )x y . If the

    neighbourhood is of size m x n, [mn] coefficients are required. These coefficients are arranged as a matrix, called a

    filter, mask etc.

    Nonlinear Filtering: Nonlinear spatial filtering is based on neighbourhood operations also, and the mechanics of

    defining m x n neighbourhood by sliding the center point through an image are the same as linear filtering.

    However, whereas linear spatial filtering is based on computing the sum of products (which is a linear operation),

    nonlinear spatial filtering is based as the name implies, on nonlinear operations involving the pixels of a

    neighbourhood.

    Example 5.1: Linear Spatial Filter

    % linear filter with matlab command close all; clear all; clc;

    f=imread('moon.tif');%read image f=im2double(f); w4=fspecial('laplacian',0);%laplacian filter with -4 at centre w8=[1 1 1;1 -8 1;1 1 1];%laplacian filter with -8 at centre g4=f-imfilter(f,w4,'same','conv');%same size and convolution g8=f-imfilter(f,w8,'same','conv');%same size and convolution subplot(2,2,1),imshow(f),title('original image'); subplot(2,2,2),imshow(g4),title('filtered image by laplacian filter with -4 at

    center'); subplot(2,2,3),imshow(f),title('original image'); subplot(2,2,4),imshow(g4),title('filtered image by laplacian filter with -8 at

    center');

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Assignment 5.1: Read the image cameraman.tif. Filter image using laplacian filter using standard

    MATLAB functions.

    original image filtered image by laplacian filter with -4 at center

    original image filtered image by laplacian filter with -8 at center

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Example 5.2: Non linear Spatial Filter

    % Non-Linear Filter close all; clear all; clc;

    f=imread('eight.tif');%read image fn=imnoise(f,'salt & pepper',0.02);%add noise K = medfilt2(fn);%median filter subplot(2,2,1),imshow(f),title('original image'); subplot(2,2,2),imshow(fn),title('image with salt and pepper noise'); subplot(2,2,4),imshow(K),title('filtered image by nonlinear filter');

    original image image with salt and pepper noise

    filtered image by nonlinear filter

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Assignment 5.2: Read the image cameraman.tif Add salt and pepper noise, poisson noise to the given

    image. Remove the noise using median filter.

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    EXPERIMENT NO. 06

    TITEL: - EDGE DETECTION OF AN IMAGE USING OPERATORS.

    OBJECTIVE: - TO DETECT THE EDGE OF AN IMAGE BY USING OPERATORS.

    6.1 THEORY:

    Edges are calculated by using difference between corresponding pixel intensities of an image. All the

    masks that are used for edge detection are also known as derivative masks. As image is also a signal so

    changes in a signal can only be calculated using differentiation. So thats why these operators are also called as derivative operators or derivative masks.

    All the derivative masks should have the following properties:

    Opposite sign should be present in the mask.

    Sum of mask should be equal to zero.

    More weight means more edge detection.

    Prewitt operator provides us two masks one for detecting edges in horizontal direction and another for

    detecting edges in a vertical direction. When we apply this mask on the image it prominent vertical edges.

    It simply works like as first order derivate and calculates the difference of pixel intensities in a edge

    region. As the centre column is of zero so it does not include the original values of an image but rather it

    calculates the difference of right and left pixel values around that edge. This increase the edge intensity

    and it became enhanced comparatively to the original image.

    Example 6.1: Edge Detection by Prewitt Operator

    % edge detection operation by prewitt operator

    close all;

    clear all;

    clc;

    f1=imread('coins.png');%read original image

    f2=im2double(f1); %converting the image value in to double

    f3=edge(f2,'prewitt'); % prewitt operation

    figure,imshow(f1),title('original image');%display original image

    figure,imshow(f3),title(after edge detection image');%display edge image

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)

    Assignment 6.1: Read the image football.jpg and fiend the edge with various operators (sobel, canny,

    Roberts, laplacian etc.).

    Original Image After edge detection image

  • FUTURE INSTITUTE OF ENGINEERING AND MANAGEMENT DEPARTMENT OF ELECTRICAL ENGINEERING DIGITAL IMAGE PROCESSING LAB MANUAL

    PAPER CODE : (EE 792D)

    PAPER CODE: EE-792D 4TH

    Yr. (7TH

    SEM)