1

I am developing a simple app in App Designer and I have been struggling with including a video stream from a webcam.

I have come across the following post, but so far I have been unsuccessful in getting my app to work: https://uk.mathworks.com/matlabcentral/answers/357814-how-to-create-a-custom-preview-window-for-matlab-webcam

So far, what I am doing is:

  1. Creating an axes object in the GUI. This is the second figure, so it is automatically labelled as app.UIAxes2.

  2. Including the following commands in my start-up function:

    app.Camera = webcam;  %('USB2.0 PC CAMERA')
    image(app.UIAxes2,zeros(size(snapshot(app.Camera)),'uint8'));
    

where app.Camera is my camera object (I have included the property in the class) and the second command is supposed to resize the image to the camera video stream size. I may be wrong about this.

  1. Creating a switch (CameraStreamSwitch), which is supposed to show the video stream when switched on (to Start).

  2. Creating the following function in the App code, which is activated whenever the switch is pressed:

    % Value changed function: CameraStreamSwitch
    function CameraStreamSwitchValueChanged(app, event)
       while strcmp(app.CameraStreamSwitch.Value,'Start')
           im = image(app.UIAxes_2,zeros(size(snapshot(app.Camera)),'uint8'));
       preview(app.Camera,im);
       end
    end
    

Now, although the other figures showing graphs that updated in real-time are working, this figure is clearly not working, i.e. it is resized to the correct size, but remains black all the time (I have checked, opening preview from the command line (outside of the app) results in a figure popping up and showing a functioning video stream). I am not sure if the problem is related to calling preview within an app, my not specifying the the correct image or image properties correctly, or whether I should simply display fewer images at a slower frame rate within an App.

What can be done to solve this?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Enrico Anderlini
  • 447
  • 6
  • 21
  • Is there a reason why you're using the app designer, and not "old" figures? Does this camera work using other software? Are you able to create a **webpage** that shows the camera stream? – Dev-iL Sep 04 '18 at 08:03
  • My intention was to use App Designer because the App displays other two graphs, uses user input for some matrices and performs operations in the background in real time. This way, if successful, I would have all code in one App, which I can then publish and use. I am not a developer and Matlab is the language I am most confident in. This is a one-off project. Many thanks! – Enrico Anderlini Sep 04 '18 at 13:17

1 Answers1

1

OK, not a great solution, but I have found that one way to solve this problem is to use imshow.

A code that works is as follows:

% Value changed function: CameraStreamSwitch
function CameraStreamSwitchValueChanged(app, event)
    while strcmp(app.CameraStreamSwitch.Value,'Start')
          img = snapshot(app.Camera);
          imshow(img,'Parent',app.UIAxes2);
          pause(0.1);
    end
 end

You can change the frame rate by changing the pause time.

If you have better solutions, please let me know!

Enrico Anderlini
  • 447
  • 6
  • 21