MATLAB中使用blockmatching算法提取光流

在matlab中使用自带的vision.BlockMatcher算法计算两张输入图像之间的光流

直接码代码
blockmatching.m

clear all; 
close all;

%% parmeters
block_size = 71;
dis = 21;
olp = 41;
thresd = 0.9;
down = 1/4;
height_img = 1600;
width_img = 1600;
show_flow = true;
kernel = 7;
scale = 1;

%% image path
artifact_path = '../results/cremia/cremia_25sff_histeq';
interp_path = '../results/cremia/cremia_25sff_interp';
flow_path = '../results/cremia/cremia_25sff_BMflow';
show_path = '../results/cremia/cremia_25sff_BMflow_show';

%% artifact images list
artifact_list = dir([artifact_path,'/','*.png']);
num = length(artifact_list);

%% loop
for i = 1: num
    fprintf('Processing %d\n', i);
    %% read images
    art_name = artifact_list(i).name;
    art_img = im2double(imread([artifact_path,'/',art_name]));
    interp_img = im2double(imread(strcat(interp_path,'/',art_name)));

    %% down sample
    % for speed
    art_img = imresize(art_img, down);
    interp_img = imresize(interp_img, down);

    %% block match
    hbm = vision.BlockMatcher('ReferenceFrameSource',...
            'Input port','BlockSize',[block_size block_size], 'MaximumDisplacement', [dis, dis], 'Overlap', [olp, olp]);
    hbm.OutputValue = 'Horizontal and vertical components in complex form';
    % halphablend = vision.AlphaBlender;
    % motion = hbm(art_img, interp_img);
    motion = step(hbm, art_img, interp_img);
    % img12 = halphablend(interp_img, art_img);
    % img12 = step(halphablend, interp_img, art_img)
    img12 = art_img;

    %% filter optical flow
    [height_flow, width_flow] = size(motion);
    xx = real(motion(:)) * scale;
    yy = imag(motion(:)) * scale;
%     xx = medfilt2(xx, [kernel 1]);
%     yy = medfilt2(yy, [kernel 1]);

    %% remove strange flow
%     len_flow = length(xx);
%     max_xx = max(abs(xx)) * thresd;
%     max_yy = max(abs(yy)) * thresd;
%     for k = 1 : len_flow
%         if abs(xx(k)) > max_xx && abs(yy(k)) > max_yy
%             xx(k) = 0;
%             yy(k) = 0;
%         end
%     end
%     for k = 1 : len_flow
%         if xx(k) > max_xx || xx(k) < min_xx
%             xx(k) = 0;
%         end
%         if yy(k) > max_yy || yy(k) < min_yy
%             yy(k) = 0;
%         end
%     end

    %% upsample optical flow
    xx_2d = reshape(xx, height_flow, width_flow);
    yy_2d = reshape(yy, height_flow, width_flow);
    xx_2d = imresize(xx_2d, [height_img, width_img], 'bicubic');
    yy_2d = imresize(yy_2d, [height_img, width_img], 'bicubic');

    %% save optical flow
    flow = zeros(height_img, width_img, 2);
    flow(:, :, 1) = xx_2d;
    flow(:, :, 2) = yy_2d;
    pre_name = strsplit(art_name, '.');
    save_name = strcat(flow_path, '/', pre_name{
    
    1}, '.flo');
    writeFlowFile(flow, save_name);

    %% show flow
    A = int32(size(art_img,2));
    B = int32(height_flow);
    step_ev = idivide(A, B, 'ceil');
    show_name = strcat(show_path, '/', pre_name{
    
    1}, '.png');
    if show_flow == true
        h_fig = figure('Visible', 'off');
        [X,Y] = meshgrid(1:step_ev:size(art_img,2),1:step_ev:size(art_img,1));         
        imshow(img12)
        hold on
        quiver(X(:),Y(:),xx,yy,0)
        hold off
        saveas(h_fig, show_name);
        close(h_fig);
    end
end

writeFlowFile.m

function writeFlowFile(img, filename)

% writeFlowFile writes a 2-band image IMG into flow file FILENAME 

%   According to the c++ source code of Daniel Scharstein 
%   Contact: schar@middlebury.edu

%   Author: Deqing Sun, Department of Computer Science, Brown University
%   Contact: dqsun@cs.brown.edu
%   $Date: 2007-10-31 15:36:40 (Wed, 31 Oct 2006) $

% Copyright 2007, Deqing Sun.
%
%                         All Rights Reserved
%
% Permission to use, copy, modify, and distribute this software and its
% documentation for any purpose other than its incorporation into a
% commercial product is hereby granted without fee, provided that the
% above copyright notice appear in all copies and that both that
% copyright notice and this permission notice appear in supporting
% documentation, and that the name of the author and Brown University not be used in
% advertising or publicity pertaining to distribution of the software
% without specific, written prior permission.
%
% THE AUTHOR AND BROWN UNIVERSITY DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
% INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
% PARTICULAR PURPOSE.  IN NO EVENT SHALL THE AUTHOR OR BROWN UNIVERSITY BE LIABLE FOR
% ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 

TAG_STRING = 'PIEH';    % use this when WRITING the file

% sanity check
if isempty(filename) == 1
    error('writeFlowFile: empty filename');
end;

idx = findstr(filename, '.');
idx = idx(end);             % in case './xxx/xxx.flo'

if length(filename(idx:end)) == 1
    error('writeFlowFile: extension required in filename %s', filename);
end;

if strcmp(filename(idx:end), '.flo') ~= 1    
    error('writeFlowFile: filename %s should have extension ''.flo''', filename);
end;

[height width nBands] = size(img);

if nBands ~= 2
    error('writeFlowFile: image must have two bands');    
end;    

fid = fopen(filename, 'w');
if (fid < 0)
    error('writeFlowFile: could not open %s', filename);
end;

% write the header
fwrite(fid, TAG_STRING); 
fwrite(fid, width, 'int32');
fwrite(fid, height, 'int32');

% arrange into matrix form
tmp = zeros(height, width*nBands);

tmp(:, (1:width)*nBands-1) = img(:,:,1);
tmp(:, (1:width)*nBands) = squeeze(img(:,:,2));
tmp = tmp';

fwrite(fid, tmp, 'float32');

fclose(fid);

猜你喜欢

转载自blog.csdn.net/qq_33757398/article/details/124782717