2

I try to do image registration on two grayscale images where the images were taken twice with different views. The images were taking by myself using a Lifecam camera.

To register these images, I used template matching method and normalized cross correlation as similarity measure and found the right location. But the result after combination of these two images was not good as I wish. I don't know how to fix it. Do I need to do some rotation or translation first before combine it? If so, I have no idea how to get the real angle for rotation. Or do you have any idea how to fix the image result without applying any rotation?

Input image 1:

input image 1

Input Image 2:

input image 2

Result:

result

This my code:

A = imread('image1.jpg');
B = imread('image2.jpg');

[M1, N1] = size(A);      % size imej A n B  
[M2, N2] = size(B);
%% finding coordinated of (r2,c2)
r1 = size(A,1)/2;        % midpoint of image A as coordinate
c1 = size(A,2
template = imcrop(A,[(c1-20) (r1-20) 40 40]);
[r2, c2] = normcorr(temp,B);     % Normalized cross correlation

%% count distance of coordinate (r1,c1) in image A and (r2,c2)in image B 
UA = r1;           % distance of coordinate (r1,c1) from top in image A
BA = M1 - r1;      % distance of coordinate (r1,c1) from bottom 
LA = c1;           % left distance from (r1,c1)
RA = N1 - c1;      % right distance from (r1,c1)

UB = r2;           % finding distance of coordinate (r2,c2) from top, 
BB = M2 - r2;      % bottom, left and right in image B  
LB = c2;
RB = N2 - c2;
%% zero padding for both image
if LA > LB  
L_diff = LA - LB;           % value of columns need to pad with zero on left side        
B = [zeros(M2,L_diff),B];
else
L_diff = LB - LA;
 A = [zeros(M1,L_diff),A];
end
if RA > RB  
 R_diff = RA - RB;          % value of columns need to pad with zero on right side
 B = [B, zeros(M2,R_diff)];
else
 R_diff = RB - RA;
 A = [A, zeros(M1,R_diff)];
end
N1 = size(A, 2);                    % renew value column image A and B    
N2 = size(B, 2);
if UA > UB 
 U_diff = UA - UB;                  % value of rows need to pad with zero on top
 B = [zeros(U_diff,N2);B];
else
 U_diff = UB - UA;
 A = [zeros(U_diff,N1);A];
end
if BA > BB 
 B_diff = BA - BB;         % value of rows need to pad with zero on bottom
 B = [B; zeros(B_diff,N2)];
else
 B_diff = BB - BA;
 A = [A; zeros(B_diff,N1)];
end
%% find coordinate that have double value
if LA > LB
 r = r1;
 c = c1;
else
 r = r2;
 c = c2;
end
if UA >= UB
 i_Start = r - UB + 1;
else
 i_Start = r - UA + 1;
end
if BA >= BB
 i_Stop = r + BB ;
else
 i_Stop = r + BA;
end
 if LA >= LB
 j_Start = c - c2 + 1;
else
 j_Start = c - c1 + 1;
end
if RA >= RB 
 j_Stop = c + RB;
else
 j_Stop = c + RA;
end
%% add image A and B 
A = im2double(A);
B = im2double(B);
final_im = A + B;
for i = i_Start:i_Stop
 for j = j_Start:j_Stop
     final_im(i,j) = final_im(i,j)/2;
 end
end

final_im = im2uint8(final_im);
badr
  • 21
  • 3
  • have you checked [this](https://stackoverflow.com/questions/29127181/matching-images-with-different-orientations-and-scales-in-matlab)? – Ryan L Jul 04 '17 at 18:05
  • i don't have detectSURFFeatures function in matlab toolbox. i use version 2014b – badr Jul 04 '17 at 18:29
  • Hmm... well please check [this](https://stackoverflow.com/questions/30677494/surface-feature-detection-on-image-processing/30678620#30678620). – Ryan L Jul 04 '17 at 18:34
  • 2
    Please show us the code you used to register the two images and combine them. Also, please indicate if you have the image processing toolbox as part of your MATLAB distribution. Depending on how good the registration is, it may simply be a blending issue or it could really be that you need a similarity transform rather than a translation. – rayryeng Jul 04 '17 at 23:29

2 Answers2

0

The answer from rayryeng in Ryan L's first link is quite applicable here. Cross-correlation likely won't provide a close enough match between the two images since the transformation between the two images is more accurately described as a homography than a 2D rigid transform.

Accurate image registration requires that you find this projective transformation. To do so you can find a set of corresponding points in the two images (using SURF, as mentioned above, usually works well) and then use RANSAC to obtain the homography's parameters from the corresponding points. RANSAC does a nice job even when some of the "corresponding" features in your two images are actually not correct matches. Once found, you can use the transformation to move one of your images to the other's point of view and fuse.

Here's a nice explanation of feature matching, RANSAC, and fusing two images with some Matlab code samples. The lecture uses SIFT features, but the idea still works for SURF.

woodstockhausen
  • 339
  • 1
  • 6
0

Best published way to perform such a registration is based on fiducial points. You can choose the most clear edges or crossing points as a fiducial and then adjust the smoothness and regularization parameter to register them together. look at the SlicerRT package. and let me know if you face any problem.

fati
  • 125
  • 5