matlab通过摄像头获取图像进行处理

来源:互联网 发布:知乎 印度军工 编辑:程序博客网 时间:2024/05/16 09:09
  1. 安装摄像头

    笔记本自带也行,或者买一个usb摄像头,安装驱动之后检查能否正常获取。有时候即使usb摄像头在电脑硬件中显示了,在matlab中还是不能调用摄像头,可以在命令窗口中写一句代码:imaqreset  ,以使得matlab加载电脑中能够获取图片的硬件。输入videoinput('winvideo')没有报错即可。
  2. 获取摄像头硬件信息

    使用imaqhwinfo函数,来获取电脑上安装的摄像头的名称,比如'winvideo',之后可以利用imaqhwinfo('winvideo')来进一步获取设备ID等边信息,这在之后的视频流获取中会用到。获取设备ID之后(比如ID为1),可以进一步用imaqhwinfo('winvideo',1)来获取视频的默认格式大小等信息。
    我们win_info=imaqhwinfo('winvideo'),则ID可用win_info.DeviceIDs来获取,同理,可用以下代码获取摄像头支持的格式
    win_info=imaqhwinfo('winvideo');d_win_info=imaqhwinfo('winvideo',1);d_win_info.SupportedFormats

    可得到
    ans =

        'YUY2_160x120'    'YUY2_176x144'    'YUY2_320x240'    'YUY2_352x288'    'YUY2_640x480'

  3. 创建视频对象

    通过第二步得知的摄像头信息,我们就可以通过函数vid = videoinput(camera_name, camera_id, format)来创建一个名为vid的视频对象
  4. 预览获取的视频流

    在进行图像数据处理之前想要预览一下摄像头所获取的视频流,可以通过函数preview(vid)来创建一个窗口进行预览。关闭窗口用closepreview(vid)。
  5. 设置获取的视频流的属性值(可选)

    视频对象包括视频输入对象,以及视频源对象,当摄像头获取输入对象时,同时产生一个视频源对象。源对象是matlab用来进行处理的对象(个人理解)。
    获取视频图像的信息可以通过get函数来获取,如get(vid)或者get(getselectedsource(vid))。使用set函数可以设置摄像头获取的图像的一些属性值,也可以直接使用结构数组的“点”来赋值,比如,要持续通过摄像头获取图像,则可以将TriggerRepeat的值设置为Inf(无穷),一下两个代码等价:
    set(vid,'TriggerRepeat',Inf);vid.TriggerRepeat= Inf;   %持续不断获取图像
    设置完摄像头触发重复时间后,再设置摄像头获取图像的快慢,可通过设置FrameGrabInterval的值来实现,设置方法同上。
    vid.FrameGrabInterval=5;        %每隔5帧取一幅图像set(vid,'ReturnedColorSpace','rgb');  %设置颜色空间为RGBset(vid,'ReturnedColorSpace','grayscale'); %设置颜色空间为灰度
    设置好视频输入对象之后,再对源对象进行设置:
    vid_src=getselectedsource(vid);set(vid,'Tag','motion detection setup');set(gcf,'doublebuffer','on');
  6. 获取图像数据

    使用start函数来开始进行图像获取,结束时使用stop函数。
  7. 附上源码:

    %http://wenku.baidu.com/view/674357ff0242a8956bece470.html?re=view%Track from foldera = imaqhwinfo;[camera_name, camera_id, format] = getCameraInfo(a);% Capture the video frames using the videoinput function% You have to replace the resolution & your installed adaptor name.vid = videoinput(camera_name, camera_id, format);set(vid, 'TriggerRepeat', Inf);% Set the properties of the video object set(vid, 'FramesPerTrigger', 1);set(vid, 'ReturnedColorspace', 'rgb');vid.FrameGrabInterval =1;%抓取时间间隔vid_src=getselectedsource(vid);%addset(vid,'Tag','motion ');%addset(gcf,'doublebuffer','on');%add%set(vid, 'FrameGrabInterval', 5);%also OK%start the video aquisition herestart(vid)% Set a loop that stop after 100 frames of aquisitionwhile(vid.FramesAcquired<=Inf)    % Get the snapshot of the current frame             %data = getsnapshot(vid);    data=getdata(vid,2);%add    diff_im=imabsdiff(data(:,:,:,2),data(:,:,:,1));%add        % Convert the resulting grayscale image into a binary image.    diff_im = im2bw(diff_im,0.18);        % Remove all those pixels less than 300px    bw = bwareaopen(diff_im,300);    rows = size(bw, 1);                 %pixels的第1维即为视频画面的行数    cols = size(bw, 2);     % 寻找上下边界     topEdge = 0;     bottomEdge=0;    cou=1;    for h = 1:rows        for w = 1:cols            if bw(h, w) > 0.5                bottomEdge = h;                if cou == 1                    topEdge = bottomEdge;                end                cou = cou+1;                break;            end        end    end    % 寻找左右边界    rightEdge=0;    leftEdge=0;    coun=1;    for w = 1:cols        for h = 1:rows            if bw(h, w) > 0.5                rightEdge = w;                if coun == 1                    leftEdge = rightEdge;                    coun = coun+1;                end                break;            end        end    end   %add    % 矩形框生成    wd = rightEdge-leftEdge;    hg = bottomEdge-topEdge;    widt = wd/2;    heit = hg/2;    cenx = leftEdge+widt;    ceny = topEdge+heit;     cenx1(1)=cenx;        % 创建一个动态数组来记录矩形框的中心坐标     ceny1(1)=ceny;    % 显示并标记    figure(1);    % Display the image    imshow(data(:,:,:,2))%add(:,:,:,2)    hold on    if wd>0&&hg>0     rectangle('Position',[leftEdge topEdge wd hg], 'EdgeColor', 'r', 'LineWidth', 2);    plot(cenx1,ceny1, 'm-.s','MarkerSize',7, 'LineWidth', 1)     end                hold on    end% Stop the video aquisition.stop(vid);% Flush all the image data stored in the memory buffer.flushdata(vid);% Clear all variablesclear all function [camera_name, camera_id, resolution] = getCameraInfo(a)camera_name = char(a.InstalledAdaptors(end));camera_info = imaqhwinfo(camera_name);camera_id = camera_info.DeviceInfo.DeviceID(end);resolution = char(camera_info.DeviceInfo.SupportedFormats(end));
2 0