Computer Vision hw5
Computer Vision hw5
Homework 5:
JPEG Compression
Pesented by:
MSc stg 1
1&2
jpeg _compression.m
figure;
subplot(1, 2, 1);
imshow(img);
title('Original Image');
Qc_std = [
17 18 24 47 99 99 99 99;
18 21 26 66 99 99 99 99;
24 26 56 99 99 99 99 99;
47 66 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99];
3. Choose an image, then apply the JPEG compression and decompression algorithms coded in steps 1
and 2 to plot the RMSE for each possible value of the quality factor Q.
function rmse_vs_q(image_path)
Q_values = 10:10:90;
RMSEs = zeros(size(Q_values));
original = im2double(imread(image_path));
for i = 1:length(Q_values)
Q = Q_values(i);
reconstructed = im2double(jpeg_compression(image_path, Q, 'h+v'));
RMSEs(i) = sqrt(mean((original(:) - reconstructed(:)).^2));
end
figure;
plot(Q_values, RMSEs, '-o');
xlabel('Quality Factor Q');
ylabel('RMSE');
title('RMSE vs JPEG Quality Factor');
grid on;
end
img = imread(image_path);
img_ycbcr = rgb2ycbcr(img);
Y = img_ycbcr(:,:,1);
Cb = img_ycbcr(:,:,2);
Cr = img_ycbcr(:,:,3);
switch downsamplingMode
case 'none'
Cb_ds = Cb_padded;
Cr_ds = Cr_padded;
case 'h'
Cb_ds = imresize(Cb_padded, [size(Cb_padded,1),
floor(size(Cb_padded,2)/2)], 'bilinear');
Cr_ds = imresize(Cr_padded, [size(Cr_padded,1),
floor(size(Cr_padded,2)/2)], 'bilinear');
case 'h+v'
Cb_ds = imresize(Cb_padded, floor(size(Cb_padded)/2), 'bilinear');
Cr_ds = imresize(Cr_padded, floor(size(Cr_padded)/2), 'bilinear');
otherwise
error('Invalid downsampling mode. Use "none", "h", or "h+v".');
end
Qc_std = [
17 18 24 47 99 99 99 99;
18 21 26 66 99 99 99 99;
24 26 56 99 99 99 99 99;
47 66 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99];
if Q < 50
S = 5000 / Q;
else
S = 200 - 2 * Q;
end
switch downsamplingMode
case 'none'
case 'h'
Cb_dq = imresize(Cb_dq, [size(Cb_dq, 1), size(Cb_dq, 2) * 2],
'bilinear');
Cr_dq = imresize(Cr_dq, [size(Cr_dq, 1), size(Cr_dq, 2) * 2],
'bilinear');
case 'h+v'
switch downsamplingMode
case 'none'
case 'h'
Cb_dq = imresize(Cb_dq, [size(Cb_dq, 1), size(Cb_dq, 2) * 2],
'bilinear');
Cr_dq = imresize(Cr_dq, [size(Cr_dq, 1), size(Cr_dq, 2) * 2],
'bilinear');
case 'h+v'
Cb_dq = imresize(Cb_dq, [size(Cb_dq, 1) * 2, size(Cb_dq, 2) * 2],
'bilinear');
Cr_dq = imresize(Cr_dq, [size(Cr_dq, 1) * 2, size(Cr_dq, 2) * 2],
'bilinear');
otherwise
error('Invalid downsampling mode.');
In command window
rmse_vs_q('C:\Users\ahmed\Pictures\ph\2.jpg')