Sie sind auf Seite 1von 39

Sr. No.

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.

Bitmap-file Structure: It consists of a bitmap-file header, a bitmap-information header, a


color table, and an array of bytes that define the bitmap image. The file has the
following form:

File Header Information


Image Header Information
Color Table (if present)
Pixel values
• BMP: BMP files have (1) a file header, (2) a bit map header, (3) a color
table, and (4) the image data. The file header, shown in figure , occupies
the first 14 bytes of all BMP files.
• The first two bytes are the file type which always equals 4D42 hex or
`BM.' The next four bytes give the size of the BMP file. The next two
bytes are reserved and are always zero.
• The last four bytes of the header give the offset to the image data. This
value points to where in the file the image data begins.
• This value, and the other four byte values in the header, is an unsigned
number (always positive). The next 40 bytes, are the bit map header.
• These are unique to the 3.x version of BMP files. The bit map header
begins with the size of the header (always 40). Next come the width and
height of the image data .
• BITMAPFILEHEADER: The bitmap file header contains information
about the type, size, and layout of a bitmap file and permits a check
as to the type of file the user is reading. The first two bytes of the file
contain the ASCII character “B” followed by an “M” to identify the file
type. The next four bytes of the header contain the file size with the
least significant bit first. The next four bytes are unused and set to
zero. The final four bytes are an offset to the start of the image pixel
data from the header and measured in bytes. Formally the structure
is of the form:
• The BMP color table has four bytes in each color table entry. The bytes are
for the blue, green, and red color values.
• The fourth byte is padding and is always zero. For a 256 gray shade image,
the color table is 4x256 bytes long.

• 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

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.
MIRROR IMAGE GENERATION
% this program produces mirror image of the image passed to it n also
% displays both the original and mirror
image a=imread('pout.tif');
[r,c]=size(a);
for i=1:1:r
k=1;
for j=c:-1:1
temp=a(i,k);
result(i,k)=a(i,j);
result(i,j)=temp;
k=k+1;
end
end
subplot(1,2,1),imshow(a)
subplot(1,2,2),imshow(result)
• TASK 1 Write a MATLAB code that reads a gray scale image and
generates the flipped image of original iamge. Your output should be
like the one given below
• I = imread('onion.png');
• I2 = flipdim(I ,2); %# horizontal flip
• I3 = flipdim(I ,1); %# vertical flip
• I4 = flipdim(I3,2); %# horizontal+vertical flip
• subplot(2,2,1), imshow(I)
• subplot(2,2,2), imshow(I2)
• subplot(2,2,3), imshow(I3)
• subplot(2,2,4), imshow(I4)
• img = imread('peppers.png'); %# Load a sample image
• imgMirror = flipdim(img,2); %# Flips the columns, making a mirror image
• imgUpsideDown = flipdim(img,1); %# Flips the rows, making an upside-down
image

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 it's a color image, first convert to a gray image

[rows, columns, numberofColorChannels] = size(originalImage);

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

binaryImage = grayImage >= 150;


binaryImage = grayImage <= 50;

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)

[rows, cols] = find(yourImage > 200);


considering the image is grayscale.
• How to calculate no. of pixels having value greater than a particular RGB value?

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:

% read in tiff image and convert it to double format


my_image = im2double(imread('picture.tif'));
my_image = my_image(:,:,1);
% perform thresholding by logical indexing
image_thresholded = my_image;
image_thresholded(my_image>3) = 256;
image_thresholded(my_image<0.5) = 0;
% display result
figure()
subplot(1,2,1)
imshow(my_image,[])
title('original image')
subplot(1,2,2)
imshow(image_thresholded,[])
title('thresholded image')
If you prefer looping over all pixels (which is not advisable due to its
slow speed), your code should look something like this:
% read in tiff image and convert it to double format
my_image = im2double(imread('picture.tif'));
my_image = my_image(:,:,1);
% allocate space for thresholded image
image_thresholded = zeros(size(my_image));
% loop over all rows and columns
for ii=1:size(my_image,1)
for jj=1:size(my_image,2)
% get pixel value
pixel=my_image(ii,jj);
% check pixel value and assign new value
if pixel<0.5
new_pixel=0;
elseif pixel>3
new_pixel=256;
else
new_pixel = pixel;
end
% save new pixel value in thresholded image
image_thresholded(ii,jj)=new_pixel;
end
end

% 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');

Das könnte Ihnen auch gefallen