0% found this document useful (0 votes)
2 views

Basic Image Import

Uploaded by

Bharathi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Basic Image Import

Uploaded by

Bharathi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Basic Image Import, Processing, and Export

Step 1: Read and Display an Image


I = imread('pout.tif');
imshow(I)

Step 2: Check How the Image Appears in the Workspace


whos I

Name Size Bytes Class Attributes

I 291x240 69840 uint8


Step 3: Improve Image Contrast
figure
imhist(I)

I2 = histeq(I);
figure
imshow(I2)
figure
imhist(I2)

Step 4: Write the Adjusted Image to a Disk File


imwrite (I2, 'pout2.png');

Step 5: Check the Contents of the Newly Written File


imfinfo('pout2.png')
ans = struct with fields:
Filename: '/tmp/Bdoc23a_2213998_985473/tp5b833e4e/images-
ex89505080/pout2.png'
FileModDate: '03-Mar-2023 06:10:18'
FileSize: 36938
Format: 'png'
FormatVersion: []
Width: 240
Height: 291
BitDepth: 8
ColorType: 'grayscale'
FormatSignature: [137 80 78 71 13 10 26 10]
Colormap: []
Histogram: []
InterlaceType: 'none'
Transparency: 'none'
SimpleTransparencyData: []
BackgroundColor: []
RenderingIntent: []
Chromaticities: []
Gamma: []
XResolution: []
YResolution: []
ResolutionUnit: []
XOffset: []
YOffset: []
OffsetUnit: []
SignificantBits: []
ImageModTime: '3 Mar 2023 11:10:18 +0000'
Title: []
Author: []
Description: []
Copyright: []
CreationTime: []
Software: []
Disclaimer: []
Warning: []
Source: []
Comment: []
OtherText: []

The Programming concepts is not included in Digital Image Processing Syllabus for ECE
students.
In order to get practical exposure of reading, writing, storing, importing of Image ,Matlab
commands explained to students.

Contrast Enhancement Techniques


This example shows how to enhance the contrast of grayscale and color images using
intensity value mapping, histogram equalization, and contrast-limited adaptive histogram
equalization.
Three functions are particularly suitable for contrast enhancement:
 imadjust increases the contrast of the image by mapping the values of the input
intensity image to new values such that, by default, 1% of the data is saturated at
low and high intensities of the input data.
 histeq performs histogram equalization. It enhances the contrast of images by
transforming the values in an intensity image so that the histogram of the output
image approximately matches a specified histogram (uniform distribution by
default).
 adapthisteq performs contrast-limited adaptive histogram equalization. Unlike histeq,
it operates on small data regions (tiles) rather than the entire image. Each tile's
contrast is enhanced so that the histogram of each output region approximately
matches the specified histogram (uniform distribution by default). The contrast
enhancement can be limited in order to avoid amplifying the noise which might be
present in the image.
Enhance Grayscale Images
Read a grayscale image with poor contrast into the workspace. Enhance the image using
the three contrast adjustment techniques with default settings.
tire = imread("tire.tif");
tire_imadjust = imadjust(tire);
tire_histeq = histeq(tire);
tire_adapthisteq = adapthisteq(tire);
Display the original image and the three contrast adjusted images as a montage.
montage({tire,tire_imadjust,tire_histeq,tire_adapthisteq},"Size",[1 4])
title("Original Image and Enhanced Images using " + ...
"imadjust, histeq, and adapthisteq")

figure
subplot(1,2,1)
imhist(pout)
title("Histogram of pout.tif")
subplot(1,2,2)
imhist(tire)
title("Histogram of tire.tif");

Histogram equalization, on the other hand, substantially changes both images.


Concentrating on the image of the tire, it would be preferable for the center of the wheel
to stay at about the same brightness while enhancing the contrast in other areas of the
image. In order for that to happen, a different transformation would have to be applied to
different portions of the image. The Contrast-Limited Adaptive Histogram Equalization
technique, implemented in adapthisteq, can accomplish this. The algorithm analyzes
portions of the image and computes the appropriate transformations. A limit on the level
of contrast enhancement can also be set, thus preventing the over-saturation caused by
the basic histogram equalization method of histeq. This is the most sophisticated
technique in this example.
Enhance Color Images
Contrast enhancement of color images is typically done by converting the image to a
color space that has image luminosity as one of its components, such as the L*a*b* color
space. Contrast adjustment is performed on the luminosity layer L* only, and then the
image is converted back to the RGB color space. Manipulating luminosity affects the
intensity of the pixels, while preserving the original colors.
Read an image with poor contrast into the workspace. Then, convert the image from the
RGB color space to the L*a*b* color space.
shadow = imread("lowlight_1.jpg");
shadow_lab = rgb2lab(shadow);
The values of luminosity span a range from 0 to 100. Scale the values to the range [0 1],
which is the expected range of images with data type double.
max_luminosity = 100;
L = shadow_lab(:,:,1)/max_luminosity;
Perform the three types of contrast adjustment on the luminosity channel, and keep the
a* and b* channels unchanged. Convert the images back to the RGB color space.
shadow_imadjust = shadow_lab;
shadow_imadjust(:,:,1) = imadjust(L)*max_luminosity;
shadow_imadjust = lab2rgb(shadow_imadjust);

shadow_histeq = shadow_lab;
shadow_histeq(:,:,1) = histeq(L)*max_luminosity;
shadow_histeq = lab2rgb(shadow_histeq);

shadow_adapthisteq = shadow_lab;
shadow_adapthisteq(:,:,1) = adapthisteq(L)*max_luminosity;
shadow_adapthisteq = lab2rgb(shadow_adapthisteq);
Display the original image and the three contrast adjusted images as a montage.
figure
montage({shadow,shadow_imadjust,shadow_histeq,shadow_adapthisteq},"Size",[1
4])
title("Original Image and Enhanced Images using " + ...
"imadjust, histeq, and adapthisteq")

Apply Gaussian Smoothing Filters to Images


This example shows how to apply different Gaussian smoothing filters to images
using imgaussfilt. Gaussian smoothing filters are commonly used to reduce noise.
Read an image into the workspace.
I = imread('cameraman.tif');
Filter the image with isotropic Gaussian smoothing kernels of increasing standard
deviations. Gaussian filters are generally isotropic, that is, they have the same standard
deviation along both dimensions. An image can be filtered by an isotropic Gaussian filter
by specifying a scalar value for sigma.
Iblur1 = imgaussfilt(I,2);
Iblur2 = imgaussfilt(I,4);
Iblur3 = imgaussfilt(I,8);
Display the original image and all the filtered images.
figure
imshow(I)
title('Original image')

figure
imshow(Iblur1)
title('Smoothed image, \sigma = 2')

figure
imshow(Iblur2)
title('Smoothed image, \sigma = 4')
figure
imshow(Iblur3)
title('Smoothed image, \sigma = 8')

Filter the image with anisotropic Gaussian smoothing kernels. imgaussfilt allows the
Gaussian kernel to have different standard deviations along row and column dimensions.
These are called axis-aligned anisotropic Gaussian filters. Specify a 2-element vector
for sigma when using anisotropic filters.
IblurX1 = imgaussfilt(I,[4 1]);
IblurX2 = imgaussfilt(I,[8 1]);
IblurY1 = imgaussfilt(I,[1 4]);
IblurY2 = imgaussfilt(I,[1 8]);
Display the filtered images.
figure
imshow(IblurX1)
title('Smoothed image, \sigma_x = 4, \sigma_y = 1')

figure
imshow(IblurX2)
title('Smoothed image, \sigma_x = 8, \sigma_y = 1')

figure
imshow(IblurY1)
title('Smoothed image, \sigma_x = 1, \sigma_y = 4')

figure
imshow(IblurY2)
title('Smoothed image, \sigma_x = 1, \sigma_y = 8')
Suppress the horizontal bands visible in the sky region of the original image. Anisotropic
Gaussian filters can suppress horizontal or vertical features in an image. Extract a section
of the sky region of the image and use a Gaussian filter with higher standard deviation
along the X axis (direction of increasing columns).
I_sky = imadjust(I(20:50,10:70));
IblurX1_sky = imadjust(IblurX1(20:50,10:70));
Display the original patch of sky with the filtered version.
figure
imshow(I_sky), title('Sky in original image')

figure
imshow(IblurX1_sky), title('Sky in filtered image')

clc;
clear all;
I = imread('cameraman.tif');
I1=I;
[row coln]= size(I);
I= double(I);
%---------------------------------------------------------
% Subtracting each image pixel value by 128
%--------------------------------------------------------
I = I - (128*ones(256));
quality = input('What quality of compression you require - ');
%----------------------------------------------------------
% Quality Matrix Formulation
%----------------------------------------------------------
Q50 = [ 16 11 10 16 24 40 51 61;
12 12 14 19 26 58 60 55;
14 13 16 24 40 57 69 56;
14 17 22 29 51 87 80 62;
18 22 37 56 68 109 103 77;
24 35 55 64 81 104 113 92;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99];

if quality > 50
QX = round(Q50.*(ones(8)*((100-quality)/50)));
QX = uint8(QX);
elseif quality < 50
QX = round(Q50.*(ones(8)*(50/quality)));
QX = uint8(QX);
elseif quality == 50
QX = Q50;
end

JPEG COMPRESSION OF GRAYSCALE IMAGE


%----------------------------------------------------------
% Formulation of forward DCT Matrix and inverse DCT matrix
%----------------------------------------------
DCT_matrix8 = dct(eye(8));
iDCT_matrix8 = DCT_matrix8'; %inv(DCT_matrix8);
%----------------------------------------------------------
% Jpeg Compression
%----------------------------------------------------------
dct_restored = zeros(row,coln);
QX = double(QX);
%----------------------------------------------------------
% Jpeg Encoding
%----------------------------------------------------------
%----------------------------------------------------------
% Forward Discret Cosine Transform
%----------------------------------------------------------
for i1=[1:8:row]
for i2=[1:8:coln]
zBLOCK=I(i1:i1+7,i2:i2+7);
win1=DCT_matrix8*zBLOCK*iDCT_matrix8;
dct_domain(i1:i1+7,i2:i2+7)=win1;
end
end
%-----------------------------------------------------------
% Quantization of the DCT coefficients
%-----------------------------------------------------------
for i1=[1:8:row]
for i2=[1:8:coln]
win1 = dct_domain(i1:i1+7,i2:i2+7);
win2=round(win1./QX);
dct_quantized(i1:i1+7,i2:i2+7)=win2;
end
end
%-----------------------------------------------------------
% Jpeg Decoding
%-----------------------------------------------------------
% Dequantization of DCT Coefficients
%-----------------------------------------------------------
for i1=[1:8:row]
for i2=[1:8:coln]
win2 = dct_quantized(i1:i1+7,i2:i2+7);
win3 = win2.*QX;
dct_dequantized(i1:i1+7,i2:i2+7) = win3;
end
end
%-----------------------------------------------------------
% Inverse DISCRETE COSINE TRANSFORM
%-----------------------------------------------------------
for i1=[1:8:row]
for i2=[1:8:coln]
win3 = dct_dequantized(i1:i1+7,i2:i2+7);
win4=iDCT_matrix8*win3*DCT_matrix8;
dct_restored(i1:i1+7,i2:i2+7)=win4;
end
end
I2=dct_restored;
% ---------------------------------------------------------
% Conversion of Image Matrix to Intensity image
%----------------------------------------------------------
K=mat2gray(I2);
%----------------------------------------------------------
%Display of Results
%----------------------------------------------------------
figure(1);imshow(I1);title('original image');
figure(2);imshow(K);title('restored image from dct');

You might also like