How can I reproduce this cuboid plot in MATLAB?
I dumped some frames of a video and would like to generate a cuboid out of them, like in the following sample image:
开发者_运维知识库I was wondering if there exists a MATLAB function to do such a plot?
It's not clear what format your video frames are in to start with, so I'll assume they're loaded into MATLAB as a structure array of movie frames (as described here). I'll create some sample movie data (a single image repeated 200 times) to first show you how you can load the first frame into an image as well as create an image out of the top and side edges of all your frames (to use as the top and side faces of the cuboid):
M = repmat(im2frame(imread('peppers.png')),1,200); %# Sample movie data
nFrames = numel(M); %# The number of frames
face1 = frame2im(M(1)); %# Get the image for the front face
[R,C,D] = size(face1); %# Get the dimensions of the image
face2 = zeros(R,nFrames,3,'uint8'); %# Initialize the image for the side face
face3 = zeros(nFrames,C,3,'uint8'); %# Initialize the image for the top face
for k = 1:nFrames %# Loop over the frames
img = frame2im(M(k)); %# Get the image for the current frame
face2(:,k,:) = img(:,end,:); %# Copy the side edge to the side face image
face3(k,:,:) = img(1,:,:); %# Copy the top edge to the top face image
end
The above code assumes that the movie frames are RGB images as opposed to indexed images. If they are indexed images you will have to get the additional colormap argument from the function FRAME2IM and then use that to convert the image to an RGB image using the function IND2RGB.
Next, you can plot each side of the cube as a texture-mapped surface using the SURF function:
offset = nFrames/sqrt(2); %# The offset in pixels between the back
%# corners and front corners of the
%# displayed cuboid
surf([0 C; 0 C],... %# Plot the front face
[R R; 0 0],...
[0 0; 0 0],...
'FaceColor','texturemap',...
'CData',face1);
hold on; %# Add to the existing plot
surf([C C+offset; C C+offset],... %# Plot the side face
[R R+offset; 0 offset],...
[0 0; 0 0],...
'FaceColor','texturemap',...
'CData',face2);
surf([0 C; offset C+offset],... %# Plot the top face
[R R; R+offset R+offset],...
[0 0; 0 0],...
'FaceColor','texturemap',...
'CData',face3);
axis equal %# Make the scale on the x and y axes equal
view(2); %# Change the camera view
axis off %# Turn off display of the axes
set(gcf,'Color','w'... %# Scale the figure up
'Position',[50 50 C+offset+20 R+offset+20]);
set(gca,'Units','pixels',... %# Scale the axes up
'Position',[10 10 C+offset R+offset]);
And here's the resulting figure:
精彩评论