function fusedImage = fusedImg(imA,imB)

%setting constraints
k = 3000;
seRadius = 1;
colopt = 'mean';
m = 25;

%function (parameters) for generating superpixels 
[labl, Am, neighbors, maskImgA, maskImgB]= slic(imA, k, m, seRadius, colopt, imB);

% Holes filling
maskImgA = imfill(maskImgA,'holes');
maskImgB = imfill(maskImgB,'holes');

%finding length
N = length(Am); 

stdevA = cell(N,1);
stdevB= cell(N,1);

% Applying Neigbouring Technique
for i =1:N;
     mask = labl==i;
     if(maskImgA(mask)== true)
         stdevA{i} = 1;
     else
         stdevA{i}=0;
     end
     if(maskImgB(mask)==true)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
         stdevB{i} =1;
     else
         stdevB{i} =0;
     end
end

[maskImgA]= findNeighbours(N,neighbors,stdevA, maskImgA, labl);
[maskImgB]= findNeighbours(N,neighbors,stdevB, maskImgB, labl);


% Image fusion by Fusion Rule
[r, c, b] = size(imA);
fusedImage = zeros(r,c,b);

%case 1
case1 = and((maskImgA),(~maskImgB));
idx = find(case1);
fusedImage(idx) =  imA(idx);
fusedImage(idx+r*c) = imA(idx+r*c);
fusedImage(idx+2*r*c) = imA(idx+2*r*c);

%case 2
case2 = and((~maskImgA),(maskImgB));
idx = find(case2);
fusedImage(idx) =  imB(idx);
fusedImage(idx+r*c) = imB(idx+r*c);
fusedImage(idx+2*r*c) = imB(idx+2*r*c);

% case 3
case3 = and((maskImgA),(maskImgB));
idx = find(case3);
fusedImage(idx) =  ((double(imA(idx))+double(imB(idx)))/2);
fusedImage(idx+r*c) = ((double(imA(idx+r*c))+double(imB(idx+r*c)))/2);
fusedImage(idx+2*r*c) = ((double(imA(idx+2*r*c))+ double(imB(idx+2*r*c)))/2);

%case 4
case4 = and((~maskImgA),(~maskImgB));
idx = find(case4);
fusedImage(idx) =  ((double(imA(idx))+double(imB(idx)))/2);
fusedImage(idx+r*c) = ((double(imA(idx+r*c))+double(imB(idx+r*c)))/2);
fusedImage(idx+2*r*c) = ((double(imA(idx+2*r*c))+ double(imB(idx+2*r*c)))/2);

end






