MATLAB: Can anyone explain this code? what is meant by imagesc

12

for a = 1:size(video,4)
imagesc(video(:,:,:,a));
axis image off
drawnow;
end;

Best Answer

imagesc() is a display routine that scales the image to your display range (0-255) and displays it in the current (last used) axes. Your video is a 4-D array where the last index is the frame number. For RGB images I don't believe imagesc() does any scaling, though for gray scale images it does. So imagesc() is just like image() and imshow() for color images. I always use imshow() since for gray scale images imagesc() applies some weird colormap by default (though not for color images).
The third index is the color channel: 1 for red, 2 for green, and 3 for blue. This code does not have any comments and used not very descriptive variable names so it may be hard to understand what's going on. video(:,:,:,a) is the a'th frame of the video and it will be a full color RGB image. The colon means "all", so it's all rows, all columns, and all 3 color channels, which means a full color image.
The code could be written clearer like this:
% Get the total number of video frames.
totalNumberOfFrames = size(video,4)
% Loop over all frames displaying them as fast as possible.
for frameNumber = 1 : totalNumberOfFrames
thisColorFrame = video(:, :, :, frameNumber);
imagesc(thisColorFrame);
axis image off
drawnow; % force immediate repainting of the screen.
end
Related Question