Why does Matlab close when I try to load a video file?
4 次查看(过去 30 天)
显示 更早的评论
I'm a student working on a project in a lab studying brain injury. I have basic experience in Matlab, mostly doing Excel type work. I would like to use Matlab to watch our neural recording and video of the behavior simultaneously. I wrote a simple program and it worked for a short test file (~30 seconds long). However, when I try to load a sample experiment (~30 minutes long), Matlab closes without displaying and error message. Any idea what would cause this error?
I'm guessing that it is because of the size of the file. It's .mp4 at 1280x960 at 29.97fps RGB24. I tried to cut the resolution in half and convert to gray scale but I was unsuccessful. How do I convert the video to a lower resolution 8bit gray scale video? My basic code is below. Please let me know if you have any questions. Thank you for help!
%Load Data (.CSV file exported from FAST ANALYSIS
[FileName1,PathName,~] = uigetfile('.csv','Please load .CSV file exported from FAST ANALYSIS');
addpath(PathName);
Data = csvread(FileName1);
Data(1:7,:) = [];
clear PathName
%Create TIME
[Frame,~] = size(Data); %FRAME = number of data points (length of DATA)
RecordingFrequency = 4; %Recording at 4Hz... change if FREQUENCY changes
Duration = Frame/RecordingFrequency;
Time = 0:0.25:Duration; %Create TIME
Time(Frame) = []; %Delete last FRAME (trim vectors to the same length)
Time = Time'; %TRANSPOSE TIME (ROW to COLUMN VECTOR)
clear RecordingFrequency n Duration
%Create FILE VARIABLES
TTL = Data(:,1);
Ch1 = Data(:,2);
Ch2 = Data(:,3);
Ch3 = Data(:,4);
Ch4 = Data(:,5);
%Ch13 = Data(:,6);
%Ch24 = Data(:,7);
%Construct a VideoReader object
[FileName2,PathName,~] = uigetfile('.mp4','Please load BEHAVIOR VIDEO file');
addpath(PathName);
vidObj = VideoReader(FileName2);
clear PathName
%Determine the height and width of the frames
vidHeight = vidObj.Height;
vidWidth = vidObj.Width;
%Create a MATLAB movie structure array, s.
s = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),'colormap',[]);
%Read one frame at a time using readFrame until the end of the file is reached.
%Append data from each video frame to the structure array.
k = 1;
while hasFrame(vidObj)
s(k).cdata = readFrame(vidObj);
k = k+1;
end
%CONVERT DATA into SPLINE (UP SAMPLE from 4Hz to 29.97Hz)
TotalFrames = vidObj.FrameRate * vidObj.CurrentTime;
FrameLength = 1:Frame;
xx = 0:1/vidObj.FrameRate:TotalFrames;
Times = spline(FrameLength,Time,xx);
Times = Times';
Ch1s = spline(FrameLength,Ch1,xx);
Ch1s = Ch1s';
Ch2s = spline(FrameLength,Ch2,xx);
Ch2s = Ch2s';
Ch3s = spline(FrameLength,Ch3,xx);
Ch3s = Ch3s';
Ch4s = spline(FrameLength,Ch4,xx);
Ch4s = Ch4s';
clear xx
%CREATE PLOT WINDOW
xmin = 1; xmax = 251;
ymin = 0; ymax = 50;
%KEYPRESS == 0
val = -1;
figure('Name','The Amazing Analyzer by Dan','NumberTitle','off')
%PLOT FILE DATA
while val ~= 13;
clf
str1 = 'Hello and welcome to The Amazing Analyzer by Dan!';
str2 = 'Quick Start Guide';
str3 = 'Play forward = Right Arrow';
str4 = 'Play backward = Left Arrow';
str5 = 'Advance 10 frames = Up Arrow';
str6 = 'Go back 10 frames = Down Arrow';
str7 = 'Pause = Space Bar';
str8 = 'Enter = End Session';
str9 = 'Good luck!';
text(1,10,str1)
text(1,8,str2)
text(2,7,str3)
text(2,6,str4)
text(2,5,str5)
text(2,4,str6)
text(2,3,str7)
text(2,2,str8)
text(4,1,str9)
axis([0 10 0 12])
axis off
if val == -1; %PAUSE to START the GUI
k = waitforbuttonpress;
end
%Get KEYPRESS value
f=gcf;
val=double(get(f,'CurrentCharacter'));
%PLAYBACK BUTTONS
if val == 28; %LEFT ARROW - PLAY BACKWARDS
if xmin > 1;
xmin = xmin-1;
xmax = xmax-1;
end
end
if val == 29; %RIGHT ARROW - PLAY FORWARD
if xmax < TotalFrames;
xmin = xmin+1;
xmax = xmax+1; %ADD statement to prevent going past the end of the vector
end
end
if val == 30; %UP ARROW - ADVANCE 10 FRAMES
if xmax < TotalFrames-10;
xmin = xmin+10;
xmax = xmax+10; %ADD statement to prevent going past the end of the vector
end
end
if val == 31; %DOWN ARROW - GO BACK 10 FRAMES
if xmin > 10;
xmin = xmin-10;
xmax = xmax-10;
end
end
xavg1 = (xmin+xmax)/2;
xavg2 = (Time(xmin,1)+Time(xmax,1))/2;
y(1:50) = xavg2;
x = 1:1:50;
%SHOW IMAGE (TOP) & PLOT DATA (BOTTOM)
subplot(2,1,1);
image(s(xavg1).cdata)
title(FileName2)
axis off
subplot(2,1,2)
plot (Times,Ch1s,'r')
hold on
plot (Times,Ch2s,'b')
plot (Times,Ch3s,'m')
plot (Times,Ch4s,'c')
plot(y,x,'k--')
title(FileName1)
ylabel('Glutamate (mM)')
xlabel('Frame')
axis([Time(xmin,1) Time(xmax,1) ymin ymax])
%PLAYBACK control
if val == 28; %LEFT ARROW - PLAY BACKWARDS
pause(1/vidObj.FrameRate);
end
if val == 29; %RIGHT ARROW - PLAY FORWARD
pause(1/vidObj.FrameRate);
end
if val == 30; %UP ARROW - ADVANCE 10 FRAMES
pause;
end
if val == 31; %DOWN ARROW - GO BACK 10 FRAMES
pause;
end
if val == 32; %SPACE BAR - GO BACK 10 FRAMES
pause;
end
if xmin == 1; %PREVENT OVER RE-WINDING
pause;
end
if xmax == TotalFrames; %PREVENT OVER PLAYING
pause;
end
end
beep; pause (0.5); beep;
1 个评论
Walter Roberson
2015-9-11
How big is that full file? My estimate is that it could be up to 63 gigabytes uncompressed.
How much memory do you have, and are you using 32 bit or 64 bit MATLAB?
采纳的回答
Dinesh Iyer
2015-9-14
Hi,
It looks you are reading in all the frames in the video into MATLAB albeit one frame at a time. This is most likely the cause of MATLAB shutting down.
The only workaround that I can suggest is to read a frame from the video as and when you need it instead of loading the entire video into memory.
Dinesh
2 个评论
Walter Roberson
2015-9-14
colormap(gray(256))
With the movie parameters you gave above, after conversion to gray, you would have about 21 Gb of movie data in memory simultaneously.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!