Mostafiz
Mostafiz

Reputation: 1687

How to process various tasks like video acquisition parallel in Matlab?

I want to acquire image data from stereo camera simultaneously, or in parallel, save somewhere and read the data when need.

Currently I am doing

for i=1:100
start([vid1 vid2]);
imageData1=getdata(vid1,1);
imageData2=getdata(vid2,1);

%do several calculations%
....
end

In this cameras are working serially and it is very slow. How can I make 2 cameras work at a time???

Please help..

P.S : I also tried parfor but it does not help .

Regards

Upvotes: 1

Views: 869

Answers (1)

Robert Seifert
Robert Seifert

Reputation: 25232

No Parallel Computing Toolbox required!

The following solution can generally solve problems like yours:

First the videos, I just use some vectors as "data" and save them to the workspace, these would be your two video files:

% Creating of some "videos"
fakevideo1 = [1 ; 1 ; 1];
save('fakevideo1','fakevideo1');
fakevideo2 = [2 ; 2 ; 2];
save('fakevideo2','fakevideo2');

The basic trick is to create a function which generates another instance of Matlab:

function [ ] = parallelinstance( fakevideo_number )

% create command
% -sd (set directory), pwd (current directory), -r (run function) ...
% finally "&" to indicate background computation
command = strcat('matlab -sd',{' '},pwd,{' '},'-r "processvideo(',num2str(fakevideo_number),')" -nodesktop -nosplash &');
% call command
system( command{1} );

end

Most important is the use of & at the end of the terminal command!

Within this function another function is called where the actual video processing is done:

function [] = processvideo( fakevideo_number )

% create file and variable name
filename = strcat('fakevideo',num2str(fakevideo_number),'.mat');
varname = strcat('fakevideo',num2str(fakevideo_number));
% load video to workspace or whatever
load(filename);
A = eval(varname);
% do what has to be done
results = A*2;
% save results to workspace, file, grandmothers mailbox, etc.
save([varname 'processed'],'results');
% just to show that both processes run parallel
pause(5)
exit

end

Finally call the two processes in your main script:

% function call with number of video: parallelinstance(fakevideo_number)
parallelinstance(1);
parallelinstance(2);

My code is completely executable, so just play around a bit. I tried to keep it simple. After all you will find two .mat files with the processed video "data" in your workspace.

Be aware to adjust the string fakevideo to name root of all your video files.

Upvotes: 1

Related Questions