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

📄 observe.m

📁 image processing in matlab
💻 M
📖 第 1 页 / 共 2 页
字号:
                if ~exist('mpgwrite')
                    logit('AVI file can not be converted since the mpgwrite function is not available.\r');
                    logit('Download it from MATLAB Central File Exchange.\r');
                    return;
                end
        
                % create mpg_filename, read the created avi file and convert it to an MPEG1 video
                logit('Converting avi file to MPEG1 video. This may take a while...\r');
                mpg_filename = strcat(getfilename_root(filename), '.mpg');
                mov = aviread(filename);
                mpgwrite(mov, [], mpg_filename);

                % delete avi file (we dont need it anymore)
                logit('Deleting superfluous avi file.\r');
                delete(filename);
            end
        end
        
        % close logfile
        if log_to_file
            logit('Closing logfile.\r');
            fclose(log_fid);
            log_to_file = false;
        end
    end


    % nested function to log to log file and/or to screen
    function logit(format, varargin)
        if length(varargin)==0
            x = sprintf(format);
        elseif length(varargin)==1
            x = sprintf(format, varargin{1});
        elseif length(varargin)==2
            x = sprintf(format, varargin{1}, varargin{2});
        elseif length(varargin)==3
            x = sprintf(format, varargin{1}, varargin{2}, varargin{3});  
        elseif length(varargin)==4
            x = sprintf(format, varargin{1}, varargin{2}, varargin{3}, varargin{4});  
        elseif length(varargin)==5
            x = sprintf(format, varargin{1}, varargin{2}, varargin{3}, varargin{4}, varargin{5});  
        elseif length(varargin)==6
            x = sprintf(format, varargin{1}, varargin{2}, varargin{3}, varargin{4}, varargin{5}, varargin{6});  
        else
            x = 'Too many input variables in varargin!';
        end

        % log to file
        if log_to_file
            if exist('log_fid')
                fprintf(log_fid, x);
                if endswith(format,'\r')
                    fprintf(log_fid, '\n');   % <- for notepad.exe 
                end
            end
        end

        % log to screen
        fprintf(x);
    end

end % end of observe



% returns a new filename for pattern if there is one
% otherwise returns pattern itself.
function out = next_filename(pattern)
    % save pattern ending and strip it off
    pattern_ending = getfilename_ending(pattern);
    pattern = getfilename_root(pattern);

    % get position of all # chars
    pos = strfind(pattern, '#');
    if isempty(pos)
        out = strcat(pattern, pattern_ending);
        return
    end

    % find position and length of (first) ###-field
    num_pos = pos(1);
    num_len = 1;
    for i=2:length(pos)
        if (pos(i)-pos(i-1))>1
            break
        end
        num_len = num_len + 1;    
    end
    prefix = pattern(1:num_pos-1);
    suffix = pattern(num_pos+num_len:length(pattern));

    % find a new filename with binary search
    i=1;
    low_bound = 0;
    while true
        if videofile_exists([prefix, extnum(i, num_len), suffix])
            low_bound = i;
            i=i*2;
            if low_bound>=10^num_len
                out = strcat(pattern, pattern_ending); % no filename left!
                return;
            end
        else
            a = low_bound + 1;
            b = min(i - 1, 10^num_len-1);
            while a<=b
                m = (a+b)/2;
                if videofile_exists([prefix, extnum(m, num_len), suffix])
                    a=m+1;
                else
                    b=m-1;
                end
            end

            if a==10^num_len
                out = strcat(pattern, pattern_ending);
            else
                out = [prefix, extnum(a, num_len), suffix, pattern_ending];
            end
            return;
        end
    end
end


% true, if the avi or mpg file exists
function yes = videofile_exists(filename_root)
    yes = exist(strcat(filename_root, '.avi')) | exist(strcat(filename_root, '.mpg'));
end


% make a uint8 image from img
function out = make_uint8_img(img)
    if (islogical(img))
        out = uint8(img)*255;
    elseif (~isinteger(img))
        out = uint8(img*255);
    else
        out = uint8(img);
    end
end

% make 3 channel image
function out = make_3channel_img(img)
    if ndims(img) == 2
        out = zeros(size(img,1), size(img,2), 3, 'uint8');
        out(:,:,1) = img;
        out(:,:,2) = img;
        out(:,:,3) = img;
    else
        out = img;
    end
end


% show available video devices and corresponding video formats
function show_video_devices
    vid_devices_info = imaqhwinfo('winvideo');
    num_vid_devices = size(vid_devices_info.DeviceInfo, 2);

    if num_vid_devices==0
        fprintf('No video device available.\r');
    end

    for i=1:num_vid_devices
        recent_device = imaqhwinfo('winvideo', i);
        num_recent_formats = size(recent_device.SupportedFormats, 2);
     
        fprintf('Device %d\t = %s\r', i, char(recent_device.DeviceName));    
        for j=1:num_recent_formats
            fprintf('\tFormat %d\t = %s\r', j, char(recent_device.SupportedFormats(j)));
        end
    end
end


% get video input from vdevice_nr and vformat_nr
function [vid_input, is_yuv] = get_video_input(vdevice_nr, vformat_nr)
    % find # of video devices
    vid_devices_info = imaqhwinfo('winvideo');
    num_vid_devices = size(vid_devices_info.DeviceInfo, 2);

    % check if vdevice_nr is ok
    if vdevice_nr < 1
        fprintf('Invalid video device nr. Video device nr has to be > 0.\r');
        vid_input = [];
    elseif vdevice_nr > num_vid_devices
        fprintf('Invalid video device nr. Only %d video device(s) available.\r', num_vid_devices);
        vid_input = [];
    else
        vid_device = imaqhwinfo('winvideo', vdevice_nr);
        % use DefaultFormat if vformat_nr is invalid
        if vformat_nr<1 | vformat_nr>size(vid_device.SupportedFormats, 2)
            vformat_str = char(vid_device.DefaultFormat);
        else
            vformat_str = char(vid_device.SupportedFormats(vformat_nr));
        end
        
        vid_input = videoinput('winvideo', vdevice_nr, vformat_str);
        is_yuv = ~beginswith(vformat_str, 'RGB');
    end
end


function framerates = get_videosrc_framerates(vid_src)
    pi = propinfo(vid_src, 'FrameRate');
    framerates=[];
    for i=1:length(pi.ConstraintValue)
        framerates=[framerates,str2num(pi.ConstraintValue{i})];
    end
end


% example: extnum(123, 5) = '00123'
function x = extnum(nr, digits)
    x = num2str(nr);
    x = [repmat('0', 1, digits-length(x)),x];
end

% example: getfilename_root('Hello.txt') = 'Hello'
function out = getfilename_root(filename)
    K = strfind(filename, '.');
    if isempty(K)
        out = filename;
    else
        out = filename(1:(K(size(K,2))-1));
    end
end

% example: getfilename_ending('Hello.txt') = '.txt'
function out = getfilename_ending(filename)
    K = strfind(filename, '.');
    if isempty(K)
        out = '';
    else
        out = filename((K(size(K,2))):size(filename,2));
    end
end

% true if text begins with an_start
function yes = beginswith(text, an_start)
    K = strfind(text, an_start);
    yes = 0;
    if ~isempty(K)
        if K(1)==1
            yes = 1;
        end
    end
end

% true if text ends with an_end
function yes = endswith(text, an_end)
    K = strfind(text, an_end);
    if isempty(K)
        yes = 0;
        return;
    end
    if K(length(K))==length(text)-length(an_end)+1
        yes = 1;
    else
        yes = 0;
    end
end

% convert java jimage object to matlab rgb image
function out = java2im(jimage)
    w = int32(jimage.getWidth());
    h = int32(jimage.getHeight());

    tmp = zeros(w, h, 'int32');
    pg = java.awt.image.PixelGrabber(jimage, 0, 0, w, h, tmp(:), 0, w);
    pg.grabPixels();
    tmp = pg.getPixels();
    tmp = reshape(typecast(tmp(:), 'uint32'), w, h)';

    out = zeros(h, w, 3, 'uint8');
    out(:,:,3) = bitand(tmp, 255);
    out(:,:,2) = bitand(bitshift(tmp, -8), 255);
    out(:,:,1) = bitand(bitshift(tmp, -16), 255);        
end

⌨️ 快捷键说明

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