1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
% this code is able to track an object within a video, to plot the
% trajectory and to calculate the velocity of the object
% initialisations
template_rows = 1;
template_columns = 1;
template_xmin = 1;
template_ymin = 1;
% importation of the video
video = aviread('number 2.avi');
% video_info is used to store the information about the video
% (number of frames, format of frames, number of frames per second...)
video_info = aviinfo('number 2.avi');
NumFrames = video_info.NumFrames;
FPS = video_info.FramesPerSecond;
% the crop function is used here to extract the ultrasound image from all
% frames
% for example sometimes the frames have a Title and some comments that the
% program doesn't need for the processing
% this enables to reduce the running time of the program
% the user selects a rectangle containing the ultrasound image
[crop,rect_image2] = imcrop(video(1).cdata);
% the loop extract the ultrasound image part of each frames
for j=1:NumFrames
video(j).cdata = imcrop(video(j).cdata,rect_image2);
video(j).cdata = rgb2gray(video(j).cdata);
end
image1 = video(1).cdata;
[frame_rows,frame_columns] = size(image1);
% the crop function is used here to select the object that he is interested
% in finding the trajectory and velocity of
[template,rect_image1] = imcrop(image1);
% the code below is saving informations about the rectangle selected by the
% user (size, position ...)
size_template = size(template);
template_rows = size_template(1);
template_columns = size_template(2);
template_width = template_columns - 1;
template_height = template_rows - 1;
if rect_image1(1)<1
rect_image1(1)=1;
end
if rect_image1(2)<1
rect_image1(2)=1;
end
% the coordinates of the object are saved
x_coordinates(1,1) = round(rect_image1(1))+((template_columns-1)/2);
y_coordinates(1,1) = round(rect_image1(2))+((template_rows-1)/2);
% the loop below computes the cross-correlation and finds the coordinates of
% the object in all frames of the video
for k=2:NumFrames
image2 = video(k).cdata;
corr = normxcorr2(template,image2);
% the program using the function max_corr
[corr_max_row,corr_max_column]=max_corr(corr);
% this part is computing the updating of the template
template_xmin = corr_max_column - template_width;
template_ymin = corr_max_row - template_height;
rect_template = [template_xmin, template_ymin, template_width, template_height];
template = imcrop(image2,rect_template);
% the coordinates of the object are saved
x_coordinates(1,k) = corr_max_column - ((template_columns-1)/2);
y_coordinates(1,k) = corr_max_row - ((template_rows-1)/2);
end
for i=1:NumFrames
y_coordinates(1,i)=frame_rows - y_coordinates(1,i);
end
% the program asks the user to enter the frame length scale
Scale = input('Dimension of the frames (in mm):\n');
% the pixel length and the time between two consecutive frames are
% calculated
pixel_length = Scale/frame_columns;
deltaT = 1/FPS;
% the loop below calculates the velocity of the object for each frames
for k=2:NumFrames-1
offset_x = sqrt((x_coordinates(1,k+1)-x_coordinates(1,k-1))^2);
offset_y = sqrt((y_coordinates(1,k+1)-y_coordinates(1,k-1))^2);
velocity(1,k-1) = (sqrt(offset_x^2 + offset_y^2))/(2*deltaT);
end
% this corresponds to the plotting of the trajectory
subplot(2,1,1), plot(x_coordinates,y_coordinates,'o');
axis([0 frame_columns 0 frame_rows]);
xlabel('Pixels in the x axis');
ylabel('Pixels in the y axis');
title('trajectory of the object')
subplot (2,1,2), plot(velocity);
title ('velocity of the object');
% the code below displays the velocity table
velocity(:,:) |
Partager