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

Monday, June 30, 2014

Fundamentals of Digital Image and Video Processing - Week 12 Solutions

Hi Coursera people,

This is a follower submitted code. Thanks to Mokam for this code.

Solution to question 8 of Week 12.

b = [-2,-6,-9,1,8,10,1,-9,-4,-3]';
S = 3;
for i = 1:10
for j = 1:10
A(i,j) = sin(i+j);
if (i==j)
A(i,j) = A(i,j) +1;
end
end
end
for i = 1: 10
Anorm(:,i) = A(:,i)/norm(A(:,i));
end
A = Anorm;
x = zeros(10,1);
r = b;
omg = [];
A_omg = [];
while nnz(x)<3 nbsp="" p="">i = 0;
maxnorm = 0;
for j=1:10
if(any(j==omg))
else
x_j = norm(transpose(A(:,j)) * r);
if(maxnormmaxnorm = x_j;
i = j;
end
end
end
omg = [omg, i];
A_omg = [A_omg, A(:,i)];
z_omega_star = pinv(A_omg' * A_omg) * A_omg' * b;
r = b - A_omg * z_omega_star;


x = zeros(10,1);
for j=1:length(omg)
index = omg(1,j);
x(index,1) = z_omega_star(j,1);
end
end

Answer is the matrix omg, in ascending order.

-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.

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.