MATLAB Answers


Matlab Motion Estimation Code

Asked by James
on 14 Jul 2011


Can somebody please explain what the code I have pasted below does, which can be found from the Link - Algorithms

for i = 0:30
    imgINumber = i;
    imgPNumber = i+2;
    if imgINumber < 10
        imgIFile = sprintf('./%s/gray/%s00%d.ras',imageName, imageName, imgINumber);
    elseif imgINumber < 100
        imgIFile = sprintf('./%s/gray/%s0%d.ras',imageName, imageName, imgINumber);
    if imgPNumber < 10
        imgPFile = sprintf('./%s/gray/%s00%d.ras',imageName, imageName, imgPNumber);
    elseif imgPNumber < 100
        imgPFile = sprintf('./%s/gray/%s0%d.ras',imageName, imageName, imgPNumber);

I want to perform some of the motion estimation algorithms using my own mpeg video, but I need to know what this code does. It would be very much appreciated, as it will help me with my final year project.



No products are associated with this question.

2 Answers

Answer by bym
on 14 Jul 2011

it creates a string based upon the image name & number. It pads the number with zeros to get a string with a consistent number of characters


Answer by Walter Roberson
on 14 Jul 2011

It is code for selecting file names from a directory relative to the current directory. The subdirectory involved is indicated by imageName and then the 'gray' subdirectory of that. The file names involved will start with that same name, followed by a 3 digit number, followed by '.ras'. For any one pass of the for loop over "i", two files are selected, one using i as the number, and the other using i+2

The code could be replaced with

for i = 0:30
  imgIFile = sprintf('./%s/gray/%s%03d.ras', imageName, imageName, i);
  imgPfile = sprintf('./%s/gray/%s%03d.ras', imageName, imageName, i+2);


on 14 Jul 2011

That simplified code is helpful.

What does this part mean though - './%s/gray/%s%03d.ras'

Also, if I had all the images in one folder called gray, then what would the code look like?

'./%s/gray/%s%03d.ras' is an sprintf format string. It means that the output string is to start with a period, then a slash, then the next parameter (the 2nd to the call) should be interpreted as a string and placed in the buffer, followed by a slash and the literal word 'gray', followed by a slash, then the next parameter (the 3rd to the call) should be interpreted as a string and placed in the buffer, followed by a slash, then the next parameter (the 4th to the call) should be interpreted as a string and placed in the buffer, then the next parameter (the 5th to the call) should be converted to be a 3 digit integer with leading zeroes, and finally that a literal period and 'ras' should be added to the buffer.

If all the images are in a folder called gray that is a subdirectory of your current directory, then the code would look like:

for i = 0:30
imgIFile = sprintf('./gray/%s%03d.ras', imageName, i);
imgPfile = sprintf('./gray/%s%03d.ras', imageName, i+2);

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!