Search code examples
matlabstreamingwebcamfigureaxes

matlab - figure keeps blinking when streaming webcam


I first created a figure to stream webcam images onto it. I then created a 3D axes on the figure while streaming from the webcam. Note that the webcam frame streaming is not set inside the 3D axes but is set outside of the 3D axes in the figure itself. The problem is that the figure keeps blinking when I stream the webcam due to the uistack(ah,'bottom') function. I need this function to keep the 3D axes ah in front of the streaming figure background frame.

As you can see, I am using my webcam to stream apples. You can also see that the 3D axes is in front of the background thanks to the uistack(ah,'bottom') function. If I didn't use this function, the 3D axes would be hidden behind the background.

enter image description here

% turn on webcam
camList = webcamlist;
cam = webcam(camList{1});

% stream webcam
while(1)
    frame = snapshot(cam);
    ah = axes('unit','normalized','position',[0 0 1 1]);
    imagesc(frame)
    drawnow
    set(ah,'handlevisibility','off','visible','off','Clipping','off')
    uistack(ah,'bottom')
end

Solution

  • I solved the problem.

    You need to define two axes: (i) one for the streaming images az and (ii) another one for the static image ah outside of the loop. Also, place the uistack function outside since it is redundant to tell the program to keep ah on top of the az axes every loop.

    camList = webcamlist;
    cam = webcam(camList{1});
    
    hold on
    az = axes('unit','normalized','position',[0 0 1 1]);
    ah = axes('unit','normalized','position',[0 0 1 1]);
    uistack(ah,'top')
    
    while(1)
        frame = snapshot(cam);
        imagesc(az,frame)
        drawnow
    end