This week's assignment was quite challenging, considering the fact that I'm novice to Matlab programming! Anyways, after hours of hard programming, I nailed it! Full 3 points :)
Here is the question number 8 of week 3.
In this problem you will get hands-on experience with changing the resolution of an image, i.e., down-sampling and up-sampling. Follow the instructions below to finish this problem. (1) Download the original image from here. The original image is an 8-bit gray-scale image of width 479 and height 359 pixels. Convert the original image from type 'uint8' (8-bit integer) to 'double' (real number). (2) Recall from the lecture that in order to avoid aliasing (e.g., jagged edges) when down-sampling an image, you will need to first perform low-pass filtering of the original image. For this step, create a 3×3 low-pass filter with all coefficients equal to 1/9. Perform low-pass filtering with this filter using the MATLAB function "imfilter" with 'replicate' as the third argument. For more information about low-pass filtering using MATLAB, refer to the programming problem in the homework of Week 2. (3) Obtain the down-sampled image by removing every other row and column from the filtered image, that is, removing the 2, 4, all the way to the 358 row, and then removing the 2, 4, all the way to the 478 column. The resulting image should be of width 240 and height 180 pixles. This completes the procedure for image down-sampling. In the next steps, you will up-sample this low-resolution image to the original resolution via spatial domain processing. (4) Create an all-zero MATLAB array of width 479 and height 359. For every odd-valued i∈[1,359] and odd-valued j∈[1,479], set the value of the newly created array at (i,j) equal to the value of the low-resolution image at (i+12,j+12). After this step you have inserted zeros into the low-resolution image. (5) Convolve the result obtained from step (4) with a filter with coefficients [0.25,0.5,0.25;0.5,1,0.5;0.25,0.5,0.25] using the MATLAB function "imfilter". In this step you should only provide "imfilter" with two arguments instead of three, that was the case in step (1). The two arguments are the result from step (4) and the filter specified in this step. This step essentially performs bilinear interpolation to obtain the up-sampled image. (6) Compute the PSNR between the upsampled image obtained from step (5) and the original image. For more information about PSNR, refer to the programming problem in the homework of Week 2. Enter the PSNR you have obtained to two decimal points in the box below.
Here's my attempt at the code...
I=imread('D:\Image processing\digital-images-week3_quizzes-original_quiz.jpg'); % read the image
I2=im2double(I); % convert the uint8 image to double
B = [1/9, 1/9, 1/9; 1/9, 1/9, 1/9; 1/9, 1/9, 1/9]; % create the 3x3 array
C = imfilter(I2, B, 'replicate'); % apply the filter
C2=C; % just to make sure the original image is intact, I'm copying it into another dummy
C2(2:2:end,:)=[]; % clear the even rows
C2(:,2:2:end)=[]; % clear the even columns
IDownScale = C2; % just a legit name
NullMatrix=zeros(359,479); % create the NULL matrix
k=2; % a constant used below in checking even rows and columns
for i=1:359 % for loop for row
if rem(i,k) ~= 0 % filter out odd rows
for j=1:479 % for loop for columns
if rem(j,k) ~= 0 % filter out odd columns
NullMatrix(i,j) = IDownScale((i+1)/2,(j+1)/2); % copy downscaled image elements to appropriate places
end
end
end
end
ConvolveFilter=[0.25,0.5,0.25;0.5,1,0.5;0.25,0.5,0.25]; % create the convolution filter
Final = imfilter(NullMatrix, ConvolveFilter); % apply the filter
MSE = mean(mean((I2 - Final).^2,2)); % get the MSE
MaxI=1;% the maximum possible pixel value of the images.
PSNR1=10*log10((MaxI^2)/MSE); % get the PSNR
PSNR1
cheers,
Vijay.