MATLAB匹配按列碎开的纸片

纵向切开的纸片的拼接

2013 CUMCM_B

%% paper_cutting.m
clear, close all
clc
%% import pictures, and save into images{img_num}
[images, img_num, vertical_border] = import_data('.\img_data\pic1\');    % 批量读入图片, 在上上篇中写到.'
%% 求出任意一纸片的左侧和另一任意纸片的右侧的距离roads

roads = zeros(img_num);
header = 0;
tailer = 0;
for i = 1 : img_num
    for j = 1 : img_num
        if i == j
            roads(i, j) = 1000;
            continue
        end
        roads(i, j) = sum(abs(images{i}(:, end) - images{j}(:, 1)));
        if roads(i, j) == 0
            header = j;
            tailer = i;
        end
    end
end
fraction_idx = zeros(img_num, 1);
fraction_idx(1) = header;
fraction_idx(end) = tailer;
for i = 1 : img_num
    if count_white_chunk(images, i, 50)
        fraction_idx(2) = i;
    end
end

%% xiongyali method:
c = roads;
[h, w] = size(c);
c = c'; c = c(:);
Aeq = zeros(h+w, h*w);
for i = 1 : h
    Aeq(i, (i-1) * w + 1 : w*i) = 1;
end
for i=1 : w
    Aeq(h + i, i : w : h*w) = 1;
end
beq = ones(h+w, 1);
A = []; b = [];
[x,fval] = bintprog(c, A, b, Aeq, beq);
X = reshape(x, w, h)';

%% Get the order.
for i = 2 : img_num-1
    fraction_idx(i) = find(X(fraction_idx(i-1), :) ~= 0);
end

%% connect the fractions and show it.
for i = 1 : img_num
    if i == 1
        connected_img = images{fraction_idx(i)};
    else
        connected_img = [connected_img, images{fraction_idx(i)}];
    end
end
figure(1);
imshow(connected_img);
imwrite(connected_img, 'saved1.png', 'png');
title('Connected fractions.');

posted @ 2017-07-31 15:53  默盒  阅读(175)  评论(0编辑  收藏  举报