Potaito
Potaito

Reputation: 1190

Matlab: How to distribute workload?

I am currently trying to record footage of a camera and represent it with matlab in a graphic window using the "image" command. The problem I'm facing is the slow redraw of the image and this of course effects my whole script. Here's some quick pseudo code to explain my program:

    figure
    while(true)
      Frame = AcquireImageFromCamera();  % Mex, returns current frame
      image(I);
    end

AcquireImageFromCamera() is a mex coming from an API for the camera. Now without displaying the acquired image the script easily grabbs all frames coming from the camera (it records with a limited framerate). But as soon as I display every image for a real-time video stream, it slows down terribly and therefore frames are lost as they are not captured.

Does anyone have an idea how I could split the process of acquiring images and displaying them in order to use multiple cores of the CPU for example? Parallel computing is the first thing that pops into my mind, but the parallel toolbox works entirely different form what I want here...

edit: I'm a student and in my faculty's matlab version all toolboxes are included :)

Upvotes: 2

Views: 265

Answers (3)

shoelzer
shoelzer

Reputation: 10708

Running two threads or workers is going to be a bit tricky. Instead of that, can you simply update the screen less often? Something like this:

figure
count = 0;
while(true)
    Frame = AcquireImageFromCamera();  % Mex, returns current frame
    count = count + 1;
    if count == 5
        count = 0;
        image(I);
    end
end

Another thing to try is to call image() just once to set up the plot, then update pixels directly. This should be much faster than calling image() every frame. You do this by getting the image handle and changing the CData property.

h = image(I); % first frame only
set(h, 'CData', newPixels); % other frames update pixels like this

Note that updating pixels like this may then require a call to drawnow to show the change on screen.

Upvotes: 4

RyanD
RyanD

Reputation: 116

I'm not sure how precise your pseudo code is, but creating the image object takes quite a bit of overhead. It is much faster to create it once and then just set the image data.

figure
himg = image(I)
while(true)
  Frame = AcquireImageFromCamera();  % Mex, returns current frame
  set(himg,'cdata',Frame);
  drawnow; %Also make sure the screen is actually updated.
end

Upvotes: 2

Prashanth
Prashanth

Reputation: 1322

Matlab has a video player in Computer vision toolbox, which would be faster than using image().

player = vision.VideoPlayer
while(true)
  Frame = AcquireImageFromCamera();  % Mex, returns current frame
  step(player, Frame);
end

Upvotes: 2

Related Questions