Uploaded by GAYATHRI K S

ep3&4

advertisement
EX.NO : 03
DATE :
GRAY LEVEL TRANSFORMATION ON IMAGES
AIM:
To perform the basic gray-level point transformations on images.
REQUIREMENTS:
v HARDWARE : Personal Computer
v SOFTWARE : MATLAB
THEORY:
The principal objective of image enhancement is to process an image so that the result is more suitable
than the original image for a specific application. Image enhancement can be done in two domains:
[1] Spatial Domain and [2] Transform domain.
In spatial domain Image processing, there are two ways:
• Point processing (Single pixel is processed at a time)2
• Neighborhood processing (Mask processing) Convolution of 3x3 or 5x5 or other size of mask
with image
Point Operations
Point operations are also referred to as gray-level transformations or spatial transformations. These are
among the simplest enhancement techniques.
It can also be expressed as
s = T(r)
The transformation function T maps the pixel value r into the pixel value s. The values of the
transformation functions are stored in 1D- array and the mapping between r and s are implemented
using look-up tables. For 8-bit gray scale image the lookup table holds values between 0 and 255.
There are three broad categories of gray-level transformations. They are:
• Linear (identity and negative transformations)
• Logarithmic (log and inverse-log transformations)
• Power-law Transformation (N-power and N-root transformations)
Each of the above operations can be expressed in mathematical form relating the original pixel value r
to pixel value s. Other forms of transformations include techniques like
• Contrast stretching
• Piece-wise linear transformation
Identity Transformation: The identity function is the trivial case in which output intensities are identical
to input intensities.
Negative Transformation: The negative of an image with gray levels in the range [0, L-1] is obtained
by using the negative transformation
s = (L-1) - r
Reversing the intensity levels of an image in this manner produces the equivalent of
a photographic negative.
Logarithmic Transformations: The log transformation maps a narrow range of low-level gray scale
intensities into a wider range of output values and similarly maps the wide range of high-level gray scale
intensities into a narrow range of high-level output values. The log transformation is given by the
expression,
s = c log (1 + r)
where c is a constant.
Power Law (Gamma) Transformation:
The power law transformation function is described by
s = c rγ
where r is the original pixel value, s is the resulting pixel value, c is a scaling constant, and γ is a positive
value.
Contrast stretching: One of the most common applications of point transformation functions is
contrast manipulation (also known by many other names such as contrast stretching, gray-level
stretching, contrast adjustment, and amplitude scaling). Pixel values of f < m are compressed toward
darker values (towards black) in the output image, whereas values of f > m are mapped to brighter
pixel values (towards white) in the resulting image.
Piecewise Linear Transformations: This technique is also known as gray-level slicing. Highlighting
a specific range of gray levels in an image often is desired.
PROCEDURE:
1. Start the MATLAB program and open new M-file.
2. Implement the negative transformation on any gray-scale input image.
3. Implement the Logarithmic Transformations on any gray-scale input image for 4 different
values of c.
4. Implement the Power law Transformations on any gray-scale input image for 4 different values
of gamma.
5. Implement the piece-wise linear transformation using if-else statements.
6. Use the MATLAB’s IPT has a built-in function imadjust to perform contrast adjustments
(including autocontrast) and contrast stretching using threshold values.
7. Implement the bit plane slicing on any gray-scale input image.
PROGRAM AND OUTPUT:
%Import the input image
Image = imread('lungct.jpeg');
inputImage = rgb2gray(Image);
subplot(1,2,1)
imshow(inputImage);
title ('Input Image');
%Calculate the negative transformation
negativeImage = 255-inputImage;
%Display the negative image
subplot(1,2,2)
imshow(negativeImage);
title ("Negative Image");
OUTPUT IMAGE:
INFERENCE:
% Import the grayscale image
Inputimg = imread('lungct.jpeg');
grayImage = rgb2gray(Inputimg);
% Four different values of C for the logarithmic transformation
C_values = [5, 15, 45, 65];
subplot(2, 3, 1)
imshow(grayImage);
title('Input Image');
% Logarithmic transformation
for i = 1:numel(C_values)
C = C_values(i);
logTransform = uint8(C .* log(double(1 + grayImage)));
subplot(2, 3, i+1)
imshow(logTransform);
logtext = sprintf('Logarithmic for C=%2.1f', C);
title(logtext);
end
OUTPUT IMAGE:
INFERENCE:
% Read the input image
inputImage = imread('ctimg.png');
inputImage = rgb2gray(inputImage);
% Define the values of C and gamma
c_values = [45, 75];
gamma_values = [0.1, 0.25];
subplot(length(gamma_values) + 1, length(c_values), 1:(length(c_values)));
imshow(inputImage);
title('Input Image');
for i = 1:length(c_values)
C = c_values(i);
% Power Law Transformations
for j = 1:length(gamma_values)
gamma = gamma_values(j);
powerLawTransform = uint8(C .* (double(inputImage) .^ gamma));
subplot(length(gamma_values) + 1, length(c_values), i + j*length(c_values));
imshow(powerLawTransform);
title(sprintf('For C=%.1f and gamma=%.2f', C, gamma));
end
end
OUTPUT IMAGE:
INFERENCE:
% Read the input image
Image = imread('lungct.jpeg');
inputImage = rgb2gray(Image);
threshold = input('Enter the threshold value: ');
% Piece-wise linear transformation
[X, Y] = size(inputImage);
pieceWiseImage = zeros(X, Y);
for i = 1:X
for j = 1:Y
if inputImage(i, j) < threshold
pieceWiseImage(i, j) = 0;
else
pieceWiseImage(i, j) = 255;
end
end
end
subplot(1, 2, 1);
imshow(inputImage);
title('Input Image');
subplot(1, 2, 2);
imshow(pieceWiseImage);
title(['Piece-wiseTransformation (Threshold = ', num2str(threshold), ')']);
OUTPUT IMAGE:
INFERENCE:
%Read the input image
Image = imread('lungct.jpeg');
inputImage = rgb2gray(Image);
% Implement Contrast Stretching
minGraylevel = min(inputImage(:));
maxGraylevel = max(inputImage(:));
contrastStretching = ((inputImage - minGraylevel) / (maxGraylevel - minGraylevel)) * 255;
% Display the original and contrast stretched images
subplot(1, 2, 1);
imshow(inputImage);
title('Original Image');
subplot(1, 2, 2);
imshow(uint8(contrastStretching));
title('Contrast Stretched Image');
OUTPUT IMAGE:
INFERENCE:
% Read the input image
inputImage = imread('lungct.jpeg');
inputImage = rgb2gray(inputImage);
% Contrast Stretching using imadjust
contrastStretchedImage = imadjust(inputImage);
subplot(1, 2, 1);
imshow(inputImage);
title('Original Image');
subplot(1, 2, 2);
imshow(contrastStretchedImage);
title('Contrast Stretched Image');
OUTPUT IMAGE:
INFERENCE:
% Read the input image
input = imread('ctimg.png');
subplot(3, 3, 1);
imshow(input);
title('Original Image');
%Bit-plane Slicing
bitPlanes = zeros(size(input, 1), size(input, 2), 8, size(input, 3));
for c = 1:size(input, 3)
for i = 1:8
bitPlanes(:,:,i,c) = bitget(input(:,:,c), i);
subplot(3, 3, (c-1)*8 + i + 1);
imshow(bitPlanes(:,:,i,c));
title(['Bit plane ', num2str(i)]);
end
end
OUTPUT IMAGE:
INFERENCE:
Laboratory Rubrics
Criteria
Maximum Marks
C1
4
C2
4
C3
4
C4
4
C5
4
Total
RESULT:
Marks Scored
/ 20
EX.NO : 04
HISTOGRAM EQUALIZATION
DATE :
AIM:
To examine the image intensity and to enhance the global contrast of an image.
REQUIREMENTS:
v HARDWARE : Personal Computer
v SOFTWARE : MATLAB
THEORY:
The histogram of an image is a plot of the number of occurrences of gray levels in the image against
the gray-level values. The histogram provides more insight about image contrast and brightness.
Equalisation is a process that attempts to spread out the gray levels in an image so that they are evenly
distributed across their range. Histogram equalisation reassigns the brightness values of pixels based
on the image histogram.
Fig 4.1 - Four image types and their corresponding histograms
Histogram equalization usually increases the global contrast of the processing image. Histogram
equalization employs a monotonic, non-linear mapping which reassigns the intensity values of pixels in
the input image such that the output image contains a uniform distribution of intensities (i.e. a flat
histogram).
PROCEDURE:
1. Start the MATLAB program and open new M-file.
2. Read the input image.
3. Plot the histogram of the original image.
4. Perform the histogram equalization of the original image and plot histogram of the equalized
image.
5. Compare the histogram of original image and equalized image.
PROGRAM AND OUTPUT:
% Read the input image
Image = imread('lungct.jpeg');
input = rgb2gray(Image);
% Plot the histogram of the original image
figure;
subplot(2,1,1);
imshow(input);
title('Original Image');
subplot(2,1,2);
imhist(input);
title('Histogram of Original Image');
OUTPUT IMAGE:
%Histogram without using inbuilt function
input = imread('lungct.jpeg');
image = rgb2gray(input);
maxIntensityLevel = 255;
pixelCount = zeros(maxIntensityLevel + 1, 1);
for i = 1:size(image, 1)
for j = 1:size(image, 2)
intensityLevel = image(i, j) + 1;
pixelCount(intensityLevel) = pixelCount(intensityLevel) + 1;
end
end
intensityLevels = 0:maxIntensityLevel;
bar(intensityLevels, pixelCount);
xlabel('Intensity Level');
ylabel('Number of Pixels');
title('Histogram');
subplot(2, 1, 1);
imshow(image);
title('Input Image');
subplot(2, 1, 2);
bar(intensityLevels, pixelCount);
xlabel('Intensity Level');
ylabel('Number of Pixels');
title('Histogram');
OUTPUT IMAGE:
%Reading the Input Image
input = imread('lungct.jpeg');
original_image = rgb2gray(input);
%Plotting the Histogram of the Original Image
figure;
subplot(2,2,1);
imshow(original_image);
title('Original Image');
subplot(2,2,3);
imhist(original_image);
title('Histogram of the Original Image');
%Histogram Equalization and Plotting the Histogram of the Equalized Image
equalized_image = histeq(original_image);
subplot(2,2,2);
imshow(equalized_image);
title('Equalized Image');
subplot(2,2,4);
imhist(equalized_image);
title('Histogram of the Equalized Image');
OUTPUT IMAGE:
INFERENCE:
Laboratory Rubrics
Criteria
Maximum Marks
C1
4
C2
4
C3
4
C4
4
C5
4
Total
RESULT:
Marks Scored
/ 20
Download