在讀入攝像頭數據的時候,按下ctrl+x鍵,可以截取圖像,就是這樣一個功能。功能不多,但很有用。
main.m
clear all; close all; clc; global m; obj=videoinput('winvideo',1,'YUY2_320x240'); h1=preview(obj); %預覽視頻,同時獲取句柄 h2=figure('KeyPressFcn',@keyPressFigure); %新建顯示圖像figure,同時獲取句柄 while ishandle(h1) && ishandle(h2) %兩個句柄有一個關閉就結束程序 frame=getsnapshot(obj); %捕獲圖像 frame=ycbcr2rgb(frame); %轉成彩色,這個frame就可以按照自己意願處理了 subplot(1,2,1); imshow(frame); drawnow; subplot(1,2,2); imshow(m); end delete(obj); %刪除對象
KeyPressFigure.m
function keyPressFigure(src,event) global m; % CTRL + x if length(event.Modifier) == 1 & strcmp(event.Modifier{:}, 'control') & event.Key == 'x' m=imcrop(); end end