0% found this document useful (0 votes)
0 views8 pages

adielab7

The document outlines a project aimed at enhancing and encoding images using Discrete Cosine Transform (DCT) and dithering techniques. It details the hardware and software requirements, the process of applying DCT and IDCT to both a 4x4 matrix and an image, as well as methods for thresholding segmentation and using Bayer's matrix for dithering. The results demonstrate successful image enhancement and encoding through these spectral representation techniques.

Uploaded by

rchandraaditya3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views8 pages

adielab7

The document outlines a project aimed at enhancing and encoding images using Discrete Cosine Transform (DCT) and dithering techniques. It details the hardware and software requirements, the process of applying DCT and IDCT to both a 4x4 matrix and an image, as well as methods for thresholding segmentation and using Bayer's matrix for dithering. The results demonstrate successful image enhancement and encoding through these spectral representation techniques.

Uploaded by

rchandraaditya3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Ex.

No:
Spectral representation for enhancement and coding – DCT and
Date : Dithering

AIM:

To enhance and encode images using spectral representation techniques such


as Discrete Cosine Transform (DCT) and Dithering.

HARDWARE AND SOFTWARE REQUIRED:


Pentium processor core i3, Windows Operating system, Matlab 2017or later version,
and Webcam

SPECTRAL ENHANCEMENT USING DCT AND SEGMENTATION:

1. DCT & IDCT on a 4×4 Matrix:


The Discrete Cosine Transform (DCT) converts spatial domain data into frequency
components, making it useful for compression. The Inverse DCT (IDCT) reconstructs
the original data by transforming frequency components back to the spatial domain.
2. DCT & IDCT on an Image:
Applying DCT to an image helps in compression by concentrating energy into a few
coefficients. IDCT is used to reconstruct the image by reversing the transformation
with minimal loss.
3. Thresholding Segmentation (Histogram Valley Point Method):
This technique segments an image by identifying valleys (local minima) in its
histogram, which represent intensity boundaries. Pixels are classified into regions
based on these threshold values.
4. Segmentation Using Bayer’s Matrix:
Bayer’s matrix is used in ordered dithering, where a threshold matrix is applied to an
image for quantization, enhancing edge details and reducing visible banding in
grayscale images.
(i) DCT and IDCT of 4x4 matrix:
clc;
clear all;
close all;

X = [9 8 6 1; 5 6 7 3; 7 10 11 9; 13 14 15 16];
[m, n] = size(X);
f = zeros(m, n);

for u = 0:m-1
for v = 0:n-1
S = 0;
if u == 0
alpha_u = sqrt(1/m);
else
alpha_u = sqrt(2/m);
end
if v == 0
alpha_v = sqrt(1/n);
else
alpha_v = sqrt(2/n);
end
for x = 0:m-1
for y = 0:n-1
S = S + (X(x+1, y+1) * cos(((2*x+1)*u*pi)/(2*m)) * cos(((2*y+1)*v*pi)/(2*n)));
end
end
f(u+1, v+1) = alpha_u * alpha_v * S;
end
end
disp('Manual DCT:');
disp(f);

d = dct2(X);
disp('DCT using dct2():');
disp(d);

X_reconstructed = zeros(m, n);


for x = 0:m-1
for y = 0:n-1
S = 0;
for u = 0:m-1
for v = 0:n-1
if u == 0
alpha_u = sqrt(1/m);
else
alpha_u = sqrt(2/m);
end
if v == 0
alpha_v = sqrt(1/n);
else
alpha_v = sqrt(2/n);
end
S = S + (alpha_u * alpha_v * f(u+1, v+1) * cos(((2*x+1)*u*pi)/(2*m)) *
cos(((2*y+1)*v*pi)/(2*n)));
end
end
X_reconstructed(x+1, y+1) = S;
end
end
disp('Manual IDCT (Reconstructed X):');
disp(X_reconstructed);

X_idct = idct2(d);
disp('IDCT using idct2():');
disp(X_idct);

Output

(ii) DCT and IDCT of an image:

img =
rgb2gray(imread('kayal.png')); img
= imresize(img, [200 200]);
img = double(img);
[m, n] = size(img);
f = zeros(m, n);
for u = 0:m-1
for v = 0:n-1
S = 0;
if u == 0
alpha_u = sqrt(1/m);
else
alpha_u = sqrt(2/m);
end
if v == 0
alpha_v = sqrt(1/n);
else
alpha_v = sqrt(2/n);
end
for x = 0:m-1
for y = 0:n-1
S = S + (img(x+1, y+1) * cos(((2*x+1)*u*pi)/(2*m)) * cos(((2*y+1)*v*pi)/(2*n)));
end
end
f(u+1, v+1) = alpha_u * alpha_v * S;
end
end

X_reconstructed = zeros(m, n);


for x = 0:m-1
for y = 0:n-1
S = 0;
for u = 0:m-1
for v = 0:n-1
if u == 0
alpha_u = sqrt(1/m);
else
alpha_u = sqrt(2/m);
end
if v == 0
alpha_v = sqrt(1/n);
else
alpha_v = sqrt(2/n);
end
S = S + (alpha_u * alpha_v * f(u+1, v+1) * cos(((2*x+1)*u*pi)/(2*m)) *
cos(((2*y+1)*v*pi)/(2*n)));
end
end
X_reconstructed(x+1, y+1) = S;
end
end

figure;
subplot(1,3,1), imshow(uint8(img)), title('Original Image','FontName','Arial','FontSize',6);
subplot(1,3,2), imshow(log(abs(f)+1), []), title('DCT of
Image','FontName','Arial','FontSize',6);
subplot(1,3,3), imshow(uint8(X_reconstructed)), title('Reconstructed
Image','FontName','Arial','FontSize',6);
Output:

(iii) Thressholding segmentation

clc;
clear all;
close all;

x = rgb2gray(imread('kayal.png'));
[m, n] = size(x);
pts = imhist(x);
valley_pts = islocalmin(pts);
thr = find(valley_pts, 1);
img = x > thr;

subplot(1,2,1);
imshow(x);
title('Original Image');

subplot(1,2,2);
imshow(img);
title('Thresholded Image');
Output:

(iv) SEGMENTATION USING BAYERS MATRIX:

clc;
clear all;
close all;

img = [120 130 140 150; 110 125 135 145;


100 115 128 138; 90 105 120 130];

B4 = [ 0 8 2 10;
12 4 14 6;
3 11 1 9;
15 7 13 5];

B4 = (B4 / 16) * 255;


segmented_img = img > B4;
disp('original matrix:');
disp(img);
disp('Bayers matrix:');
disp(B4);
disp('segmented matrix:');
disp(segmented_img);

Output:
Inferences:

(i) DCT and IDCT of 4x4 matrix:


The Discrete Cosine Transform (DCT) and its inverse (IDCT) were manually computed
for a 4x4 matrix X. The manual DCT involves scaling and applying cosine transforms
across rows and columns. The dct2() and idct2() MATLAB functions were also used to
compare the results, showing that they match the manual calculations.

(ii) DCT and IDCT of an image:


The image kayal.png was resized, and its DCT was calculated manually in a similar
manner to the 4x4 matrix. The DCT was visualized with a log scale, and the image was
reconstructed using the IDCT. The results show the original image, its DCT, and the
reconstructed image, demonstrating the effect of DCT on the image.

(iii) Thresholding segmentation:


For the image kayal.png, a histogram was computed, and valley points were found
using islocalmin(). The threshold was set at the first valley point, and the image was
segmented by thresholding. The original and thresholded images were displayed,
showing the segmentation.

(iv) Segmentation using Bayer's matrix:


A 4x4 Bayer matrix was used to apply dithering to the img matrix. The matrix was
normalized between 0 and 255, and the image was segmented by comparing it with the
Bayer matrix. The original, Bayer matrix, and segmented images were displayed,
showing the effect of the dithering process.

Result:
Thus we have successfully enhanced and encoded images using spectral representation
techniques such as Discrete Cosine Transform (DCT) and Segmentation using
Dithering.

You might also like