Image DCT Data Shift
clear all,clcI = imread('Market.png');I=rgb2gray(I); fun = @dct2;mn=max(max(I))% I1=im2double(I); I2=I-128;mn2=max(max(I2)) J = blkproc(I2,[8 8],fun);imagesc(J), colormap(hot) fun2=@idct2; J1 = blkproc(J,[8 8],fun2); figure(2),imshow(uint8(J1));
Original Image
After restoring the image without shifting back
After shifting back some values are lost
We see a problem with the values under 127
• I(9:16,9:16)
• ans =
• 45 56 81 129 148 151 152 145• 43 46 69 115 147 154 152 147• 41 44 59 96 138 151 153 152• 38 40 52 78 124 148 152 151• 39 42 47 59 104 139 151 148• 46 43 45 47 89 130 150 152• 56 46 44 44 71 114 146 152• 61 45 40 47 50 95 138 149
• >> I3(9:16,9:16)
• ans =
• 128 128 128 129 148 151 152 145• 128 128 128 128 147 154 152 147• 128 128 128 128 138 151 153 152• 128 128 128 128 128 148 152 151• 128 128 128 128 128 139 151 148• 128 128 128 128 128 130 150 152• 128 128 128 128 128 128 146 152• 128 128 128 128 128 128 138 149
Now fixing the values using int16clear all,clcI = imread('Market.png');I=rgb2gray(I); I=int16(I);fun = @dct2;mn=max(max(I))% I1=im2double(I); I2=I-128;mn2=max(max(I2)) J = blkproc(I2,[8 8],fun);imagesc(J), colormap(hot)title('Color map of DCT of image'); fun2=@idct2; J1 = blkproc(J,[8 8],fun2); J1=J1+128; figure(2),imshow(uint8(J1)); title('Restoring the image after shifting back');
Shifting before the DCTJ(1:8,1:8)
ans =
-638.8750 -29.3727 4.1062 -16.0527 0.1250 -7.0450 -0.2126 -4.8849 85.6364 65.3826 33.3134 -17.2827 8.4377 -6.4442 -0.1027 -3.7384 -39.9354 -15.6904 30.2180 9.2577 4.1787 -2.2267 2.9812 2.4027 2.5924 -13.6623 -5.4940 -7.8364 3.6032 2.0654 1.9024 -1.0399 2.3750 4.8097 8.6232 -5.1047 -5.6250 0.9445 0.8930 0.3232 2.9945 -3.2131 -0.8315 6.9848 4.8601 0.3043 -1.6718 1.9621 1.2530 -0.0697 0.7312 -0.2981 1.9222 2.9600 -0.7180 -2.5778 2.9642 0.1810 -0.3388 -2.6802 0.8737 -1.0296 -2.5350 -1.8505
>> J(9:16,9:16)
ans =
-258.2500 -337.8517 15.4053 44.1097 -3.0000 3.1585 -0.6985 2.7715 93.1163 -12.7923 -105.2548 6.4951 21.3826 -6.4124 -5.7623 -1.4411 3.9429 29.0674 5.5570 -20.2654 8.7304 15.5683 -4.3624 -6.9161 6.3625 -0.4752 -3.8135 1.3667 -8.5273 -3.2138 1.0229 2.4084 -4.7500 5.8781 -0.2078 -5.6255 -0.5000 3.6717 -1.8081 -3.3458 5.5093 -0.4890 -0.0957 -0.4586 -0.3461 -1.6795 0.1375 1.1942 -0.2802 0.6512 1.8876 -2.0940 0.1721 -0.2394 -0.8070 -0.5737 0.1225 1.8141 1.4955 -0.7570 -1.3032 -0.4243 -1.4556 0.1052
Now without shifting
DCT without shiftingJ(1:8,1:8)
ans =
385.1250 -29.3727 4.1062 -16.0527 0.1250 -7.0450 -0.2126 -4.8849 85.6364 65.3826 33.3134 -17.2827 8.4377 -6.4442 -0.1027 -3.7384 -39.9354 -15.6904 30.2180 9.2577 4.1787 -2.2267 2.9812 2.4027 2.5924 -13.6623 -5.4940 -7.8364 3.6032 2.0654 1.9024 -1.0399 2.3750 4.8097 8.6232 -5.1047 -5.6250 0.9445 0.8930 0.3232 2.9945 -3.2131 -0.8315 6.9848 4.8601 0.3043 -1.6718 1.9621 1.2530 -0.0697 0.7312 -0.2981 1.9222 2.9600 -0.7180 -2.5778 2.9642 0.1810 -0.3388 -2.6802 0.8737 -1.0296 -2.5350 -1.8505
>> J(9:16,9:16)
ans =
765.7500 -337.8517 15.4053 44.1097 -3.0000 3.1585 -0.6985 2.7715 93.1163 -12.7923 -105.2548 6.4951 21.3826 -6.4124 -5.7623 -1.4411 3.9429 29.0674 5.5570 -20.2654 8.7304 15.5683 -4.3624 -6.9161 6.3625 -0.4752 -3.8135 1.3667 -8.5273 -3.2138 1.0229 2.4084 -4.7500 5.8781 -0.2078 -5.6255 -0.5000 3.6717 -1.8081 -3.3458 5.5093 -0.4890 -0.0957 -0.4586 -0.3461 -1.6795 0.1375 1.1942 -0.2802 0.6512 1.8876 -2.0940 0.1721 -0.2394 -0.8070 -0.5737 0.1225 1.8141 1.4955 -0.7570 -1.3032 -0.4243 -1.4556 0.1052
Why Shift data?• Because the DCT is designed to work on pixel
values ranging from -128 to 127, the original block is “leveled off” by subtracting 128 from each entry.
With shifting DC component becomes negative
dct2(int16(I(17:24,17:24))-128)
ans =
-257.2500 113.9048 190.8229 -14.2258 -31.7500 -26.0361 -7.0623 2.5148 -2.2054 -19.3567 26.7378 49.5825 18.4131 11.2723 -8.1334 -1.2940 -3.0683 -6.7897 -8.6176 -2.1773 0.6929 2.1958 0.9660 -1.2293 2.3132 3.9376 3.2723 8.6746 4.5256 2.9021 1.4571 1.7553 0 1.4061 -3.0190 -1.6145 1.5000 1.3845 -0.1025 -2.3183 -0.8553 -1.8905 2.8431 0.5789 3.0710 -1.4592 0.0228 -1.5351 -4.1410 2.3969 2.2160 2.8282 -0.2870 -0.1222 -0.1324 1.9538 1.8516 1.3828 -0.3954 2.7004 0.8187 1.7177 -0.6069 -1.3586
K>> dct2(int16(I(17:24,17:24)))
ans =
766.7500 113.9048 190.8229 -14.2258 -31.7500 -26.0361 -7.0623 2.5148 -2.2054 -19.3567 26.7378 49.5825 18.4131 11.2723 -8.1334 -1.2940 -3.0683 -6.7897 -8.6176 -2.1773 0.6929 2.1958 0.9660 -1.2293 2.3132 3.9376 3.2723 8.6746 4.5256 2.9021 1.4571 1.7553 0 1.4061 -3.0190 -1.6145 1.5000 1.3845 -0.1025 -2.3183 -0.8553 -1.8905 2.8431 0.5789 3.0710 -1.4592 0.0228 -1.5351 -4.1410 2.3969 2.2160 2.8282 -0.2870 -0.1222 -0.1324 1.9538 1.8516 1.3828 -0.3954 2.7004 0.8187 1.7177 -0.6069 -1.3586