Sunteți pe pagina 1din 12

Name: Yoong Jia Wee

Title: 30% JPEG Computing Exercise


Objectives:
1. Have a deeper appreciation and understanding of the amount of data required for an
uncompressed image as compared to that of a JPEG compressed image.
Introduction:
JPEG is a lossy compression technique for color images. The degree of compression can be adjusted,
allowing a selectable tradeoff between storage size and image quality. JPEG typically achieves 10:1
compression with little perceptible loss in image quality. Normallythe uncompressed image contains
unnecessary data that cannot perceived by human eye.
In this coursework, the steps for JPEG compression are:
1. Select 8x8 pixel region of the image.
2. Perform 2D DCT operation on the image.
3. Quantise the DCT Coefficient.
4. Dequantise the DCT Coefficient.
5. Perform inverse 2D DCT operation.
2D DCT of NxN blocks are computed and the results are quantized and entropy coded. In this case, N
is typically 8 and the DCT-II formula is applied to each row and column of the block. The result is an 8
8 transform coefficient array in which the (0,0) element (top-left) is the DC (zero-frequency)
component and entries with increasing vertical and horizontal index values represent higher vertical
and horizontal spatial frequencies.
The resulting block after DCT (the same size as the original block) is then pre-multiplied by the
quantisation scale code and divided element-wise by the quantization matrix, and rounding each
resultant element. The quantization matrix is designed to provide more resolution to more
perceivable frequency components over less perceivable components (usually lower frequencies
over high frequencies) in addition to transforming as many components to 0, which can be encoded
with greatest efficiency.
Typically this process will result in matrices with values primarily in the upper left (low frequency)
corner. By using a zig-zag ordering to group the non-zero entries and run length encoding, the
quantized matrix can be much more efficiently stored than the non-quantized version.

Part A.
Low Contrast 8x8 Area.
1. The 8x8 pixel region can be done by loading the entire image.
>>imshow(img); % to load the image

>>impixelregion; %to show pixel region

The region between x(1:8) and y(987:994) of the original 2048x2048 was selected. Low contrast
region is the region has lighter colour and contains pixel values of 120-255.
>> selectpixel=img(1:8,987:994);
2. The following screenshot is the selected pixel.
>> imshow(selectpixel);

The pixel values are displayed as follows:


>> disp(selectpixel);
141 141 145 144 141 145 144 145
140 143 144 145 141 143 143 141
143 144 141 146 143 145 143 139

144 143 139 143 143 144 143 139


146 144 144 143 144 140 144 140
146 141 145 143 145 139 141 140
143 139 139 139 143 139 140 140
145 145 140 140 141 139 140 140

The pixels of low contrast have higher pixel values.


3. By using dct2 function, DCT transform of the selected region is performed and it is stored in a
variable A. DCT2 is used because it has a strong energy compaction property. Most of the signal
information are concentrated at low frequency components of DCT. It is efficient in image
compression.
>>A=dct2(selectpixel);
3. The DCT coefficient values obtained are as follow:
>>disp(A);
Columns 1 through 7

1.1381 0.0048 -0.0015 0.0027 0.0009 0.0024 -0.0020


0.0056 -0.0050 -0.0036 -0.0025 -0.0041 0.0022 0.0015
-0.0026 -0.0025 0.0019 -0.0033 -0.0008 -0.0014 0.0022
0.0017 -0.0009 0.0000 -0.0034 0.0014 0.0012 0.0030
0.0016 -0.0000 0.0029 0.0005 -0.0011 -0.0005 -0.0005
-0.0024 -0.0034 -0.0001 0.0030 0.0013 -0.0004 0.0012
0.0031 0.0018 -0.0006 -0.0001 0.0005 0.0004 0.0006
0.0006 -0.0004 0.0004 0.0001 -0.0001 0.0002 -0.0005

Column 8

0.0007
-0.0046

-0.0010
0.0047
-0.0006
-0.0007
-0.0003
0.0002

High contrast 8x8 area:


5. The region between x(1828:1835) and y(1630:1637) of the original 2048x2048 was selected and
stored in a variable named choosepixel. High contrast image are usually dark.

>> choosepixel=img(1729:1736,1852:1859);
6. The following screenshot is the selected pixel:
imshow(choosepixel);

The pixel values are displayed as follows:


>> disp(choosepixel);
6 8 5 6 4 5 5 8
9 8 5 6 4 1 1 6
8 4 4 6 4 4 1 6
3 1 4 6 4 8 4 6

3 0 3 5 8 6 3 4
6 1 1 4 8 4 1 3
6 0 0 3 5 3 0 1
4 0 3 3 3 1 0 0
Higher contrast region has a smaller pixel values.
7. By using dct2 function, DCT transform of the selected region is performed and it is stored in a
variable B.
>>B=dct2(choosepixel);
8. The DCT coefficient values obtained are as follow:
>>disp(B);
Columns 1 through 7

31.5000 2.2141 -1.3530 2.6873 8.0000 -0.5217 3.2664


9.9771 1.2938 4.7504 -4.9140 -1.6624 -1.4822 -1.1873
-1.6168 5.2273 3.4660 -1.9947 -0.6929 -0.2693 -2.1857
2.1385 -2.1232 -0.8474 1.9758 0.4435 -1.7848 -1.7596
0.7500 -4.7027 -2.1744 -1.5309 -3.7500 1.1183 0.6301
0.3250 -2.7703 0.4159 1.2204 -1.1297 0.8045 0.9373
0.6697 -0.2379 0.0643 0.0722 0.2870 -0.1621 0.2840
-0.3932 -0.1026 -0.3823 0.7018 0.6233 -0.0552 0.1087

Column 8

-1.1636
-3.1078
0.1382
0.9116
-0.2017

-1.8429
0.1310
0.4259
Comparison
9.
Low contrast region
Columns 1 through 7

High contrast region


Columns 1 through 7

1.1381 0.0048
0.0024 -0.0020
0.0056 -0.0050
0.0022 0.0015
-0.0026 -0.0025
0.0014 0.0022
0.0017 -0.0009
0.0012 0.0030
0.0016 -0.0000
0.0005 -0.0005
-0.0024 -0.0034
0.0004 0.0012
0.0031 0.0018
0.0004 0.0006
0.0006 -0.0004
0.0002 -0.0005

31.5000 2.2141
-0.5217 3.2664
9.9771 1.2938
-1.4822 -1.1873
-1.6168 5.2273
-0.2693 -2.1857
2.1385 -2.1232
-1.7848 -1.7596
0.7500 -4.7027
1.1183 0.6301
0.3250 -2.7703
0.8045 0.9373
0.6697 -0.2379
-0.1621 0.2840
-0.3932 -0.1026
-0.0552 0.1087

-0.0015 0.0027 0.0009


-0.0036 -0.0025 -0.0041
0.0019 -0.0033 -0.0008 0.0000 -0.0034 0.0014
0.0029 0.0005 -0.0011 -0.0001 0.0030 0.0013 -0.0006 -0.0001 0.0005
0.0004 0.0001 -0.0001

Column 8

Column 8

0.0007
-0.0046
-0.0010
0.0047
-0.0006
-0.0007
-0.0003
0.0002

-1.1636
-3.1078
0.1382
0.9116
-0.2017
-1.8429
0.1310
0.4259

-1.3530 2.6873 8.0000


4.7504 -4.9140 -1.6624
3.4660 -1.9947 -0.6929
-0.8474 1.9758 0.4435
-2.1744 -1.5309 -3.7500
0.4159 1.2204 -1.1297
0.0643 0.0722 0.2870
-0.3823 0.7018 0.6233

As expected from theory, we notice that the value in the very first cell is the highest and as we along
the array, the values drastically decrease in value because of the following formula:

The first largest value is the DC value.


For the low contrast matrix, it has generally smaller number than the numbers in high contrast
matrix after dct. For the high contrast matrix, more number of pixels can be restored.

Part B
10. The DCT coefficents obtained in 4. Are quantised by diving the coefficients by M1 and rounded
off to the nearest integer value and stored in a variable named D.
>>Quotient=(A/M1);
>>D=round(Quotient);
11. The pixels were restored by multiplying D by M1 which is the quantisation matrix used earlier
and perform inverse DCT on the product of D and M1. The quantization matrix is designed to
provide more resolution to more perceivable frequency components over less perceivable
components (usually lower frequencies over high frequencies) in addition to transforming as many
components to 0, which can be encoded with greatest efficiency.
>>product=D*M1;
>> undo = idct2(product);
12. The restored pixel values are being rounded off and the original and restored values are as
follows:
Original Pixel Values
141 141 145 144 141
140 143 144 145 141
143 144 141 146 143
144 143 139 143 143
146 144 144 143 144
146 141 145 143 145
143 139 139 139 143
145 145 140 140 141

145
143
145
144
140
139
139
139

144
143
143
143
144
141
140
140

145
141
139
139
140
140
140
140

Restored pixel values


262 124 154 135 154 136 143 150
19 209 127 150 145 147 136 143
418 -46 230 106 167 134 156 142
82 178 138 147 146 144 140 140
188 147 139 142 148 139 141 143
14 234 98 157 137 145 132 141
42 188 118 148 135 144 136 139
284 34 185 121 145 135 150 139

From the above table, we can see that there are more deviation at the first 3 columns restored pixel
values. This is because IDCT is not rigorously defined, therefore it does not guarantee that the
decoded output will match the original pixel values. The quality of the restored image is not as good
as the original image because some data were lost during the rounding off stage. DCT itself does not
introduce losses since there are techniques that give lossless jpeg that also use DCT
or the modified versions of DCT.

13. MSE= 3748.04687500000.


>>selectpixel2=double(selectpixel); % to convert the entire array to double type
>>[M N] = size(selectpixel2); % to get the size of the entire array
>> error = selectpixel2- Undo_int; % to get the differences

>> Mean_Square_Error = sum(sum(error .* error)) / (M * N); %sum of differences divide by size of


array.
Part D
14. Using the blockproc function in conjunction with block_struct, the entire image was split into
regions of 8x8 and operations were performed on individual regions using quantisation matrix M1.
BLKPROC process the image img by applying the function fun to 8x8 block of img. Block struct is a
structure that contains the block data as well as other information about the block. The code written
for this process is attached below:
>>fun = @dct2;
>> J = blkproc(img,[8 8],fun);

The figure above shows dct-ed image


>> B1=blockproc(J,[8 8],@(block_struct) block_struct.data./M1);

The figure above shows quantised image.


>> B2=round(B1);
>> imshow(B2);
>>C1=blockproc(B2,[8 8],@(block_struct) block_struct.data.*M1);
>>funidct=@idct2
>>C2=blkproc(C1,[8 8],funidct);
>>imshow(C2);
>>Round=round(C2);
>>imshow(Round);
>>Round2=uint8(Round);
>>imshow(Round2);

15.
Original Image

Restored Image

Comparison of pixel values:


Original Image

Restored Image

From the 2 tables show above, we can notice that there are no differences between the quality of
the original image and the restored image. The pixel values of the restored image are the same with
the original image. Hence, we can conclude that the compression technique we used in 14. Is a
lossless compression method.
References:
1. http://en.wikipedia.org/wiki/Discrete_cosine_transform#CITEREFAraiAguiNakajima1988
2. http://en.wikipedia.org/wiki/Quantization_(image_processing)

S-ar putea să vă placă și