Lab 1
Lab 1
List of Experiment
1. Write a C Program to display header information of 16 color .bmp image.
Objective : Student should be able to develop a program of 16 color .bmp image using
c.
bitmap files stored in a device-independent bitmap form with the name extension .DIB.
• The blue, green, and red values equal one another. The data is stored row
by row with padding on the end of each row.
• The padding ensures the image rows are multiples of four. The four, just like
in the color table, makes it easier to read blocks and keep track of
addresses.
• Algorithm:
• 1. Start
• 2. Create structure of bitmap
• 3. create file pointer
• 4. Declare int() ,char() array[]
• 5. Read file is available or not
• 6. Create pointer of bitmap structure
• 7. Read image information
• 8. Display
2. Program to enhance image using image arithmetic and logical
operations.
3. Program for an image enhancement using pixel operation.
4. Program for gray level slicing with and without background.
5. Program for image enhancement using histogram equalization.
6. Program to filter an image using averaging low pass filter in spatial domain.
And median filter.
7. Program to sharpen an image using 2-D laplacian high pass filter in spatial
domain.
8. Program for detecting edges in an image using Roberts cross gradient
operator and sobel operator.
9. Program for smooth an image using low pass filter in frequency domain .
(Butterworth lpf)
10. Program for smooth an image using high pass filter in frequency domain .
(Butterworth hpf)
11. Program for morphological image operations-erosion, dilation, opening &
closing.
12. Program for illustrating color image processing.
13. Program for image Watermarking .
Lab Objectives This objective of this lab is to understand
• 1. How to read an image in Matlab.
• 2. How to show an image in Matlab.
• 3. How to access Image Pixels in Matlab.
• 4. How to write Image in Matlab.
• 5. Mirror Image generation. 6. Flipped Image generation
• 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. 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.
• Display An Image
• To display iamge, 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)
Example:
a=imread('pout.tif');
imwrite(a,gray(256),'b.bmp');
imshow('b.bmp')% imshow is used to display image
Writing image to disk
• 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
To mirror flip an "Image_Original" you can just use flip or rotate and transpose
the image :
• Image_Flip = flip(Image_Original,2);
• Image_Flip = imrotate(Image_Original,90)';
From scratch, for a gray scale image
% img=zeros(100,100); % img(50:75,20:35)=1; % img(10:20,10:20)=1;
img=imread('cameraman.tif')
subplot(1,2,1)
imshow(img)
[r,c]=size(img);
imgthili=zeros(100,100);
for i=1:r
for u=1:c
if (img(i,u)>=1 )
imgthili(i,c+1-u)=img(i,u);
else
imgthili(i,c+1-u)=img(i,u);
end
end
end
subplot(1,2,2)
intth=uint8(imgthili);
imshow(intth)
TASK 2
Write a MATLAB code that will do the following
1. Read any gray scale image.
2. Display that image.
3. Again display the image such that the pixels having intensity values below
than 50 will display as black and pixels having intensity values above than 150
will display as white. And the pixels between these will display as it is.
How to find the intensity of each pixel of an image
if numberofColorChannels > 1
grayImage = rgb2gray(originalImage);
else
grayImage = originalImage;
End
otherwise if it's already gray, you don't need to call rgb2gray(). Then threshold
Then count
numberOfWhitePixels = sum(binaryImage(:));
numberOfBlackPixels = sum(binaryImage(:));
Note: MATLAB uses the American spelling of gray, not the English spelling of grey.
• How can I get the pixel coordinates of the pixels which have intensity
value greater than 200? (in matlab)
I want to calculate the no. of pixels having value greater than a particular RGB value for eg. lets say
180. (pic(mm,nn,1) > 180 && pic(mm,nn,2) > 180 && pic(mm,nn,3) > 180).
% Extract the individual red, green, and blue color channels and threshold them.
binaryR = rgbImage(:, :, 1) > 180;
binaryG = rgbImage(:, :, 2) > 180;
binaryB = rgbImage(:, :, 3) > 180;
% AND the binary images together to find out where ALL THREE are > 180.
binaryImage = binaryR & binaryG & binaryB;
% Count the number of pixels where it's true that all 3 are > 180;
pixelCount = sum(binaryImage(:));
You're wrong. binaryImage is 1 when all 3 channels of the pixel is above the threshold, and 0
otherwise. Hence the sum is the number of pixels, Image Analyst could have used nnz instead:
pixelCount = nnz(binaryImage);
• changing values of pixels in an image pixel by pixel ( Thresholding )
You can simply use operations on your image matrix, which would look like this:
% display result
figure()
subplot(1,2,1)
imshow(my_image,[])
title('original image')
subplot(1,2,2)
imshow(image_thresholded,[])
title('thresholded image')
• Program to enhance an image using image arithmetic and logical
operations
• % 1.To perform Basic Image Processing Operations
• % a. Arithmetic and Logical Operations
• clc;
• clear all;
• close all;
A=imread('cameraman.tif');
subplot(5,3,1)
imshow(A)
title('Image A');
B=imread('rice.png');
subplot(5,3,2)
imshow(B)
title ('Image B');
C=zeros(size(A));
for (x= 100: 200)
for (y=100: 200)
C(x,y)=255;
end
end
subplot(5,3,3)
imshow(C)
title('Image C');
C=uint8(C);
Note:
B=zeros(A) Create an array of all zeros
If A is a scalar, then Matlab returns a A*A matrix of zeros
Uint8
uint*(X) converts the elements of array X into unsigned integers. X can be any numeric
object (such as a double ). The results of a uint* operation are shown in the next table
% Logical Operations
lr1=bitand(A,C);
subplot(5,3,4)
imshow(lr1)
title('A and C');
lr2=bitand(B,C);
subplot(5,3,5)
imshow(lr2)
title('B and C');
lr3=bitand(A,B);
subplot(5,3,6)
imshow(lr3)
title('A and B');
lr4=bitor(A,C);
subplot(5,3,7)
imshow(lr2)
title('A or C');
lr5=bitor(B,C);
subplot(5,3,8)
imshow(lr5)
title('B or C');
lr6=bitor(A,B);
subplot(5,3,9)
imshow(lr6)
title('A or B');
lr7=bitxor(A,C);
subplot(5,3,10)
imshow(lr7)
title('A exor C');
lr8=bitxor(B,C);
subplot(5,3,11)
imshow(lr8)
title('B exor C');
lr9=bitxor(A,B);
subplot(5,3,12)
imshow(lr9)
title('A exor B');
lr10= bitcmp(A);
subplot(5,3,13)
imshow(lr10)
title('Not A');
lr11= bitcmp(B);
subplot(5,3,14)
imshow(lr11)
title('Not B');
lr12= bitcmp(C);
subplot(5,3,15)
imshow(lr12)
title('Not C');
% Arithmetic Operations
lr13=imadd(A,C);
figure,subplot(4,3,1)
imshow(uint8(lr13))
title('A + C');
lr14=imadd(B,C);
subplot(4,3,2)
imshow(uint8(lr14))
title('B + C');
lr15=imadd(A,B);
subplot(4,3,3)
imshow(uint8(lr15))
title('A + B');
lr16=imsubtract(A,C);
subplot(4,3,4)
imshow(uint8(lr16))
title('A - C');
lr17=imsubtract(B,C);
subplot(4,3,5)
imshow(uint8(lr17))
title('B - C');
lr18=imsubtract(A,B);
subplot(4,3,6)
imshow(uint8(lr18))
title('A - B');
lr19=immultiply(A,C);
subplot(4,3,7)
imshow(uint8(lr19))
title('A * C');
lr20=immultiply(B,C);
subplot(4,3,8)
imshow(uint8(lr20))
title('B * C');
lr21=immultiply(A,B);
subplot(4,3,9)
imshow(uint8(lr21))
title('A * B');
lr22=imdivide(A,C);
subplot(4,3,10)
imshow(uint8(lr22))
title('A/C');
lr23=imdivide(B,C);
subplot(4,3,11)
imshow(uint8(lr23))
title('B/C');
lr24=imdivide(A,B);
subplot(4,3,12)
imshow(uint8(lr24))
title('A/B');