2011-03-30 35 views
2

我是Matlab新手,嘗試在Matlab中編程一個GUI,它將在我的筆記本電腦上顯示一個小窗口中的攝像頭。我試圖創建一個函數,推動它將啓動我已經完成的攝像頭,但同時我想要拍攝5秒快照並在我的主窗口中顯示圖像。我需要這方面的幫助。如何將圖形大小設置爲更大。下面是我的MATLAB代碼在Matlab GUI和函數中編程

% 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',hTextLabel); 

preview(vid, hImage); 
+0

我不能強調在MATLAB以外的指南中開發GUI是一項非常困難的任務,並且不適用於初學者。 – linuxuser27 2011-03-30 16:44:21

+0

...那麼真正的問題是什麼?我可以第二個linuxuser的建議使用指南,這有助於你的情況?如果不是,實際問題是什麼? – 2011-04-13 15:06:09

回答

0

我同意與先前的評論,有關使用指南,我也建議使用數據accquition工具箱,因爲有人在這裏DAQ Matlab Webcam完成,嘗試圍繞編寫代碼在這種情況下你可以按照需要的時間間隔循環收集數據。

希望幫助,

0

好笑的是,我只是寫代碼今天上午處理這個問題。您將不得不添加自己的邏輯來退出循環,但這應該適用於您。

% 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