Simultaneous playback of multiple videos with MATLAB - matlab

I searched the internet and stack overflow but could not find a solution or even helpful hints to my problem.
I need to write a specialised video annotation software in MATLAB which has to be capable to play multiple videos (at least 2) simultaneously on a GUI. The video files are XVID-encoded. Up to now, I basically just adjusted the mathworks.com example for video playback (xylophon.avi, see movie() description).
I am familiar with the mmreader, VideoReader, movie and implay functions but still I am facing two issues:
Even if I read in only a small number of frames (like in the xylophon.avi example), my progam soon exceeds available memory. Also, it takes quite long to read in even relatively few frames (say 100).
The movie() function is sycnhronous, so the second video does not start until the first video completed. How can I call two movie()-functions concurrently? Or is there another way to show two (or more) videos simultaneously?
Any suggestions? Thanks!

First of all MATLAB is not multithreaded. Doing two things in parallel will be difficult. Try to breakout to Java. Matlab uses JIDE as their graphical front-end which is built on Swing. Use MATLAB Builder JA in order to compile your MATLAB code to Java, or add your own 'Panels' to the IDE as shown in this question.

You could display the videos in two different windows and start the playback simultaneously by giving the videos a handle and calling its undocumented play function. This removes any struggle you might have with videos of unequal length as well.
handle1 = implay('file1.mp4');
handle2 = implay('file2.mp4');
handle1.Parent.Position = [100 100 640 480];
handle2.Parent.Position = [740 100 640 480];
play(handle1.DataSource.Controls)
play(handle2.DataSource.Controls)

In principle, you can display each video frame as an image and alternate updating each video, but getting it to play at exactly the right frame rate might be difficult.
Try something like the following. This probably won't work as-is, but you should be able to update it.
v1 = VideoReader(firstVideo)
v2 = VideoReader(secondVideo)
i1 = 0;
i2 = 0;
while i1 < v1.NumberOfFrames && i2 < v2.NumberOfFrames
if i1 < v1.NumberOfFrames
i1 = i1+1;
subplot(1,2,1)
image(v1.read(i1))
end
if i2 < v2.NumberOfFrames
i2 = i2+1;
subplot(1,2,2)
image(v2.read(i2))
end
drawnow
end

Related

MATLAB: Combine all videos in directory

I have directories that each have several short (~10 second) .avi videos. Does anybody know how I can concatenate all of the videos in a specific directory in alphabetical order to form one single video?
I would try to use VLC, but I have to do this for over a thousand different directories. I didn't realize this would be so difficult, but not able to find anything on Google.
More specifics:
For each directory I want to perform this action on, all videos are guaranteed to be:
.avi,MJPG,20fps,640x480 resolution,no audio,between less than 1 second to 15 seconds long
I'd like the single video file to play just as if I played the individuals back-to-back.
If there's any other specifics I missed please let me know.
The combined videos are intended to all be put into the same directory and given to another person to perform their own video processing on with Matlab. They'll be doing something with either crosscorrelation or machine learning to try and identify a particular object in the videos.
You can use a combination of the VideoReader and VideoWriter (see doc for more examples). Iterate through your video files in alphabetical order and "stream" them into a new file.
I threw together some (untested) code. I have no idea how fast this is, though:
cd(VIDEO_DIRECTORY);
tmp = dir('*.avi'); % all .avi video clips
videoList = {tmp.name}'; % sort this list if necessary! sort(videoList) might work
% create output in seperate folder (to avoid accidentally using it as input)
mkdir('output');
outputVideo = VideoWriter(fullfile(workingDir,'output/mergedVideo.avi'));
% if all clips are from the same source/have the same specifications
% just initialize with the settings of the first video in videoList
inputVideo_init = VideoReader(videoList{1}); % first video
outputVideo.FrameRate = inputVideo_init.FrameRate;
open(outputVideo) % >> open stream
% iterate over all videos you want to merge (e.g. in videoList)
for i = 1:length(videoList)
% select i-th clip (assumes they are in order in this list!)
inputVideo = VideoReader(videoList{i});
% -- stream your inputVideo into an outputVideo
while hasFrame(inputVideo)
writeVideo(outputVideo, readFrame(inputVideo));
end
end
close(outputVideo) % << close after having iterated through all videos

How do I control which channel sound is played through using MATLAB?

I'm a novice MATLAB user, so apologies if the question is very basic. I need a .wav sound file to be played in a single, specific channel -- let's say the left channel. As it is, my code reads in the sound file, and I add in a column of zeros to nullify the channel I don't want, like so:
currentDir = pwd;
soundFile = [currentDir '\sound1.wav']; % load the file
[y, Fs] = audioread(soundFile); % read the file in
soundData(:,1) = y(:,1); % keeps sound for the left channel
soundData(:,2) = 0; % nullifies the right channel
sound = audioplayer(soundData,Fs);
play(sound);
As it stands, the code currently produces a sound that is full volume in the left speaker, and half volume (but still very much audible) in the right speaker. I have tried this with at least 20 .wav files, with the same result.
In case it's relevant, this happens even when I write in code that explicitly matches the length of the sound variable in 0s, like so:
[y, Fs] = audioread(soundFile);
silentChannel = zeros(size(y));
soundData(:,1) = y(:,1); % keeps sound for the left channel
soundData(:,2) = silentChannel(:,2); % nullifies the right channel
Does anybody know what I'm doing wrong, or have any thoughts?
Your code is definitely correct and it should only play audio in the left channel. I suspect that the problem is caused by a sound-card/driver issues. Allow me suggest the following troubleshooting steps:
Save your output as a wav file using audiowrite('output.wav', soundData, Fs). Play this using a different audio player, such as Audacity. If you still hear output in both channels, it must be a sound-card/driver issue.
Assuming that you are using a Windows PC (going by the syntax in your file path), make sure all sound enhancements are disabled. How to do this depends on the PC. If there is a third-party app controlling the playback settings, you'd have to use that. Otherwise find the settings shown in the picture below in the Control Panel.
In MatLab the expected method for playing sound is the method sound(data,Fs)
To control the channel the sound emits on, you'll want to know how sound() reads data.
data is a matrix with the columns representing channels, and with the rows holding the samples of the waveform for a given sampling fequency Fs
here is a simple implementation.
function treismanwolfe()
close all
clear all
clc
Fs = 40000;
tau = 2*pi();
t = 0:tau/(Fs-1):tau/2;
left = sin(t).*(sin(t*200)+sin(t*1600));
left= left/max(abs(left));
left = left'; %turn column vector into row
right = sin(t).*(sin(t*800)+sin(t*400));
right= right/max(abs(right));
right = right'; %turn column vector into row
data = [left,right*0]; %multiply either by 0 to nullify
sound(data,Fs); %so you can hear it.
end
I hope this works for you. Enjoy!
When I run your code the audio output is only audible in the left channel as you have specified.
#Austin Kootz' version with the sound()-function is just as good and also produces what you're looking for, but with audioplayer-object you have the ability to stop the playback in the middle of the playback (as you probably know)
Have you tried converting your .wav to another format to see if that makes a change?

Parallel image processing with MATLAB

I have written a MATLAB program which performs calculations on a video. I think it is a perfect candidate to adapt to multiple cpu cores as there is a lot of averaging done. I am just sturggling with the first bit of sending each section of frames to each lab. Say (for simplicity) it is a 200 frame file. I have read some guides and using SPMD gotten this.
spmd
limitA = 1;
limitB = 200;
a = floor(((limitB-limitA)/numlabs)*(labindex-1)+limitA);
b = floor((((limitB-limitA)/numlabs)*(labindex-1)+limitA)+(((limitB-limitA)/numlabs)));
fprintf (1,'Lab %d works on [%f,%f].\n',labindex,a,b);
end
It successfully outputs that each worker will work on their respective section (Eg Lab 1 works on 1:50, Lab 2 50:100 etc).
Now where I am stuck is how do actually make my main body of code work on each Lab's section of frames. Is there a tip or an easy way to now edit my main code so it knows what frames to work on based on labindex? Adding spmd to the loop results in an error hence my question.
Thanks
Following on from what you had, don't you simply need something like this:
spmd
% each lab has its own different values for 'a' and 'b'
for idx = a:b
frame = readFrame(idx); % or whatever
newFrame = doSomethingWith(frame);
writeFrame(idx, newFrame);
end
end
Of course, if that's the sort of thing you're doing, you may need to serialize the frame writing (i.e. make sure only one process at a time is writing).

Matlab: exactly timed getsnapshot for real-time event analyzing

I got a camera triggered by external source at a constant rate of 1/0.14s, and Matlab for-loop is used to take timed pictures for real-time measurements. However, the elapsed time for 1 execution of "getsnapshot" is so different each time. Sometimes I get 1 picture with less than 0.14s and
sometimes it takes 0.5s to take a picture. Is there anyway to synchronize the "getsnapshot"
with the external trigger? or at least make the "getsnapshot" exactly timed?
The following is my code:
vid = videoinput('camera');
preview(vid);
for i=1:100
data=getsnapshot(vid);
%...data processing...
%....
clear data
end
First, delete the preview(vid) line, this is probably why the rep. rate you are getting is weird. When you take data you don't need this preview option on, as it takes resources from your cpu.
Then, you may need to set the camera properties on the imaq toolbox to be in triggered mode. For example, for a gentl camera type this might look something like:
triggerconfig(vid, 'hardware', 'DeviceSpecific', 'DeviceSpecific');
src = getselectedsource(vid);
src.FrameStartTriggerMode = 'On';
src.FrameStartTriggerActivation = 'RisingEdge';
src.FrameStartTriggerDelayAbs = 0;
src.FrameStartTriggerSource = 'Line1';
src.FrameStartTriggerOverlap = 'Off';
Then, with some camera's you can read their trigger out, that is whenever the camera is exposing, it sends a ttl to some output. Matlab way to define it is something like:
src.SyncOut1SyncOutPolarity = 'Normal';
src.SyncOut1SyncOutSource = 'Exposing';
Again, you'll need to play with your camera's options in the imaq tool.
Also, the data processing step that you take afterwards may take some time, so benchmark it to see you can take data and analyze it on the fly without bottlenecks happening.
Last, you can use getdata instead of getsnapshot (read the documentation to see their difference) , and in the form: [img, time, metadata] = getdata(vid);
This will give you timestamps for each image taken, so you can see what's happening. Also, instead of clear data use flushdata(vid) to keep the vid object from completely filling the memory buffer (though if you only run 100 iterations in a loop, you should be fine).

How to make an animation from multiple results?

I have some calculation with matrices and have set my loop to run for (let's say) 50 times.
I also assigned a color to each value so I can get a picture in the end of these matrices based on their values.
What I don't know is - how to make an animation from this multiple images I get in each turn.
Is it possible?!
The following code is what I have used previously to produce an .avi file
n = 15;
p = randperm(n);
figure('Color','white');
fcount = 0;
for k = 1:n-1
% produce the plot
[idx,idx] = min(p(k:n));
p(idx+k-1) = p(k);
p(k) = k;
plot(p,'*')
% Make sure plot updates before we capture the contents
pause(0.1)
F(k) = getframe(gcf); %#ok
end
movie2avi(F,'so1.avi','fps',2,'quality',100);
However, there seems to be some issues with the avi codec now for use with Windows XP, for example see this thread.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/271172
I had the same problem; the avi file produced with the default Indeo codec would not run in Windows Media Player. Using a different codec, such as
movie2avi(F,'so1.avi','fps',2,'quality',100,'compression','Cinepak');
solved the problem. You may need to experiment to find a working combination.
Hth, Darren
I'm not sure what you're trying to do. One option is to use the MS-GIF animator, although 50 images is a bit much. See http://en.wikipedia.org/wiki/Microsoft_GIF_Animator for info. Considering the number of images, you might want to create a powerpoint document.