Showing posts with label Solutions. Show all posts
Showing posts with label Solutions. Show all posts

Monday, June 16, 2014

Fundamentals of Digital Image and Video Processing - Week 11 Solutions

Hi Coursera people,

Week 11 was not very mathematically intensive, except for the fact that I couldn't grasp the equations. I have to admit that I did understand all of the concepts of segmentation that were spoken about in the class. I was very keen to try the quiz, as this week's material was pretty challenging! The question on programming was so skillfully worded that it could baffle and deter a normal soul! And that feeling when you see a full 11 point score on the first attempt is so intriguing; you just can't express through words :)

Here is the question number 9 of week 11 :

In this problem, you will use Accumulative Difference Image (ADI) to calculate the motion of an object. The object is a bright rectangle moving with a constant speed in a dark background. Your task is to find out the speed of the object in the horizontal direction (x direction) and in the vertical direction (y direction), as well as the total space this object occupied while moving. The total space is defined as the total number of pixels that this object occupies at least once during its movement. Download the MATLAB code "motion_ADI.m" from here. The code has detailed comments regarding each functioning part. Basically, the code generates the reference frame and 10 consecutive frames containing the moving object. All you need to do is to decide on the appropriate threshold T in line 23 in the code and implement the three equations for ADI in the video lectures regarding motion-based segmentation. Starting your code flowing line 37 and finish it before the end of the for-loop. The rest of the code will calculate the speed of the moving object and the total space it occupies for you. Enter the values of speed_X_Direction, speed_Y_Direction, and total_space_occupied in the box below.

Here is my attempt at the code : (thanks to the discussions in the forums by fellow learners)

clear all
close all


A = zeros(256,256); % initialize a 256*256 image

% initialize absolute ADI, positive ADI and Negative ADI
% all initialized to zero
% Note that all ADIs are of the same size with the image
% DO NOT change the name of the ADIs as they will be used later
ADI_abs = zeros(256,256);
ADI_pos = zeros(256,256);
ADI_neg = zeros(256,256);

% initialize the starting position of the moving object
% the moving object is a rectangle similar to the example in the lecture
% slides
start1 = 100;
start2 = 150;
start3 = 40;
start4 = 110;

%threshold T as in euations in the lecture slides regarding ADI
T = 0.1;

%initialize the reference frame R
A(start1:start2, start3:start4) = 1;

%visualize the object and in the reference frame R
figure,imshow(A,[], 'border','tight');

j = 0;
for i = 5: 5 :50
        j = j + 12;
        A2 = zeros(256,256);
        A2(start1 + i: start2 + i, start3 + j: start4 + j) = 1;
        ADI_abs(abs(A-A2) > T) = ADI_abs(abs(A-A2) > T) +1;
        ADI_pos((A-A2) > T) = ADI_pos((A-A2) > T) +1;
        ADI_neg((A-A2) < -T) = ADI_neg((A-A2) < -T) +1;
     
        % You need to code up the follwing part that calculate the ADIs
        % Namely, the absolute ADI, the positive ADI and the negative ADI
        % Equations can be found in lecture slides regarding ADIs
        % You need to decide on the appropriate threshold T for this case
        % at line 23
end

% The following part will calculate the moving speed
% and the total space(in pixel number) occupied by the moving object
[row, col] = find(ADI_neg > 0);
speed_X_Direction = (max(col) - start4) / 10
speed_Y_Direction = (max(row) - start2) / 10
total_space_occupied = sum(sum(ADI_abs > 0))

% The following part helps you to visualize the ADIs you compute
% compare them with the example shown in lecture
% You should be getting someting very similar
figure,imshow(ADI_abs,[], 'border','tight');
figure,imshow(ADI_pos,[], 'border','tight');
figure,imshow(ADI_neg,[], 'border','tight');

-Cheers,
Vijay.



Saturday, June 14, 2014

Fundamentals of Digital Image and Video Processing - Week 10 Solutions

Hi Coursera people,

Well, for a pretty lecture intensive week, the solution to the problem posted was rather simple!

Just run the code without changing any parameters, and it should work fine!

PS : Remember to change the path of the images in the downloaded program!

-Cheers,
Vijay.

Tuesday, June 3, 2014

Fundamentals of Digital Image and Video Processing - Week 9 Solutions

Hi Coursera people,

I have to admit that the initial part of this week was very highly mathematical and I couldn't understand the videos. I was expecting the assignment to be equally tough. But to my surprise, the assignment was a straightforward implementation of a built-in Matlab function!

Here goes the Question number 7 of Week 9 :

In this problem you will get hands-on experience in JPEG image compression. Follow the instructions below to complete this problem. (1) Download the original 8-bit grayscale image here, and load it into a MATLAB array. (2) Perform JPEG compression by using the MATLAB function "imwrite". For the purpose of this problem, you need to specify 5 input arguments. The first argument is the MATLAB array containing the input image; the second argument is a string specifying the output file name; the third argument is 'jpg' (including the single quotes); the fourth argument is the string 'quality' (including the single quotes); and the last argument is a number that specifies the quality level used for compression. The quality level is an integer between 0 and 100. For this step, set the quality level to be 75 (the defaut value). After the function "imwrite" is invoked, a new JPEG image will be created in the location that was specified by you. (3) Load
the newly created JPEG image into a MATLAB array. Compute the PSNR between the JPEG compressed image and the original image. Note that the image loaded into MATLAB is of type 'uint8' (i.e., 8-bit integer). In order to compute the PSNR, you need to convert these arrays into 'double'. (4) Repeat steps (2) and (3) with the quality level set at 10. Enter the PSNR values corresponding to the JPEG images at quality level 75 and 10, respectively. Enter the numbers to 2 decimal points.

Here is my attempt at the code :

Original = imread('D:\private\MS related\~Coursera courses\Image and Video processing\Week 9\Cameraman256.bmp');
Original_double = im2double(Original);
imwrite(Original,'D:\private\MS related\~Coursera courses\Image and Video processing\Week 9\Converted.jpg','jpg','quality',75);
Converted = imread('D:\private\MS related\~Coursera courses\Image and Video processing\Week 9\Converted.jpg');
Converted_double = im2double(Converted);
MSE1= mean(mean((Original_double - Converted_double).^2,2));
MaxI=1;
PSNR1=10*log10((MaxI^2)/MSE1);
imwrite(Original,'D:\private\MS related\~Coursera courses\Image and Video processing\Week 9\ConvertedLowQual.jpg','jpg','quality',10);
ConvertedLowQual = imread('D:\private\MS related\~Coursera courses\Image and Video processing\Week 9\ConvertedLowQual.jpg');
ConvertedLowQual_double = im2double(ConvertedLowQual);
MSE2 = mean(mean((Original_double - ConvertedLowQual_double).^2,2)); % get the MSE
PSNR2=10*log10((MaxI^2)/MSE2);
PSNR1
PSNR2

-Cheers,
Vijay.

Wednesday, May 21, 2014

Fundamentals of Digital Image and Video Processing - Week 8 Solutions

Hi Coursera people,

I never knew this week would be so much interesting! I could understand almost every bit of information spoken in the class! :)

Here is the question number 7 of week 8.

In this problem, you will write a MATLAB program to compute the entropy of a given gray-scale image. Follow the instructions below to finish this problem. (1) Download the input image from here. The input is a gray-scale image with pixel values in the range [0,255]. Treat the pixel intensities in this image as symbols emitted from a DMS. (2) Build a probability model (i.e., an alphabet with associated probabilities) corresponding to this input image. Specifically, this alphabet consists of symbols {0,1,2,⋯,255}. In order to find the probabilities associated with each symbol, you will need to scan over all the pixels in this image, and for each pixel, adjust the probability associated with that pixel's intensity value accordingly, or in other words find the histogram of the image. Make sure you normalize the probability model correctly such that each probability is a real-valued number in [0,1]. (3) Compute the entropy using the formula that you have learned in class. Enter the result below to at least 2 decimal points.

Here is my attempt at the code.

A = imread('C:\~Coursera courses\Image and Video processing\Week 8\Cameraman256.bmp');
for i = 1:256
DMS(i,1) = i-1;
DMS(i,2) = 0;
end
for i = 1:256
for j = 1:256
for k = 1:256
if A(i,j) == DMS(k,1)
DMS(k,2) = DMS(k,2)+1;
end
end
end
end
sum = 0;
for i = 1:256
sum = sum + DMS(i,2);
end
for i = 1:256
prob(i) = DMS(i,2)/sum;
end
ans=0;
for i = 1:256
entropy(i) = -1 * prob(i) * log2(prob(i));
ans = ans + entropy(i);
end
ans

-Cheers,
Vijay.

Wednesday, May 14, 2014

Fundamentals of Digital Image and Video Processing - Week 6 Solutions

Hi Coursera people,

This week's lectures were very exasperating with regards to their length! Yes, I understand that there is a time limit of 12 weeks to complete the material; but it is equally important for followers to understand what is happening right? Anyways, with what I could manage to understand, I've cooked up this week's solution. Its very surprising to learn that a few lines of code will solve the purpose...

Here is question number 6 of week 6.

This problems pertains to inverse filtering. You should review the corresponding slides in the video lectures to refresh your memory before attempting this problem. To help you understand how inverse filter is implemented and applied, we have provided you with a MATLAB script here. Download the script and the original image, and open the script using MATLAB. Once you open the script, you will see on Line 8 the statement "T = 1e-1". This defines the threshold value used in the inverse filter. The script simulates the blur due to motion and applies inverse filtering for its removal. We encourage you to try different values of the threshold and see how it affects the performance of the inverse filter. We ask you to enter the ISNR value below when the threshold is set to 0.5. Make sure you enter the number with at least 2 decimal points.

Here's the code that was given as a part of the question. I've added the code that gives the solution at the end.

% inverse filter with thresholding

clear all
close all
clc

% specify the threshold T
T = 0.5;

%% read in the original, sharp and noise-free image
original = im2double(rgb2gray((imread('C:\Image and Video processing\Week 6\original_cameraman.jpg'))));
[H, W] = size(original);

%% generate the blurred and noise-corrupted image for experiment
motion_kernel = ones(1, 9) / 9;  % 1-D motion blur
motion_freq = fft2(motion_kernel, 1024, 1024);  % frequency response of motion blur
original_freq = fft2(original, 1024, 1024);
blurred_freq = original_freq .* motion_freq;  % spectrum of blurred image
blurred = ifft2(blurred_freq);
blurred = blurred(1 : H, 1 : W);
blurred(blurred < 0) = 0;
blurred(blurred > 1) = 1;
noisy = imnoise(blurred, 'gaussian', 0, 1e-4);


%% Restoration from blurred and noise-corrupted image
% generate restoration filter in the frequency domain
inverse_freq = zeros(size(motion_freq));
inverse_freq(abs(motion_freq) < T) = 0;
inverse_freq(abs(motion_freq) >= T) = 1 ./ motion_freq(abs(motion_freq) >= T);
% spectrum of blurred and noisy-corrupted image (the input to restoration)
noisy_freq = fft2(noisy, 1024, 1024);
% restoration
restored_freq = noisy_freq .* inverse_freq;
restored = ifft2(restored_freq);
restored = restored(1 : H, 1 : W);
restored(restored < 0) = 0;
restored(restored > 1) = 1;

%% analysis of result
noisy_psnr = 10 * log10(1 / (norm(original - noisy, 'fro') ^ 2 / H / W));
restored_psnr = 10 * log10(1 / (norm(original - restored, 'fro') ^ 2 / H / W));


%% visualization
figure; imshow(original, 'border', 'tight');
figure; imshow(blurred, 'border', 'tight');
figure; imshow(noisy, 'border', 'tight');
figure; imshow(restored, 'border', 'tight');
figure; plot(abs(fftshift(motion_freq(1, :)))); title('spectrum of motion blur'); xlim([0 1024]);
figure; plot(abs(fftshift(inverse_freq(1, :)))); title('spectrum of inverse filter'); xlim([0 1024]);

%% Calculation of ISNR

e1=original-noisy;
e2=original-restored;
E1=mean2(e1.*e1);
E2=mean2(e2.*e2);
result=10*log(E1/E2)/log(10)




P.S. : The answer is varying with regards to the second decimal as this code is run several times on the same machine. Don't ask me why! I'm as clueless as you are :p
Just type "2.85" in the answer area, and you get a full 3 points!

-Cheers,
Vijay.

Saturday, May 3, 2014

Fundamentals of Digital Image and Video Processing - Week 4 Solutions

Hi coursera people,

This week was very informative and very stretchy! Here is the solution to question number 8 of week 4.

Firstly, here is the question :

In this problem you will perform block matching motion estimation between two consecutive video frames. Follow the instructions below to complete this problem. (1) Download the two video frames from frame_1 and frame_2. The frames/images are of height 288 and width 352. (2) Load the frame with file name "frame_1.jpg" into a 288×352 MATLAB array using function "imread", and then convert the array type from 8-bit integer to real number using function "double" or "cast" (note that the range of intensity values after conversion is between 0 and 255). Denote by I1 the converted MATLAB array. Repeat this step for the frame with file name "frame_2.jpg" and denote the resulting MATLAB array by I2. In this problem, I2 corresponds to the current frame, and I1 corresponds to the previous frame (i.e., the reference frame). (3) Consider the 32×32 target block in I2 that has its upper-left corner at (65,81) and lower-right corner at (96,112). Note this is MATLAB coordinate convention, i.e., the first number between the parenthesis is the row index extending from 1 to 288 and the second number is the column index extending from 1 to 352. This target block is therefore a 32×32 sub-array of I2. (4) Denote the target block by Btarget. Motion estimation via block matching searches for the 32×32 sub-array of I1 that is "most similar" to Btarget. Recall in the video lectures we have introduced various forms of matching criteria, e.g., correlation coefficient, mean-squared-error (MSE), mean-absolute-error (MAE), etc. In this problem, we use MAE as the matching criterion. Given two blocks B1 and B2 both of size M×N, the MAE is defined as MAE(B1,B2)=1M×N∑Mi=1∑Nj=1|B1(i,j)−B2(i,j)|. To find the block in I1 that is most similar to Btarget in the MAE sense, you will need to scan through all the 32×32 blocks in I1, compute the MAE between each of these blocks and Btarget, and find the one that yields the smallest value of MAE. Note in practice motion search is only performed over a certain region of the reference frame, but for the sake of simplicity, we perform motion search over the entire reference frame I1 in this problem. When you find the matched block in I1, enter the following information: (1) the coordinate of the upper-left corner of the matched block in MATLAB convention. This requires two integer numbers; (2) the corresponding MAE value, which is a floating-point number. Enter the last number to two decimal points. As an example for format of answer, suppose the matched block has upper-left corner located at (1,1), and the corresponding MAE is 10.12, then you should enter 1 1 10.12 (the three numbers are separated by spaces).

And, here is my attempt at the solution :

frame_1 = imread('D:\Image processing\Week 4\digital-images-week4_quizzes-frame_1.jpg');
frame_2 = imread('D:\Image processing\Week 4\digital-images-week4_quizzes-frame_2.jpg');
I1 = double(frame_1);
I2 = double(frame_2);
Btarget = I2(65:96,81:112);
for i=1:288
if (i+31 <= 288)
for j=1:352
if (j+31 <= 352)
Btemp = I1(i:i+31,j:j+31);
err = Btarget - Btemp;
absoluteerr = abs(err);
ComputedMAE = mean2(absoluteerr);
MAEArray(i,j) = ComputedMAE;
end
end
end
end
A = min(MAEArray(:))
X = MAEArray;
[p,q] = find(X==min(X(:)))

-Cheers,
Vijay.

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.

Monday, April 14, 2014

Fundamentals of Digital Image and Video Processing - Week 2 Solutions

Hi coursera people,

I'm thrilled to take the course "Fundamentals of Digital Image and Video Processing". Matlab programming assignments have just started. The question 7 of week 2 involves some coding, which we are expected to write to get the right answers.

Here's the question :

In this problem you will implement spatial-domain low-pass filtering using MATLAB, and evaluate the difference between the filtered image and the original image using two quantitative metrics called Mean Squared Error (MSE) and Peak Signal-to-Noise Ratio (PSNR). Given two N1×N2 images x(n1,n2) and y(n1,n2), the MSE is computed as MSE=1N1N2∑N1n1=1∑N2n2=1[x(n1,n2)−y(n1,n2)]2. The PSNR is defined as PSNR=10log10(MAX2IMSE), where MAXI is the maximum possible pixel value of the images. For the 8-bit gray-scale images considered in this problem, MAXI=255. Follow the instructions below to finish this problem. (1) Download the original image from here. The original image is a 256×256 8-bit gray-scale image. (2) Convert the original image from type 'uint8' (8-bit integer) to 'double' (real number). (3) Create a 3×3 low-pass filter with all coefficients equal to 1/9, i.e., create a 3×3 MATLAB array with all elements equal to 1/9. (4) Low-pass filter the original image (converted to type 'double') with the filter created in step (3). This can be done using the built-in MATLAB function "imfilter". The function "imfilter" takes three arguments and returns one output. The first argument is the original image (converted to type 'double'); the second argument is the low-pass filter created in step (3); and the third argument is a string specifying the boundary filtering option. For this problem, use 'replicate' (including the single quotes) for the third argument. The output of the function "imfilter" is the filtered image. (5) Compute and record the PSNR value between the original image (converted to type 'double') and the filtered image by using the formulae given above. (6) Repeat steps (3) through (5) using a 5×5 low-pass filter with all coefficients equal to 1/25. Enter the PSNR values you have obtained from your experiments (The PSNR corresponding to 3×3 filter first, followed by the PSNR corresponding to 5×5 filter). Make sure you order the answers correctly and separate them by a space. Enter the numbers to 2 decimal points.

Here's my approach :

I = imread('C:\Users\****\Desktop\digital-images-week2_quizzes-lena.gif'); % 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
MSE = mean(mean((I2 - C).^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 % print the PSNR
B1 = [1/25, 1/25, 1/25, 1/25, 1/25; 1/25, 1/25, 1/25, 1/25, 1/25; 1/25, 1/25, 1/25, 1/25, 1/25; 1/25, 1/25, 1/25, 1/25, 1/25; 1/25, 1/25, 1/25, 1/25, 1/25];
C1 = imfilter(I2, B1, 'replicate');
MSE1 = mean(mean((I2 - C1).^2,2));
PSNR2=10*log10((MaxI^2)/MSE1);
PSNR2