How can I acquire images from one camera to parallel processes to perform actions in the background of a script?

I want to get a live video stream and find find a single circle in each image. This works great with a single progessor at lower resolutions (30 fps), but bogs down at higher resolution (4 fps). Is it possible to get an image and start processing it, and while that process is still going get the next image and start processing as a separate process? I would also like to have a primary loop that takes data from the parallel background loops and procedes in a determanistic manner (although if they came in slightly out of order I could probably deal with that). I have tried using a parfor loop, but it does not seem to have access to the camera object created outside the loop. I have tried creating the camera object inside spmd, but it fails because there is only one camera. Below is the non parallel code. The parfor loop I tried was exactly the same other than the for call.
delete(imaqfind)
cam= videoinput('winvideo', 1,'RGB24_320x240');
stop(cam)
triggerconfig(cam, 'manual');
cam.TriggerRepeat = inf;
cam.FramesPerTrigger = 1;
start(cam);
trigger(cam);
frame = getdata(cam);
for idx=1:500
if mod(idx,50) % Prevent memory leak.
flushdata(cam);
end
trigger(cam);
frame = getsnapshot(cam);
%frame= imcrop(frame,vid.ROIPosition);
if islogging(cam)
while islogging(cam) % Pause while video is being logged.
pause(0.001) % This seems to prevent skips in data for very fast reloads.
end
end
frameg=rgb2gray(frame);
[ball,bbox]=imfindcircles(frameg,[20,30],'ObjectPolarity','dark','Sensitivity',0.92);%
detectedLocation=ball;
end

Answers (1)

No it is not possible. The camera objects cannot be shared between processes.
Instead you can have one process reading from the camera and sending it to be processed. If you use parfeval then when you wait for the "futures" in order you created you effectively wait for the process to be finished in order dispatched even if some other process finishes first.

Asked:

on 4 Nov 2020

Answered:

on 4 Nov 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!