在Matlab中刷新imshow?

时间:2015-04-29 07:13:16

标签: matlab video plot avi

我想将此答案的code转换为imshow。 它通过

在MOVIE2AVI中创建一部电影
%# preallocate
nFrames = 20;
mov(1:nFrames) = struct('cdata',[], 'colormap',[]);
%# create movie
for k=1:nFrames
   surf(sin(2*pi*k/20)*Z, Z)
   mov(k) = getframe(gca);
end
close(gcf)
movie2avi(mov, 'myPeaks1.avi', 'compression','None', 'fps',10);

我的伪代码

%# preallocate
nFrames = 20;
mov(1:nFrames) = struct('cdata',[], 'colormap',[]);
%# create movie
for k=1:nFrames
    imshow(signal(:,k,:),[1 1 1]) % or simply imshow(signal(:,k,:))
    drawnow
    mov(k) = getframe(gca);
end
close(gcf) 
movie2avi(mov, 'myPeaks1.avi', 'compression','None', 'fps',10);

然而,这会在屏幕上创建动画,但它只保存大小为0 kB的AVI文件。运行surf命令后文件myPeaks1.avi正确存储,但不是imshow。 我不确定命令drawnow

实际案例代码

%% HSV 3rd version 
% https://stackoverflow.com/a/29801499/54964
rgbImage = imread('http://i.stack.imgur.com/cFOSp.png');
% Extract blue using HSV 
hsvImage=rgb2hsv(rgbImage);
I=rgbImage;
R=I(:,:,1);
G=I(:,:,2);
B=I(:,:,3);
R((hsvImage(:,:,1)>(280/360))|(hsvImage(:,:,1)<(200/360)))=255;
G((hsvImage(:,:,1)>(280/360))|(hsvImage(:,:,1)<(200/360)))=255;
B((hsvImage(:,:,1)>(280/360))|(hsvImage(:,:,1)<(200/360)))=255;
I2= cat(3, R, G, B);

% Binarize image, getting all the pixels that are "blue"
bw=im2bw(rgb2gray(I2),0.9999);

% The label most repeated will be the signal. 
% So we find it and separate the background from the signal using label.
% Label each "blob"
lbl=bwlabel(~bw);

% Find the blob with the highes amount of data. That will be your signal.
r=histc(lbl(:),1:max(lbl(:)));
[~,idxmax]=max(r);
% Profit!
signal=rgbImage;
signal(repmat((lbl~=idxmax),[1 1 3]))=255;
background=rgbImage;
background(repmat((lbl==idxmax),[1 1 3]))=255;

%% Error Testing
comp_image = rgb2gray(abs(double(rgbImage) - double(signal)));
if ( sum(sum(comp_image(32:438, 96:517))) > 0 )
    break; 
end

%% Video       
% 5001 units so 13.90 (= 4.45 + 9.45) seconds. 
% In RGB, original size 480x592. 
% Resize to 480x491
signal = signal(:, 42:532, :); 
% Show 7 seconds (298 units) at a time. 
% imshow(signal(:, 1:298, :)); 

%% Video VideoWriter
% movie2avi deprecated in Matlab
% https://stackoverflow.com/a/11054155/54964
% https://stackoverflow.com/a/29952648/54964
%# figure
hFig = figure('Menubar','none', 'Color','white');
Z = peaks; 
h = imshow(Z, [], 'InitialMagnification',1000, 'Border','tight');
colormap parula; axis tight manual off; 
set(gca, 'nextplot','replacechildren', 'Visible','off');
% set(gcf,'Renderer','zbuffer'); % on some Windows


%# preallocate
N = 40; % 491;
vidObj = VideoWriter('myPeaks3.avi');
vidObj.Quality = 100;
vidObj.FrameRate = 10;
open(vidObj);

%# create movie
for k=1:N
   set(h, 'CData', signal(:,k:k+40,:))
   % drawnow
   writeVideo(vidObj, getframe(gca));
end

%# save as AVI file
close(vidObj);

如何通过imshow或相应的方式替换绘图功能? 如何正确存储动画?

0 个答案:

没有答案