metlab控制摄像头操作笔记(二)简单的预览和拍照功能

今天做一个简单的界面,打开时能看到摄像头的实时图像,添加2个按钮:

按钮1:拍照功能,拍照后显示拍照的图片,并将图片保存;

按钮2:摄像头预览,点击后开始重新预览;

最后的效果如下图:

 

metlab控制摄像头操作笔记(二)简单的预览和拍照功能_第1张图片

创建GUI的步骤:

创建两个按钮:

拍照:pushbutoon1

预览摄像头:pushbutton2  

添加axes用来显示预览摄像头信息:axes1

 

先修改OpeningFcn函数,让程序运行时就显示摄像头:

function Uderma_OpeningFcn(hObject, eventdata, handles, varargin)

% Choose default command line output for Uderma
handles.output = hObject;

%显示摄像头
%首先创建摄像头
global vid;
global hImage;
vid=videoinput('winvideo');

%如何获取摄像头名称见 https://blog.csdn.net/windvally/article/details/54175623
set(vid,'TriggerRepeat',inf);
set(vid,'FramesPerTrigger',1);

%获取axes;
axes(handles.axes1);
%创建图像用来显示图像
vidRes=vid.VideoResolution;
nBands=vid.NumberOfBands;
hImage=image(zeros(vidRes(2),vidRes(1),nBands));
preview(vid,hImage);
% Update handles structure
guidata(hObject, handles);

 

 

%拍照功能回调函数

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% 拍照按钮回调函数


global vid;
global hImage;
%保存拍照图片到
frame=getsnapshot(vid);
imwrite(frame,'snap.jpg');
%停止预览并显示保存的图片
%delete(vid);
%获取axes;
axes(handles.axes1);
image(frame);






% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%
%预览按钮回调函数 
global vid;
global hImage;
vid=videoinput('winvideo');


set(vid,'TriggerRepeat',inf);
set(vid,'FramesPerTrigger',1);


%获取axes;
axes(handles.axes1);


%创建图像用来显示图像
vidRes=vid.VideoResolution;
nBands=vid.NumberOfBands;
hImage=image(zeros(vidRes(2),vidRes(1),nBands));


preview(vid,hImage);


% Update handles structure
guidata(hObject, handles);

 

这样一个简单的拍照功能的程序就实现了。

 

你可能感兴趣的:(MatLab)