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.



13 comments:

  1. thanks a lot ! i was repeating my small mistake in calculating ADI_neg, ! realised now

    ReplyDelete
  2. Thanks Vijay for week 11 solution...

    ReplyDelete
  3. I used your code and executed it. calculation of speed_X_Direction and speed_Y_Direction is returning two empty matrices but the calculation of total space occupied is correct. is their any modifications i need to do in your code? please help

    ReplyDelete
  4. sorry my mistake!!! wrong implementation of ADI-neg.........thanx for providing help

    ReplyDelete
  5. hi. you did a great job and without your help i would never go thruogh tis course. thank you soooo much!
    but i'm afraid i still need some help with the 12th week. will the solution appear here or should i keep searching somewhere else?

    ReplyDelete
    Replies
    1. Hi bro. Vijay is helping us. Please consider this subject. You can just spend a few minutes to find out the solution. It is a kind of Vijay that is putting his codes to help us to check our codes to them. Of course I solved some of codes without checking, but it would be better to check to find out new ideas.

      Best,
      Majid

      Delete
  6. Hello,

    can you help me with the last question of week 12? I dont understand :/

    ReplyDelete
  7. Hi Vijay...

    Can u update me Week 12 solutions

    ReplyDelete
  8. I see many of you are having trouble with week 12, here is my code:

    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
    i = 0;
    maxnorm = 0;
    for j=1:10
    if(any(j==omg))
    else
    x_j = norm(transpose(A(:,j)) * r);
    if(maxnorm<x_j)
    maxnorm = 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

    ReplyDelete
  9. Amazing information you shared. For getting information about Best Digital Marketing Services click on the link.

    ReplyDelete