jeudi 16 juin 2016

Image Mosaic of two images

I need matlab code for making flat seamless view from warped image, image is the warp output of two images

in the image 1 the black portion need to be removed and it has to appear without any distortions.

I tried it with mask overlapping technique but it is not working,i studied in the paper "Mask Based Image Blending and Its Applications on Mobile Devices" by {Yingen Xionga and Kari Pullia aNokia Research Center, 955 Page Mill Road, Palo Alto, CA 94304, USA}

finding out the masks {mask1,mask2} matlab code......

              ``function [mask1, mask2] = masks( i1, i2, homography)

                 stitchedImage = i1;
                 stitchedImage = padarray(stitchedImage, [0 size(i2, 2)], 0,                      'post');
                 stitchedImage = padarray(stitchedImage, [size(i2, 1) 0], 0, 'both');

                    mask1 = ones(size(i1));
                    mask1 = padarray(mask1, [0 size(i2, 2)], 0, 'post');
                    mask1 = padarray(mask1, [size(i2, 1) 0], 0, 'both');

                    mask2 = ones(size(mask1));

                    for i = 1:size(stitchedImage, 2)
                     for j = 1:size(stitchedImage, 1)
                    p2 = homography * [i; j-floor(size(i2, 1)); 1];
                    p2 = p2 ./ p2(3);

                    x2 = floor(p2(1));
                    y2 = floor(p2(2));
           if x2 > 0 && x2 <= size(i2, 2) && y2 > 0 && y2 <= size(i2, 1)
        stitchedImage(j, i) = i2(y2, x2);
        mask2(j,i) = 0;
    end
    end
    end

   % % %crop
% [row,col] = find(stitchedImage);
% c = max(col(:));
% d = max(row(:));
% 
% st=imcrop(stitchedImage, [1 1 c d]);
% mask1=imcrop(mask1, [1 1 c d]);
% mask2=imcrop(mask2, [1 1 c d]);
% 
% [row,col] = find(stitchedImage ~= 0);
% a = min(col(:));
% b = min(row(:));
% st=imcrop(st, [a b size(st,1) size(st,2)]);
% mask1=imcrop(mask1, [a b size(st,1) size(st,2)]);
% mask2=imcrop(mask2, [a b size(st,1) size(st,2)]); 

    enter code here
and finding blend using mask intersection
function target = stitch_blend( img1, img2, bestHomography,mask1,mask2)

% the intersection mask
mask3=zeros(size(mask2));
load('result.mat');
for i=1:size(mask2,1)
    for j=1:size(result,2)
        if mask1(i,j)==1 && mask2(i,j)==0
            mask3(i,j)=1;
        end
    end
end


%%
% copy the first image over
target = img1;

target = padarray(target, [0 size(img2, 2)], 0, 'post');
target = padarray(target, [size(img2, 1) 0], 0, 'both');
%%
imagesc(target);colormap gray;
imwrite(target,'target.jpg');
%%
% copy the 2nd image over (white is 1, black is 0)
source = zeros(size(target));
for j = 1:size(source, 1)
   for i = 1:size(source, 2)
        p2 = bestHomography * [i; j-floor(size(img2, 1)); 1];
        p2 = p2 ./ p2(3);

        x2 = floor(p2(1));
        y2 = floor(p2(2));
        if x2 > 0 && x2 <= size(img2, 2) && y2 > 0 && y2 <= size(img2, 1)
           source(j, i) = img2(y2, x2);
        end
    end
end
%
imagesc(source);colormap gray;



%%
% if mask(x,y) is 0:
%     final(x,y) = target(x,y)
% else:
%     final(x,y) = 0
%     for each neighbor (nx,ny) in (x-1,y), (x+1,y), (x,y-1), (x,y+1):
%         final(x,y) += source(x,y) - source(nx,ny)
%         if mask(nx,ny) is 0:
%             final(x,y) += target(nx,ny)
%         else:
%             final(x,y) += final(nx,ny)
% %%
% mask3 = padarray(target, [0 size(img2, 2)], 0, 'post');
% mask3 = padarray(target, [size(img2, 1) 0], 0, 'both');
% 
final = zeros(size(source));
for i=1:size(final,1)
    for j=1:size(final,2)
        if mask3(i,j) == 0
            final(i,j) = target(i,j);
        else
            if 1
                if i > 2
                    ni = i-1; nj = j;
                    final(i,j) = final(i,j) + source(i,j) - source(ni,nj);
                    if mask3(ni,nj) == 0
                       final(i,j) = final(i,j) + target(ni,nj);
                    else
                        final(i,j) = final(i,j) + final(ni,nj);
                    end
                end
                if i < size(source,1)
                    ni = i+1; nj = j;
                    final(i,j) = final(i,j) + source(i,j) - source(ni,nj);
                    if mask3(ni,nj) == 0
                       final(i,j) = final(i,j) + target(ni,nj);
                    else
                        final(i,j) = final(i,j) + final(ni,nj);
                    end
                end
                if j > 2
                    ni = i; nj = j-1;
                    final(i,j) = final(i,j) + source(i,j) - source(ni,nj);
                    if mask3(ni,nj) == 0
                       final(i,j) = final(i,j) + target(ni,nj);
                    else
                        final(i,j) = final(i,j) + final(ni,nj);
                    end
                end
                if j < size(source,2)
                    ni = i; nj = j+1;
                    final(i,j) = final(i,j) + source(i,j) - source(ni,nj);
                    if mask3(ni,nj) == 0
                       final(i,j) = final(i,j) + target(ni,nj);
                    else
                        final(i,j) = final(i,j) + final(ni,nj);
                    end
                end
            end
        end
    end
end
imagesc(final);colormap gray


figure(8),imshow(uint8(final));


% %%

% target = img1;
% 
% target = padarray(target, [0 size(img2, 2)], 0, 'post');
% target = padarray(target, [size(img2, 1) 0], 0, 'both');
% for i = 1:size(source, 2)
%     for j = 1:size(source, 1)
%         p2 = bestHomography * [i; j-floor(size(img2, 1)); 1];
%         p2 = p2 ./ p2(3);
% 
%         x2 = floor(p2(1));
%         y2 = floor(p2(2));
% 
%      if x2 > 0 && x2 <= size(img2, 2) && y2 > 0 && y2 <= size(img2, 1)
%      if target(j,i)~=0&&img2(y2, x2)~=0
%         final(j,i)=final(j,i)*(1-bwdist(j,i))+im2(y2, x2)*(bwdist(j,i));
%      else
%          final(j, i) = img2(y2, x2);
%          
%      end
%      end
%     end
% end
% % 
% % 
% %%%crop
[row,col] = find(final);
c = max(col(:));
d = max(row(:));

st=imcrop(final, [1 1 c d]);

[row,col] = find(final ~= 0);
a = min(col(:));
b = min(row(:));
st=imcrop(st, [a b size(st,1) size(st,2)]);

target = st;

Aucun commentaire:

Enregistrer un commentaire