Showing posts with label question 7. Show all posts
Showing posts with label question 7. Show all posts

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.

Thursday, May 15, 2014

Fundamentals of Digital Image and Video Processing - Week 7 Solutions

Hi Coursera people,

After hours of struggle, I had found the solution to question number 7 of week 7! There is absolutely no material available online regarding calculation of frequency response of the CLS filter! Finally, the answer sprung up from the discussions forum of the course itself :) I must thank my fellow Courserans for helping me out with the code. Here goes the question number 7 of week 7.

In this problem, you will implement the Constrained Least Squares (CLS) filter and examine its performance when the regularization parameter is set at different values. You will be provided with the original image and a set of MATLAB files. Follow the instructions below to finish this problem. (1) Download the original image and the MATLAB code from here. Place the original image and all the provided MATLAB files in the same directory. (2) The file "wrapper.m" is the entry or the "main" code. It loads the original image, applies a motion blur to it, and degrades the image by adding noise. The 17th line in "wrapper.m" sets the value of the regularization parameter "alpha". (3) The MATLAB file "cls_restoration.m" has an incomplete implementation of the CLS filter. You need to un-comment line 24 in "cls_restoration.m" and complete the implementation of the CLS filter. (4) After you complete the implementation of the CLS filter, you should run "wrapper.m" with different values of alpha. Specifically, we ask you to try the following values of alpha: {0.0001, 0.001, 0.01, 0.1, 1, 10, 100}. For each value of alpha, we ask you to compute the Improvement in SNR (ISNR). Note that the computation of ISNR involves there images: the original image, the blurred and noisy image, and the restored image. After you obtain the ISNR values, enter in the box below the largest ISNR value. Enter the number with at least two decimal points.

Here is my attempt at the code :

wrapper.m

clear all
close all

%% Simulate 1-D blur and noise
image_original = im2double(imread('C:\Image and Video processing\Week 7\downloaded codes\Cameraman256.bmp', 'bmp'));
[H, W] = size(image_original);
blur_impulse = fspecial('motion', 7, 0);
image_blurred = imfilter(image_original, blur_impulse, 'conv', 'circular');
noise_power = 1e-4;
randn('seed', 1);
noise = sqrt(noise_power) * randn(H, W);
image_noisy = image_blurred + noise;

figure; imshow(image_original, 'border', 'tight');
figure; imshow(image_blurred, 'border', 'tight');
figure; imshow(image_noisy, 'border', 'tight');

%% CLS restoration
alpha = 0.0001;  % you should try different values of alpha
image_cls_restored = cls_restoration(image_noisy, blur_impulse, alpha);
figure; imshow(image_cls_restored, 'border', 'tight');

%% computation of ISNR

e1=image_original-image_noisy;
e2=image_original-image_cls_restored;
E1=mean2(e1.*e1);
E2=mean2(e2.*e2);
result=10*log(E1/E2)/log(10)


cls_restoration.m

function image_restored = cls_restoration(image_noisy, psf, alpha)

%% find proper dimension for frequency-domain processing
[image_height, image_width] = size(image_noisy);
[psf_height, psf_width] = size(psf);
dim = max([image_width, image_height, psf_width, psf_height]);
dim = next2pow(dim);

%% frequency-domain representation of degradation
psf = padarray(psf, [dim - psf_height, dim - psf_width], 'post');
psf = circshift(psf, [-(psf_height - 1) / 2, -(psf_width - 1) / 2]);
H = fft2(psf, dim, dim);

%% frequency-domain representation of Laplace operator
Laplace = [0, -0.25, 0; -0.25, 1, -0.25; 0, -0.25, 0];
Laplace = padarray(Laplace, [dim - 3, dim - 3], 'post');
Laplace = circshift(Laplace, [-1, -1]);
C = fft2(Laplace, dim, dim);

%% Frequency response of the CLS filter
% Refer to the lecture for frequency response of CLS filter
% Complete the implementation of the CLS filter by uncommenting the
% following line and adding appropriate content

R = conj(H)./(abs((H.*H))+(alpha*abs((C.*C))));

%% CLS filtering
Y = fft2(image_noisy, dim, dim);
image_restored_frequency = R .* Y;
image_restored = ifft2(image_restored_frequency);
image_restored = image_restored(1 : image_height, 1 : image_width);


next2pow.m

function result = next2pow(input)
if input <= 0
    fprintf('Error: input must be positive!\n');
    result = -1;
else
    index = 0;
    while 2 ^ index < input
        index = index + 1;
    end
    result = 2 ^ index;
end


And don't get freaked if you get negative ISNR values as the result; it is absolutely normal. Here are the observations for the various values of alpha.
0.0001    -5.9191
0.001      -1.5292
0.01        3.4933
0.1          4.3048
1             2.1471
10           0.4866

-Cheers,
Vijay.

Sunday, May 11, 2014

Fundamentals of Digital Image and Video Processing - Week 5 Solutions

Hi Coursera people,

This week's videos were large and I expected the assignment also to be challenging. On the contrary, the assignment was a very simple and was a replica of week 1's assignment.

Here is the question number 7 of week 5 :

In this problem you will perform median filtering to enhance the quality of a noise corrupted image. Recall from the video lecture that median filtering is effective for removing "salt-and-pepper" noise from images. Follow the instructions below to complete this problem. (1) Download the noisy image from here. Load the noisy image into a MATLAB array and convert the type of the array from 8-bit integer 'uint8' to real number 'double'. Refer to MATLAB problems in previous homework if you need help with loading and converting images. Visualize the noisy image using the built-in MATLAB function "imshow". The function "imshow" takes as its argument either [0-255] for an 8-bit integer array (i.e., of type 'uint8'), or [0-1] for a normalized real-valued array (i.e., of type 'double'). To provide "imshow" with the correct argument, you would need either to "cast" your real-valued array into 'uint8', or normalize it by 255. (2) Perform 3x3 median filtering using the built-in MATLAB function "medfilt2". For this problem, the only argument you need to provide "medfilt2" with is the array you have created in step (1). Visualize the filtered image using "imshow". Remember to either cast the result to 'uint8' or normalize it before feeding it to "imshow". (3) Perform a second-pass median filtering on the filtered image that you have obtained from step (2). Visualize the two-pass filtered image. Compare it with the noisy input image and the 1-pass filtered image. (4) Download the noise-free image from here. Compute the PSNR values between (a) the noise-free image and the noisy input image, (b) the noise-free image and the 1-pass filtering output, and (c) the noise-free image and the 2-pass filtering output. Enter the three PSNR values in the box below. Enter the numbers to two decimal points.

And here's my attempt at the code :

A = imread('C:\xxxxxx\Week 5\digital-images-week5_quizzes-noisy.jpg');
B = im2double(A);
C = medfilt2(B,[3 3]);
D = medfilt2(C,[3 3]);
E = imread('C:\xxxxxx\Week 5\digital-images-week5_quizzes-original.jpg');
F = im2double(E);
MaxI=1;
MSE1= mean(mean((F- B).^2));
PSNR1 = 10*log10((MaxI^2)/MSE1);
MSE2 = mean(mean((F-C).^2));
PSNR2 = 10*log10((MaxI^2)/MSE2);
MSE3 = mean(mean((F-D).^2));
PSNR3 = 10*log10((MaxI^2)/MSE3);


-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