Thursday, April 24, 2014

Fundamentals of Digital Image and Video Processing - Week 3 Solutions


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.

21 comments:

  1. dear vijay

    thanks for the code designing

    ReplyDelete
  2. Dear samsir,

    Thanks for the support.

    ReplyDelete
  3. Indexing of I2 and Final doesnt match
    index out of bounds because
    size(IDownScale)=[128,128].

    ReplyDelete
  4. Hi PS123,

    This code works perfectly for me.
    size(IDownScale) is 180x240. Why does it show 128x128 for you? Can you post your code here so that I can have a look?

    ReplyDelete
  5. I got it ! Thanks a lot ! it was error from my side !

    ReplyDelete
  6. Vijay Thanks For The Great Help I Was Stuck T Step 5 And Your Coding Really Helped Me.
    Could You Tell The Answer To Ques 6 :
    A 4×4 pixel image x(n1,n2) is depicted below. Denote by X(k1,k2) the 4×4 point DFT of x(n1,n2). Calculate X(0,0).
    Please.

    ReplyDelete
  7. I Know It's A Little Late, But If You Could Tell Me The Answer To Ques 6 Before The Deadline Of Week 3's Quiz 28th April'2014 7AM It Would Be Great.
    Still Thanks For The Coding You Truly Helped.

    ReplyDelete
  8. The Deadline Is 28th April'2014 7:30 PM Not 28th April'2014 7AM.
    My TimeZone Was Changed, So If You Could Still Tell Me.

    ReplyDelete
  9. Answer is a sum of everything. 136

    ReplyDelete
  10. Hey Vijay,

    Have you tried week 4 Q8?

    ReplyDelete
  11. I have tried the following code ,just check out

    A1=imread('frame_1.jpg');
    A=double(A1);
    B1=imread('frame_2.jpg');
    B=double(B1);
    topLine = 65;
    bottomLine = 96;
    leftColumn = 81;
    rightColumn = 112;
    width = bottomLine - topLine;
    height = rightColumn - leftColumn;
    I2=B(topLine : bottomLine, leftColumn : rightColumn);
    [M N]=size(I2);
    for i=1:size(A,1)-31
    for j=1:size(A,2)-31
    I1=A(i:i+31,j:j+31);
    % T=abs(I2-I1);
    % MAE(i,j)=sum(T(:))/(M*N);
    MAE(i,j)= mean2(abs(I1-I2));
    end
    end
    low=MAE(1,1);
    for i=1:size(A,1)-31
    for j=1:size(A,2)-31
    if (MAE(i,j)<low)
    low=MAE(i,j);
    m=i;
    n=j;
    end
    end
    end
    disp(low);
    disp(m);
    disp(n);

    ReplyDelete
  12. Hi PS123,

    Good to know that you've tried the code. Give me some time. I'll post the solution maximum by tomorrow.

    ReplyDelete
  13. Hi PS123,

    I've posted the solution. Your code also produced the correct output! Way to go!
    Kudos :)

    ReplyDelete
  14. i use MSE = (sum(sum((B- out2).^2)))/(256*256)..what is the difference between this formula and MSE = mean(mean((I2 - Final).^2,2)) one.i got different answers.

    ReplyDelete
  15. The M in MSE is the abbreviation for mean. You add the difference between the original image pixels and modified image pixels and take a mean of its square along both x and y directions.

    ReplyDelete
  16. Could you please tell me how this works ?
    C2(2:2:end,:)=[]; % clear the even rows
    C2(:,2:2:end)=[]; % clear the even columns

    Where can I read more about it?

    ReplyDelete
    Replies
    1. Hi Pavan,

      This is how you select even rows or even columns of any matrix. Assigning it to a null matrix clears it's contents. You can read more about matrix manipulation in Mathworks website.

      See this link, it explains what I was trying to do in my code more clearly:
      https://www.mathworks.com/matlabcentral/answers/102465-how-do-i-extract-the-odd-and-even-rows-of-my-matrix-into-two-separate-matrices-and-reconstruct-it-ba

      I've mentioned a few links below for your reference.

      https://ewh.ieee.org/r1/ct/sps/PDF/MATLAB/chapter2.pdf

      https://www.maths.unsw.edu.au/sites/default/files/MatlabSelfPaced/lesson6/MatlabLesson6_Creating.html

      Delete
  17. Thanks for giving really impressive information. For more knowledge click on the link Best Digital Marketing Services

    ReplyDelete