MATLAB: Webcam preview closes after opening

imagelogitechpreviewwebcam

I have the correct image acquisition toolbox which is updated, the device drivers are up to date, and I am using MATLab R2016b. When using:
cam=webcam('Logitech')
preview(cam)
The preview opens to a black screen for a second or so, and then closes. Any idea what it might be?
Cheers, Nick

Best Answer

If the firewall is active for camera connection, even if some firewall exceptions are set for MATLAB, it will most likely still interfere with camera communication. Depending on the firewall configuration it is possible to have a scenario in which the camera is detected, but the image data stream is blocked by the firewall.
Also, this is a common issue since many cameras do not return 16-bit data and so they do not send a frame that spans the full range of a 16-bit frame. Try using the following command before ‘preview’ to the allow MATLAB to capture full bitness depth:
>> imaqmex('feature', '-previewFullBitDepth', true);
If this does not solve your issue, it might be due to dropping of packets/frames. For optimum performance and to prevent dropped frames, configure the camera to insert an appropriate delay between packets by setting the 'PacketDelay' source property. The recommended PacketDelay value depends on the PacketSize, camera resolution (image height and width), pixel format (for example 'Mono8'), camera frame rate, and other camera specific settings. Refer to the following link to calculate appropriate packet delay-
https://www.mathworks.com/matlabcentral/answers/91834-how-do-i-calculate-the-packet-delay-for-a-gige-vision-camera-to-prevent-dropped-frames