donutsk8r | Bonjour,
Je suis actuellement en stage au Canada et mon maitre de stage souhaiterait que je réalise un programme matlab permettant de tracker un objet sur une video et ensuite déterminer sa vitesse et tracer sa trajectoire (dendrite en photo ci-jointe). Dans mon école d'ingénieur, la formation a matlab est loin d'être une priorité et donc j'ai un peu de mal à me lancer dessus.
En regardant sur un forum, j'ai trouvé ce programme qui avait été posté et qui correspondrait bien voire très bien à ce que je veux.
Code :
- % 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(:,:)
|
Mais lorsque je tente de le lancer sur ma video, il y a un problème avec la fonction max_corr:
??? Undefined function or method 'max_corr' for input arguments of type 'double'.
Error in ==> vitesse at 85
[corr_max_row,corr_max_column]=max_corr(corr);
Cette fonction devait etre realiser par un programme indépendant de celui-ci. Quelqu'un de familier avec le template matching pourrait il m'expliquer ce que réalsie cette fonction?
merci beaucoup pour votre aide |