How can I do the image stitching with constant overlap area?

조회 수: 14 (최근 30일)
gui li
gui li 2021년 3월 23일
댓글: Bjorn Gustavsson 2021년 3월 26일
Hello.
I have a set of images (around 100 images in each subfolder). The images are named as tile_x001_y001, tile_x001_y002,tile_x001_y003... The name of images indicate the position. In order to stitch my images, I wrote a program as follows:
function filename = construct_filename(rootfolder,rootfilename,f,x,y)
digits = @(n)['%0',num2str(n),'d'];
nf = 2; % nr digits in foldernames for focus
nx = 3; % nr digits x coordinate
ny = 3; % nr digits y coordinate
if numel(f)==1 && numel(x)==1 && numel(y)==1
filename = [rootfolder,'/',num2str(f,digits(nf)),'/',rootfilename,...
'_x',num2str(x,digits(nx)),'_y',num2str(y,digits(ny)),'.tif'];
else
filename = cell(numel(f),numel(x),numel(y));
for fi=1:numel(f)
for xi =1:numel(x)
for yi=1:numel(y)
filename{fi,xi,yi} = [rootfolder,'/',...
num2str(f(fi),digits(nf)),'/',rootfilename,...
'_x',num2str(x(xi),digits(nx)),...
'_y',num2str(y(yi),digits(ny)),'.tif'];
end
end
end
end
rootfolder = 'U:\20210321';
rootfilename = 'tile';
f = 45;
X = 10;
Y = 10;
filenames_xy = construct_filename(rootfolder, rootfilename, f, 1:X, 1:Y);
images_xy = cellfun(@(f)imread(f), filenames_xy, 'UniformOutput', false);
columns = cellfun(@(x)cat(1, images_xy{:, x, :}), num2cell(1:10), 'UniformOutput', false);
mosaic = cat(2, columns{:});
figure, imagesc(mosaic), axis image
I found that I did not consider the overlap area, so the image is not stitched in a good way. How should I do the image stitching correctly?
Note: I do not want to used the feature based algorithm. And the overlap ratio is constant (15%).
Many thanks for the help.
Best regards,
Gui

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2021년 3월 23일
Something like this works for a smallish toy-example:
% Read image
Im = imread('cameraman.tif');
% Generate some tiles with a couple of pixels overlap:
for i1 = 1:4,
for i2 = 1:4,
ims{i1,i2} = Im(min(256,64*(i1-1)+(1:68)),min(256,64*(i2-1)+(1:68)));
end
end
% Generate a suitable weighting-matrix for smooth overlap-merging:
W = ones(64,64);
K = ones(5,5)/16;
K(1,:) = 0;K(:,1) = 0;
W1 = conv2(W,K,'full');
% Merge tiles:
ImR = zeros(280); % Yeah, it's a little too big - but I'm lazy...
for i1 = 1:4,
for i2 = 1:4,
ImR((64*(i1-1)+(1:68)),(64*(i2-1)+(1:68))) = ImR((64*(i1-1)+(1:68)),(64*(i2-1)+(1:68))) + W1.*double(ims{i1,i2});
end
end
% Display:
subplot(2,2,1)
imagesc(Im)
subplot(2,2,3)
imagesc(ImR(1:256,1:256))
subplot(2,2,2)
imagesc(Im(5:end,5:end)-ImR(5:256,5:256))
HTH

추가 답변 (0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by