Digital Image Procesing Lab
Digital Image Procesing Lab
IMAGE PROCESSING
(Pratical)
MTECE-206N
Semester-2nd
Syntax
imshow(X,map)
imtool(X,map)
imshow(I)
imtool(I)
Theory:
To display a grayscale image, using either imshow or imtool, specify the image matrix as an
argument. This documentation uses the variable name I to represent a grayscale image in the
workspace.imshow(I)orimtool(I)Both functions display the image by scaling the intensity values
to serve as indices into a grayscale colormap.If I is double, a pixel value of 0.0 is displayed as
black, a pixel value of 1.0 is displayed as white, and pixel values in between are displayed as
shades of gray. If I is uint8, then a pixel value of 255 is displayed as white. If I is uint16, then a
pixel value of 65535 is displayed as white.Grayscale images are similar to indexed images in that
each uses an m-by-3 RGB colormap, but you normally do not specify a colormap for a grayscale
image. MATLAB® displays grayscale images by using a grayscale system colormap (where
R=G=B). By default, the number of levels of gray in the colormap is 256 on systems with 24-bit
color, and 64 or 32 on other systems. Displaying Grayscale Images That Have Unconventional
RangesIn some cases, the image data you want to display as a grayscale image could have a
display range that is outside the conventional toolbox range (i.e., [0,1] for single or double
arrays, [0,255] for uint8 arrays, [0,65535] for uint16 arrays, or [-32767,32768] for int16 arrays).
For example, if you filter a grayscale image, some of the output data could fall outside the range
of the original data.To display unconventional range data as an image, you can specify the
display range directly, using this syntax for both the imshow and imtool
functions.imshow(I,'DisplayRange',[low high]) or imtool(I,'DisplayRange',[low high])If you use
an empty matrix ([]) for the display range, these functions scale the data automatically, setting
low and high to the minimum and maximum values in the array.The next example filters a
grayscale image, creating unconventional range data. The example calls imtool to display the
image, using the automatic scaling option.
Matlab Code:
a = imread();
i = rgb2gray(a)
subplot(2,2,1)
imshow(a)
subplot(2,2,2)
imshow(i)
Program No. 1(b)
Q. Write a program to display any image as a histogram.
Apparatus Required: Computer, Matlab Software
Syntax
Theory
The Image Processing Toolbox software includes two display functions, imshow and imtool.
Both functions work within the Handle Graphics® architecture: they create an image object and
display it in an axes object contained in a figure object.
imshow is the toolbox's fundamental image display function. Use imshow when you want to
display any of the different image types supported by the toolbox, such as grayscale (intensity),
truecolor (RGB), binary, and indexed The imshow function is also a key building block for image
applications you can create using the toolbox modular tools.
The other toolbox display function, imtool, launches the Image Tool, which presents an
integrated environment for displaying images and performing some common image-processing
tasks. The Image Tool provides all the image display capabilities of imshow but also provides
access to several other tools for navigating and exploring images, such as scroll bars, the Pixel
Region tool, the Image
In general, using the toolbox functions to display images is preferable to using MATLAB image
display functions image and images because the toolbox functions set certain Handle Graphics
properties automatically to optimize the image display. The following table lists these properties
and their settings for each image type. In the table, X represents an indexed image, I represents a
grayscale image, BW represents a binary image, and RGB represents a truecolor image.
MATLAB CODE:
a = imread();
w = im2double(i)
subplot(2,2,3)
imshow(w)
subplot(2,2,4)
imhist(i)
Syntax
Theory
Negative – The negative of an image means the output image is the reversal of the input image.
In the case of an 8-bit image, the pixels with a value of 0 take on a new value of 255, while the
pixels with a value of 255 take on a new value of 0. All the pixel values in between take on
similarly reversed new values. The new image appears as the opposite of the original. The
imadjust function performs this operation.
Matlab Code:
w = imread();
subplot(2,2,1);
imshow(w)
subplot(2,2,2)
y = rgb2gray(w);
imshow(y)
title('GrayScale Image');
z = imcomplement(w);
subplot(2,2,3)
imshow(z)
title('imcomplement Image');
c = size(w);
for i = 1:c(1)
for j = 1:c(2)
for k= 1:c(3)
end
end
end
subplot(2,2,4)
imshow(w)
title('Negative Image');
OUTPUT:
Program No. 3
Aim- WAP to display an image in RED, Grayscale, GREEN and blue.
Apparatus Required: Computer,Matlab Software
Syntax
image(C)
image(x,y,C)
image(x,y,C,'PropertyName',PropertyValue,...)
image('PropertyName',PropertyValue,...)
handle = image(...)
Theory
image creates an image graphics object by interpreting each element in a matrix as an index into
the figure's colormap or directly as RGB values, depending on the data specified.
A high-level function that calls newplot to determine where to draw the graphics objects
and sets the following axes properties:
o XLim and YLim to enclose the image
o Layer to top to place the image in front of the tick marks and grid lines
o YDir to reverse
o View to [0 90]
A low-level function that adds the image to the current axes without calling newplot. The
low-level function argument list can contain only property name/property value pairs.
You can specify properties as property name/property value pairs, structure arrays, and cell
arrays (see set and get for examples of how to specify these data types).
image(C) displays matrix C as an image. Each element of C specifies the color of a rectangular
segment in the image.
image(x,y,C), where x and y are two-element vectors, specifies the range of the x- and y-axis labels,
but produces the same image as image(C). This can be useful, for example, if you want the axis
tick labels to correspond to real physical dimensions represented by the image. If x(1) > x(2) or
y(1) > y(2), the image is flipped left-right or up-down, respectively. It can also be useful when you
want to place the image within a set of axes already created. In this case, use hold on with the
current figure and enter x and y values corresponding to the corners of the desired image
location. The image is stretched and oriented as applicable.
handle = image(...) returns the handle of the image object it creates. You can obtain the handle with
all forms of the image function.
MATLAB CODE:
w = imread();
subplot(3,3,1)
imshow(w);
a = rgb2gray(w);
subplot(3,3,2)
imshow(a);
subplot(3,3,3)
b = w(:,:,1);
c = w(:,:,2);
d = w(:,:,3);
just_red = cat(3,b, s, s)
just_green = cat(3,s,c,s)
just_blue = cat(3,s,s,d)
imshow(just_red)
subplot(3,3,4)
imshow(just_blue)
subplot(3,3,5)
imshow(just_green)
OUTPUT:
Program No. 4
Aim- WAP to apply linear and power log transformation functions on an image.
Apparatus Required: Computer,Matlab Software
MATLAB CODE:
w = imread();
subplot(3,3,1)
imshow(w);
c = rgb2gray(w);
subplot(3,3,2)
imshow(c)
y = 2 * w;
subplot(3,3,3)
imshow(y)
y2 = w * (2^2);
subplot(3,3,4)
imshow(y2)
z = imadjust(w);
subplot(3,3,5)
imshow(z)
OUTPUT:
Program No. 5
Aim- WAP to enhance an image properties using combination of spatial filters.
Apparatus Required: Computer,Matlab Software
MATLAB CODE:
w = imread();
subplot(3,3,1)
imshow(w);
c = rgb2gray(w);
subplot(3,3,2)
imshow(c)
y = 2 * w;
subplot(3,3,3)
imshow(y)
d = w(:,:,3);
just_blue = cat(3,s,s,d)
subplot(3,3,4)
imshow(just_blue)
z = imadjust(w);
subplot(3,3,8)
imshow(z)
z = imcomplement(w);
subplot(2,2,7)
imshow(z)
OUTPUT:
Program No. 6
Aim- WAP to illustrate the effect of square averaging of different masks on an
image.
Apparatus Required: Computer,Matlab Software
Matlab Code:
I = imread();
subplot(3,3,1);imshow(I);title('Original Image');
H = fspecial('average',3);
MotionBlur = imfilter(I,H,'replicate');
subplot(3,3,2)
imshow(MotionBlur);
title('Average 3X3');
H = fspecial('average',5);
MotionBlur = imfilter(I,H,'replicate');
subplot(3,3,3)
imshow(MotionBlur);
title('Average 5X5');
H = fspecial('average',9);
MotionBlur = imfilter(I,H,'replicate');
subplot(3,3,4)
imshow(MotionBlur);
title('Average 9X9');
H = fspecial('average',15);
MotionBlur = imfilter(I,H,'replicate');
subplot(3,3,5)
imshow(MotionBlur);
title('Average 15X15');
OUTPUT:
PROGRAM 7
Aim- WAP to observe the effect of median filtered on an image corrupted by salt
and pepper method.
Apparatus Required: Computer,Matlab Software
MATLAB CODE:
A= imread();
I = rgb2gray(A);
subplot(2,2,1);
imshow(I);
title('Original Image');
subplot(2,2,2)
imshow(w)
K = medfilt2(w);
subplot(2,2,3)
imshow(K)
title('Filtered Image')
m = medfilt2(K);
subplot(2,2,4)
imshow(m)
title('Filtered Image2')
OUTPUT:
PROGRAM 8
Aim- WAP to show Image Enhancement using various filter “SOBEL”, “PREVIT”
and “LAPLACIAN”.
Apparatus Required: Computer,Matlab Software
MATLAB CODE:
I = imread();
subplot(2,2,1);imshow(I);title('Original Image');
H = fspecial('prewitt');
MotionBlur = imfilter(I,H,'replicate');
subplot(2,2,2);imshow(MotionBlur);title('Prewitt Image');
H = fspecial('sobel');
MotionBlur = imfilter(I,H,'replicate');
subplot(2,2,3);imshow(MotionBlur);title('Sobel Image');
H = fspecial('Laplacian',0.2);
MotionBlur = imfilter(I,H,'replicate');
subplot(2,2,4);imshow(MotionBlur);title('Laplacian Image');
OUTPUT:
PROGRAM 9
Aim- Write a program for image Histogram Equalization.
Syntax
J = histeq(I, hgram)
J = histeq(I, n)
[J, T] = histeq(I,...)
[newmap, T] = histeq(X,...)
Theory
histeq enhances the contrast of images by transforming the values in an intensity image, or the
values in the colormap of an indexed image, so that the histogram of the output image
approximately matches a specified histogram.
J = histeq(I, hgram) transforms the intensity image I so that the histogram of the output intensity
image J with length(hgram) bins approximately matches hgram.
histeq automatically scales hgram so that sum(hgram) = prod(size(I)). The histogram of J will
better match hgram when length(hgram) is much smaller than the number of discrete levels in I.
J = histeq(I, n) transforms the intensity image I, returning in J an intensity image with n discrete
gray levels. A roughly equal number of pixels is mapped to each of the n levels in J, so that the
histogram of J is approximately flat. (The histogram of J is flatter when n is much smaller than
the number of discrete levels in I.) The default value for n is 64.
[J, T] = histeq(I,...) returns the grayscale transformation that maps gray levels in the image I to
gray levels in J.
newmap = histeq(X, map, hgram) transforms the colormap associated with the indexed image X
so that the histogram of the gray component of the indexed image (X,newmap) approximately
matches hgram. The histeq function returns the transformed colormap in newmap. length(hgram)
must be the same as size(map,1).
newmap = histeq(X, map) transforms the values in the colormap so that the
histogram of the gray component of the indexed image X is approximately flat. It
returns the transformed colormap in newmap.
Examples
I = imread('tire.tif');
J = histeq(I);
imshow(I)
figure, imshow(J)
Display a histogram of the original image.
figure; imhist(I,64)
figure; imhist(J,64)
Algorithm
When you supply a desired histogram hgram, histeq chooses the grayscale transformation T to
minimize where c0 is the cumulative histogram of A, c1 is the cumulative sum of hgram for all
intensities k. This minimization is subject to the constraints that T must be monotonic and
c1(T(a)) cannot overshoot c0(a) by more than half the distance between the histogram counts at
a. histeq uses the transformation b = T(a) to map the gray levels in X (or the colormap) to their
new values.If you do not specify hgram, histeq creates a flat hgram,
hgram = ones(1,n)*prod(size(A))/n;
Result
Syntax
To demonstrate edge detection
% numbers of colors
sncols=128;
ncols=32;
% get image from MATLAB image
load('trees');
% show original image
figure(1);
showgimg(real(X),sncols);
drawnow;
% construct convolution functions
[m,n] = size(X);
gs = [1 -1]; ge = [];
hs = [1 -1]; he = [];
g = [gs,zeros(1,m-length(gs)-length(ge)),ge];
h = [hs,zeros(1,n-length(hs)-length(he)),he];
% construct convolution matrices as sparse matrices
Y = spcnvmat(g);
Z = spcnvmat(h);
Wg = Y*X;
Wh = X*Z';
% show transformed images
figure(2);
showgimg(Wg,ncols);
drawnow;
figure(3)
showgimg(Wh,ncols);
drawnow;
figure(4)
showgimg(abs(Wg)+abs(Wh),ncols);
drawnow;
Theory
Edges characterize boundaries and are therefore a problem of fundamental importance in image
processing. Edges in images are areas with strong intensity contrasts – a jump in intensity from
one pixel to the next. Edge detecting an image significantly reduces the amount of data and
filters out useless information, while preserving the important structural properties in an image.
There are many ways to perform edge detection. However, the majority of different methods
may be grouped into two categories, gradient and Laplacian. The gradient method detects the
edges by looking for the maximum and minimum in the first derivative of the image. The
Laplacian method searches for zero crossings in the second derivative of the image to find edges.
An edge has the one-dimensional shape of a ramp and calculating the derivative of the image can
highlight its location. Suppose we have the following signal, with an edge shown by the jump in
intensity below: The intensity changes thus discovered in each of the channels are then
represented by oriented primitives called zero-crossing segments, and evidence is given that this
representation is complete. (2) Intensity changes in images arise from surface discontinuities or
from reflectance or illumination boundaries, and these all have the property that they are
spatially localized. Because of this, the zero-crossing segments from the different channels are
not independent, and rules are deduced for combining them into a description of the image. This
description is called the raw primal sketch.
Result
Watermarking
A digital watermark is an invisible signature embedded inside an image to show authenticity and
ownership. An
effective digital watermark should be perceptually invisible to prevent obstruction of the original
image. It should be
statistically invisible to prevent detection, and it should als be robust to many image
manipulations, such as filtering,
additive noise, and compression. Digital Watermarking is the process of embedding information
into digital multimedia content such that the information (which we call the watermark) can later
be extracted or detected for a variety of purposes including copy prevention and control. Digital
watermarking has been proposed as a new, alternative method to enforce the intellectual property
rights and protect digital media from tampering. It involves a process of embedding into a host
signal a perceptually transparent digital signature, carrying a message about the
Signature is called the digital watermark. The digital watermark contains data that can be used
in various applications, including digital rights management, broadcast monitoring and tamper
proofing. Although perceptually transparent, the existence of the watermark is indicated when
watermarked media is passed through an appropriate watermark detector. Figure gives overview
of the general watermarking system. A watermark, which usually consists of a binary data
sequence, is inserted into the host signal in the watermark embedder. Thus, a watermark
embedder has two inputs; one is the watermark message (usually accompanied by a
secret key) and the other is the host signal (e.g. image, video clip, audio sequence etc.). The
output of the watermark embedder is the watermarked signal, which cannot be perceptually
discriminated from the host signal.
The watermarked signal is then usually recorded or broadcasted and later presented to the
watermark detector. The detector determines whether the watermark is present in the tested
multimedia signal, and if so, what message is encoded in it. The research area of watermarking is
closely related to the fields of information hiding and steganography. The three fields have a
considerable overlap and many common technical solutions. However, there are some
fundamental philosophical differences that influence the requirements and therefore the design of
a particular technical solution.
Information hiding (or data hiding) is a more general area, encompassing a wider range of
problems than the watermarking. The term hiding refers to the process of making the
information imperceptible or keeping the existence of the information secret. Steganography is
a word derived from the ancient Greek words steganos , which means covered and graphia,
which in turn means writing. It is an art of concealed communication. Therefore, we can define
watermarking systems as systems in which the hidden message is related to the host signal and
nonwatermarking systems in which the message is unrelated to the host signal.
img1=imresize(img1,[160 160]);
imshow(img1)
function pushbutton4_Callback(hObject, eventdata, handles)
global img1;
clc
preview(vid) ;
pause
img=getsnapshot(vid); % acquiring of image from
camera
closepreview
delete(vid);
img1=imresize(img,[320 240]); % size normalization
k=img1;
imshow(img1)
Step 3:- Convert image from gray scale to black and white.
function graybw_OpeningFcn(hObject, eventdata, handles,
varargin)
global BW;
global I;
BW=im2bw(I);
imshow(BW)
function pushbutton1_Callback(hObject,
eventdata, handles)
close graybw;
finfface
Step 4:- Crop image and provide binary co-
ordinates.
function pushbutton1_Callback(hObject,
eventdata, handles)
close finfface;
cropimage;
Step 5:- Show cropped image watermarking
image to
watermark with another.
function pushbutton1_Callback(hObject, eventdata, handles)
close cropimage;
step1;
Fig.
function pushbutton1_Callback(hObject,eventdata,handles)
close step1;
function pushbutton2_Callback(hObject, eventdata,
handles)
global img1;
[ans,pathname]=uigetfile( ...%opens folder to select
input image
{'*.jpg';'*.png';...
}, ...
'Select an IMAGE');
I=[pathname ans];
img1=imread([pathname ans]);
imshow(img1)
Step 7: Selection to send either image or text.
function step3_OpeningFcn(hObject, eventdata, handles,
varargin)
global img1;
imshow(img1)
global ext_out
ext_out='bmp';
function
pushbutton1_Callback(hObject,eventdata, handles)
close step3;
step5;
function
popupmenu1_Callback(hObject,eventdata, handles)
val = get(hObject,'Value');
str = get(hObject, 'String');
global img1;global img;global imgw;global ext_out;global I1;
ext_out='bmp';
switch str{val}
case 'TEXT'
case 'IMAGE'
watermark=I1;
[imgw]=watermark_img(img1,watermark) ;
imwrite(imgw,['watar_image' '.' ext_out],ext_out);
end
Step 8:- Watermarked image and provide link to upload
image. (Fig. 3.9)
function pushbutton1_Callback(hObject, eventdata, handles)
close step5;