开发者

Programming in Matlab GUI and Functions

I am new to Matlab and trying to program a GUI in Matlab that will display the webcam I have on my laptop in a small window.I am trying to create a function for when the start button is pushed it will start the webcam in which I have done but at the same time I want a snapshot to be taken ever 5 seconds and display the image in my main window. I need help on this.Also how to set the figure size to a bigger one. Below is my Matlab Code

% Create a video input object.
vid = videoinput('winvideo');

% Create a figure window. This example turns off the default
% toolbar and menubar in the figure.
hFig = figure('Toolbar','none',...
       'Menubar', 'none',...
       'NumberTitle','Off',...
       'Name','LegoBot');




% Set up the push buttons
uicontrol('String', 'Start Preview',...
    'Callback', 'preview(vid)',...
    'Units','normalized',...
    'Position',[0 0 0.15 .07]);
uicontrol('String', 'Stop Preview',...
    'Callback', 'stoppreview(vid)',...
    'Units','normalized',...
    'Position',[.17 0 .15 .07]);
uicontrol('String', 'Close',...
    'Callback', 'close(gcf)',...
    'Units','normalized',...
    'Position',[0.34 0 .15 .07]);



% Create the text label for the timestamp
hTextLabel = uicontrol('style','text','String','Timestamp', ...
    'Units','normalized',...
    'Position',[0.85 -.04 .15 .08]);

% Create the image object in which you want to
% display the video preview data.
vidRes = get(vid, 'VideoResolution');
imWidth = vidRes(1);
imHeight = vidRes(2);
nBands = get(vid, 'NumberOfBands');
hImage = image( zeros(imHeight, imWidth, nBands) );

% Specify the size of the axes that contains the image object
% so that it displays the image at the right resolution and
% centers it in the figure window.
figSize = get(hFig,'Position');
figWidth = figSize(7);
figHeight = figSize(8);
set(gca,'unit','pixels',...
        'position',[ ((figWidth - imWidth)/2)... 
                     ((figHeight - imHeight)/2)...
                       imWidth imHeight ]);

% Set up the update preview window function.
setappdata(hImage,'UpdatePreviewWindowFcn',@mypreview_fcn);

% Make handle to text label available to update function.
setappdata(hImage,'HandleToTimestampLabel',hTextLa开发者_运维问答bel);

preview(vid, hImage);


I agree with the earlier comments, about using Guide, I would also recommend using the data accquition toolbox, as someone has done here DAQ Matlab Webcam and try writing your code around that in which case you could make a loop to collect data at the time intervals you want.

Hope that helps,


Funny, I just wrote code this morning to handle this. You will have to add your own logic to exit the loop, but this should work for you.

% Initialize variables
objects = imaqfind; delete(objects); clear objects
cameraOn = true; %Loop initialization
counter = 0;
fileName = 'OutputFile';

%Initialize Webcam Object
vid = videoinput('winvideo');
src = getselectedsource(vid);
get(src);

% Configure Camera
vid = videoinput('winvideo', 1, 'MJPG_640x480'); %Gotten from imaqtool
src = getselectedsource(vid);
triggerconfig(vid, 'manual');
src.ExposureMode = 'manual'; %Speeds up webcam to max
src.BacklightCompensation = 'off'; %Speeds up webcam to max

% Call Preview Window
vid = videoinput('winvideo',1);
preview(vid)

% Begin snapshot collection
while cameraOn == true;
for ii = 5:-1:1
    display([num2str(ii)]); %Display a countdown
    pause(1);
end
snapshot = getsnapshot(vid); %Collect video frame
counter=counter+1;
imwrite(snapshot,[fileName,num2str(counter,3),'.png'],'png');
end

% Stop previewing video data.
stoppreview(vid);

delete(vid); clear vid
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜