webcam preview in a gui

12 views (last 30 days)
Oscar Gonzalo
Oscar Gonzalo on 24 May 2021
Answered: Oscar Gonzalo on 25 May 2021
Hi,
I have been trying to include a webcam preview in a gui but I didn't success.
I have used the userData field within the main figure to store all the revelant informaiton and the 'fig' is a global variable.
When I launch the app and turn on the camera (video button), I get a solid color image (black, white...it depends) but not the webcam preview.
I have tested the preview function outside of the gui and it works.
My code is as follows:
clear all, close all, clc
global fig
fig = uifigure('Name','Gui');
fig.Position = [50 50 1700 800];
fig.UserData.g = uigridlayout(fig);
fig.UserData.g.RowHeight = {30,30,30,30,30,30,30,'1x'};
fig.UserData.g.ColumnWidth = {150,150,'1x','1x'};
fig.UserData.ax3 = uiaxes(fig.UserData.g);
fig.UserData.ax3.Layout.Row = length(fig.UserData.g.RowHeight);
fig.UserData.ax3.Layout.Column = 4;
title(fig.UserData.ax3,'Video','FontSize',14)
bt6 = uibutton(fig.UserData.g, 'Text', 'Video', 'ButtonPushedFcn', @(bt6,event) fun_Video(bt6, fig));
bt6.Layout.Row = 1;
bt6.Layout.Column = 2;
fig.UserData.bt6=bt6;
bt2 = uibutton(fig.UserData.g, 'Text', 'Stop', 'ButtonPushedFcn', @(bt2,event) fun_Stop(bt2, fig));
bt2.Layout.Row = 4;
bt2.Layout.Column = [1 2];
fig.UserData.bt2=bt2;
function fun_Video(bt6, fig)
global fig
fig.UserData.dev_info = imaqhwinfo('winvideo',1)
fig.UserData.vid = videoinput('winvideo',1,fig.UserData.dev_info.SupportedFormats{1})
fig.UserData.vidRes = fig.UserData.vid.VideoResolution;
fig.UserData.nBands = fig.UserData.vid.NumberOfBands;
fig.UserData.himage=image(zeros(fig.UserData.vidRes(2), fig.UserData.vidRes(1), fig.UserData.nBands), 'Parent', fig.UserData.ax3);
preview(fig.UserData.vid, fig.UserData.himage);
end
function fun_Stop(bt2, fig)
global fig
closepreview(fig.UserData.vid);
end
Does anyone faced a similar problem?
I also tried the code indicated in https://es.mathworks.com/help/imaq/previewing-data.html using "setappdata(hImage,'UpdatePreviewWindowFcn',@mypreview_fcn)" but the problem remains.
Thanks.
Oscar

Answers (1)

Oscar Gonzalo
Oscar Gonzalo on 25 May 2021
Just in case anyone has the same problem.... I found a functional solution using axes instead of uiaxes:
fig.UserData.ax3 = uiaxes(fig.UserData.g); -------> fig.UserData.ax3 = axes(fig.UserData.g);
I don't know why, but with uiaxes it doesn't work.

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!