Search code examples
imagematlabreal-time

GETSNAPSHOT in MATLAB is too slow


I have a code which acquires images from an analog camera using a USB video grabber. The HUGE problem for me is that whenever I use GETSNAPSHOT to get an image, the process takes a few seconds to perform, while the frame rate of the device is actually 30 frames per second. The funny thing is that the preview(vidObj) works perfectly fine. I understand that there's been a lot of discussion over why GETSNAPHOT is so slow, and there's one proposed solution that is very popular using TRIGGERCONFIG, but for some reason, that doesn't do anything to boost the image acquisition rate for me. I'll explain both cases below:

  1. Directly using GETSNAPSHOT

    obj = videoinput('winvideo', 2);
       while someconditionhere
       img= getsnapshot(cam); % extract frame i from the video
       imshow(img);
       %do stuff
    end
    
  2. With TRIGGERCONFIG

    obj = videoinput('winvideo', 2);
    triggerconfig(obj,'manual');
    start(obj);
    while someconditionhere
        img= getsnapshot(cam); % extract frame i from the video
        imshow(img);
        %do stuff
    end
    

I have tried to keep the preview window running in the background (a crude solution I found online) but then my while loop doesn't execute. Also, if in the first code, I add start(obj), then it gives me an error: "A timeout occurred during GETSNAPSHOT."

I'm running out of ideas and I have this due in a few hours. Any help would be greatly appreciated!


Solution

  • A friend of mine and I managed to solve the problem described in this question, and I just wanted to describe how, in case anyone needs to know:

     cam = imaq.VideoDevice('winvideo');
    
    • I didn't use VideoReader, as I did before, so that I could use the step function to plot each frame, and it's faster:

      I = step(cam);
      h = imagesc(I);
      set(h, 'EraseMode', 'none');
      
    • I captured the first frame before I started looping with the above code, and then I began the loop to acquire one frame at a time:

      img = step(cam);
      set(h, 'CData', img);
      
    • The above gets rid of imshow as well, besides getsnapshot, which slows down image plotting.

    This sped up the code significantly, up to 30 fps. Hope this helps!