⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 observe.m

📁 image processing in matlab
💻 M
📖 第 1 页 / 共 2 页
字号:
%OBSERVE Create a video file, by continuously reading an image from a local
%   image file, an image file on the internet, a video device connected to
%   your computer or your computer screen.
%   IMG_CNT = OBSERVE(IMAGE_URL, INTERVAL_SEC, VIDEO_FILENAME_PATTERN, VIDEO_FPS,
%   VIDEO_CODEC_FOURCC, VIDEO_CODEC_QUALITY, LOG_TO_FILE)
%   loads the image present at the specified IMAGE_URL every INTERVAL_SEC
%   seconds and stores it in an avi file with name VIDEO_FILENAME_PATTERN.
%
%   There are three possible ways of capturing:
%  
%   1. Capturing from an image file on your computer or the internet:
%     If for example IMAGE_URL == 'c:\hello.png' then this image will be
%     used as video source for the capturing. Of course this only makes
%     sense if the content of the file changes during the capturing process.
%     You can also use an image url such as
%     'http://www.heavens-above.com/images/tinylogo.jpg'
%     to capture online webcams or weather satellite images.
%
%   2. Capturing from video devices connected directly to your computer:
%     If IMAGE_URL has the format 'vid:D,F' where D and F are numbers
%     defining a video device nr D and corresponding video format nr F,
%     that video input will be used for capturing.
%   
%     To get a list of available video devices and corresponding video
%     formats enter observe('vid?');
%
%   3. Capturing from the screen:
%     If IMAGE_URL has the format 'scr:X,Y,W,H' a screen section with the
%     specified position and size will be used as video source.
%     If no parameters are specified the entire screen serves as input.
%   
%   OBSERVE returns the # of images that were stored in the avi file.
%
%   The resulting avi file will have a framerate of VIDEO_FPS fps.
%   VIDEO_CODEC_FOURCC is the four character code of the video codec to be
%   used and VIDEO_CODEC_QUALITY is the "quality" (between 0 and 100)
%   of the video. If LOG_TO_FILE is true, a log file will be created.
%   If VIDEO_CODEC_FOURCC is 'MPG' and the mpgwrite function is available,
%   an MPEG1 video file will be created at the end of the observation.
%
%   Default values are:
%      IMAGE_URL              = 'vid:1'
%      INTERVAL_SEC           = 20
%      VIDEO_FILENAME_PATTERN = 'video####.avi'
%      VIDEO_FPS              = 15
%      VIDEO_CODEC_FOURCC     = 'IV50'
%      VIDEO_CODEC_QUALITY    = 100
%      LOG_TO_FILE            = true
%
%   If the parameter VIDEO_FILENAME_PATTERN contains one or more '#'
%   characters, a new video file will be created automatically.
%   The program ends, by closing the figure window.
%
function img_cnt = observe(image_url, interval_sec, video_filename_pattern, video_fps, video_codec_fourcc, video_codec_quality, log_to_file)

    img_cnt = 0;

    % codec image size granularity (change to e.g. 8 or 16 if codec complains)
    codec_img_size_granularity = 4;

    % parameter preprocessing (default value substitution)
    if nargin < 7, log_to_file = true; end
    if nargin < 6, video_codec_quality = 100; end
    if nargin < 5, video_codec_fourcc = 'IV50'; end
    if nargin < 4, video_fps = 15; end
    if nargin < 3, video_filename_pattern = 'video####.avi'; end
    if nargin < 2, interval_sec = 20; end
    if nargin < 1, image_url = 'vid:1'; end

    % show list of video devices and formats
    if beginswith(image_url, 'vid?')
        show_video_devices;
        return;
    end

    % get screen input if needed
    scr_input = false;
    if beginswith(image_url, 'scr')

        % get rectangle parameters --> x,y,width,height
        rect_params = sscanf(image_url,'scr:%d,%d,%d,%d');

        % java stuff... (thx to saurabh kumar for the code)
        robo    = java.awt.Robot;
        toolkit = java.awt.Toolkit.getDefaultToolkit();
        % if no or not enough parameters where specified...
        if length(rect_params)<2
            % use entire screen
            rectangle = java.awt.Rectangle(toolkit.getScreenSize());
        elseif length(rect_params)<4
            % use default width and height if only x and y where specified
            rectangle = java.awt.Rectangle(rect_params(1),rect_params(2),256,256);
        else
            % use specified parameters
            rectangle = java.awt.Rectangle(rect_params(1),rect_params(2),rect_params(3),rect_params(4));
        end
        scr_input = true;
    end    
    
    % get video input if video device is specified (instead of an image url)
    vid_input = [];
    if beginswith(image_url, 'vid:')

        % get video parameters
        vid_params1 = sscanf(image_url,'vid:%d:%d');    % <- just for compability the "old syntax" still works (but considered as deprecated)
        vid_params2 = sscanf(image_url,'vid:%d,%d');
        if length(vid_params1)>length(vid_params2)
            vid_params = vid_params1;
        else
            vid_params = vid_params2;
        end

        if length(vid_params)==1
            vdevice_nr = vid_params(1);
            vformat_nr = 0;
        elseif length(vid_params)==2
            vdevice_nr = vid_params(1);
            vformat_nr = vid_params(2);
        else
            logit('Invalid video device syntax. Correct is e.g. vid:1,15\r');
            return;
        end
        
        
        % get video input
        [vid_input, is_yuv] = get_video_input(vdevice_nr, vformat_nr);      
        if isempty(vid_input)
            logit('Video input could not be opened.\r');
            return;
        end
        set(vid_input, 'FramesPerTrigger', 1);
        set(vid_input, 'TriggerRepeat', Inf);
        triggerconfig(vid_input, 'manual');
        vid_src = getselectedsource(vid_input);

        % find and set minimum framerate for capturing (should give best picture quality)
        framerates = get_videosrc_framerates(vid_src);
        capture_framerate = min(framerates);
        logit('Video capture mode set to %d fps. Capturing with %d seconds interval\r',capture_framerate,interval_sec);
        set(vid_src, 'FrameRate', num2str(capture_framerate));
        start(vid_input);
    end

    % true if MPEG1 video file is to be created
    is_mpg = 0;
    if strcmpi(video_codec_fourcc, 'mpg')
        is_mpg = 1;
        video_codec_fourcc = 'IV50';
    end

    % determine filename and create avi file
    if ~endswith(video_filename_pattern, '.avi'), video_filename_pattern = strcat(video_filename_pattern, '.avi'); end
    filename = next_filename(video_filename_pattern);
    aviobj = avifile(filename, 'compression', video_codec_fourcc, 'fps', video_fps, 'quality', video_codec_quality);
    if log_to_file
        logfilename = strcat(getfilename_root(filename), '.log');
        log_fid = fopen(logfilename, 'w');
        logit('Logging to %s\r', logfilename);
    end
    logit('Video source   = %s\r', image_url);
    logit('Video filename = %s\r', filename);

    % observe...
    fig_handle = figure('Name',['Observation of ',image_url,' to ',filename]);
    t0 = clock;
    interval_cnt = 0;
    frame_size = [];
    
    while true
  
        % load image from url
        A = get_image;
        if isempty(A)
            logit('Image could not be loaded. Trying again');
        else
            % if first image captured...
            if isempty(frame_size)
                % store height and width of final video
                frame_size(1) = size(A,1);
                frame_size(2) = size(A,2);
            
                % apply image size granularity and check for zero dimensions
                frame_size = round(frame_size / codec_img_size_granularity)*codec_img_size_granularity;
                frame_size = frame_size + (frame_size==0);            
                logit('Final video size is %dx%d.\r', frame_size(2), frame_size(1) );          
            end

            % if image has changed dimensions 
            if (size(A,1) ~= frame_size(1) || size(A,2) ~= frame_size(2))
                A = imresize(A, [frame_size(1), frame_size(2)], 'bicubic');
            end
        end
    
        there_was_problem = false;
        while isempty(A)
            there_was_problem = true;
            logit('.');
            pause(2.0);
            A = get_image;
        
            % window closed???
            if ~ishandle(fig_handle)
                finalize_video;
                return;
            end
        end
        if there_was_problem, logit('\r'); end
    
        % add frame to avi file and show image
        img_cnt = img_cnt + 1;
        imshow(A);
        aviobj = addframe(aviobj, A);
        logit('%s - Image #%d captured.\r', datestr(now), img_cnt);
    
    
        % wait for next interval...
        et = etime(clock,t0);
        new_mod = mod(et, interval_sec);
        while true
            % window closed???
            if ~ishandle(fig_handle)
                finalize_video;
                return;
            end

            % interval end???
            et = etime(clock,t0);
            old_mod = new_mod;
            new_mod = mod(et, interval_sec);
            if new_mod < old_mod
                break;
            end
            pause(min(0.2, interval_sec-new_mod));
        end
    end

    % get uint8 rgb image from location
    function out = get_image()
        try
            if scr_input
                out = java2im(robo.createScreenCapture(rectangle));        
            elseif ~isempty(vid_input)
                trigger(vid_input);
                out = make_3channel_img(make_uint8_img(getdata(vid_input)));
                if is_yuv
                    out = ycbcr2rgb(out);   % convert to rgb
                end
            else
               [out, map] = imread(image_url);
               if ~isempty(map)
                   out = ind2rgb(out, map);
               else
                   out = make_3channel_img(make_uint8_img(out));
               end
            end
        catch
           out = []; 
        end
    end
    

    % nested function to finalize the video file
    function finalize_video
        % close avi file
        logit('%d images captured.\r', img_cnt);
        aviobj = close(aviobj);
        close;

        % close video input (if open)
        if ~isempty(vid_input)
            delete(vid_input);
            clear vid_input;
        end

        % if no image was captured...
        if img_cnt == 0
            logit('Deleting empty avi file.\r');
            delete(filename);
        else
            % create mpg file???
            if is_mpg
                % mpgwrite function not there?

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -