Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 620
Kaya311 Avatar asked Nov 20 '25 13:11

Kaya311


2 Answers

Could you try the following link. Someone has done real time mouse pointer control in matlab, so they should have got pretty fast response. http://www.mathworks.com/matlabcentral/fileexchange/42943-virtual-mouse--mouse-pointer-control-using-color-detection/content/MouseControl.m

The main code that you would find helpful should be this -

cam = imaqhwinfo; % Get Camera information
cameraName = char(cam.InstalledAdaptors(end));
cameraInfo = imaqhwinfo(cameraName);
cameraId = cameraInfo.DeviceInfo.DeviceID(end);
cameraFormat = char(cameraInfo.DeviceInfo.SupportedFormats(end));

vidDevice = imaq.VideoDevice(cameraName, cameraId, cameraFormat, ... % Input Video from current adapter
                'ReturnedColorSpace', 'RGB');

while (condition)
   rgbFrame = step(vidDevice); % Acquire single frame
like image 188
Shantanu Deshpande Avatar answered Nov 23 '25 05:11

Shantanu Deshpande


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!

like image 38
Kaya311 Avatar answered Nov 23 '25 07:11

Kaya311



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!