Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw matched points between two images in MATLAB

I am working on a fingerprint recognition system and have completed the step in feature extraction.

I have two sets of matching points between two images. What I want to do is create an image so that the two images are displayed side by side. This image should show the matching points connected with lines: One end of each line is connected to a matching point in the first image and other end of the line is connected to the corresponding matching point in the second image.

Thanks in advance.

like image 405
Ritesh Avatar asked Mar 29 '14 16:03

Ritesh


2 Answers

You can use the showMatchedFeatures function that is built-in to MATLAB. The function can take either a pair of features structures if you have used any of their built-in feature detection algorithms, or two matrices of size N x 2. Two of them are required as each are describing what pairs of co-ordinates between the two images correspond to each other. The function also requires the two images that you detected keypoints for.

You can either present them overlaid with some transparency, or the most popular method is to place them side by side each other with a line drawn between each pair of corresponding points (which I believe is what you're after).

Check out the MATLAB documentation for more details:

http://www.mathworks.com/help/vision/ref/showmatchedfeatures.html

If you want to see some example code and output, check here:

http://www.mathworks.com/help/vision/ref/showmatchedfeatures.html#btfmijs

ANOTHER OPTION

NB: You will need the Computer Vision toolbox in order to run the showMatchedFeatures function. I'm not sure what toolboxes you have available. If you don't have the Computer Vision toolbox, then you could stack the two images side by side, then run a loop through each pair of points and draw a line through them. This is assuming that both images are the same type (uint8, uint16, etc.) and are both grayscale.

Assuming you have the Image Processing Toolbox available, you could do something like this:

% Assuming im1 and im2 are already loaded into your environment
% im1 and im2 are the two images you are comparing to
% points1 and points2 are M x 2 matrices of corresponding points
% between im1 and im2
% The co-ordinates in the ith row of points1 correspond to the
% ith row of points2
% Important Note: Each row assumes co-ordinates in (x,y) format
% x - horizontal, y - vertical
% y is assumed to be y-down (i.e. downwards is positive)

figure;
stackedImage = cat(2, im1, im2); % Places the two images side by side
imshow(stackedImage);
width = size(im1, 2);
hold on;
numPoints = size(points1, 1); % points2 must have same # of points
% Note, we must offset by the width of the image
for i = 1 : numPoints
    plot(points1(i, 1), points1(i, 2), 'y+', points2(i, 1) + width, ...
         points2(i, 2), 'y+');
    line([points1(i, 1) points2(i, 1) + width], [points1(i, 2) points2(i, 2)], ...
         'Color', 'yellow');
end

This should roughly achieve what showMatchedFeatures does.


What if both images are not of the same dimensions?

If you have a situation where both of the images you want to compare to do not have the same dimensions (i.e. both images not sharing the same number of rows and/or columns), you simply create an output image which is blank first such that the total number of rows for the output is the maximum of the rows between the two and the total number of columns is the sum of them together. Therefore, you simply do this. This is assuming that both images are of the same type and are grayscale like earlier:

figure;
[rows1,cols1] = size(im1);
[rows2,cols2] = size(im2);

%// Create blank image
stackedImage = zeros(max([rows1,rows2]), cols1+cols2);
stackedImage = cast(stackedImage, class(im1)); %// Make sure we cast output
%// Place two images side by side
stackedImage(1:rows1,1:cols1) = im1;
stackedImage(1:rows2,cols1+1:cols1+cols2) = im2;

%// Code from before
imshow(stackedImage);
width = size(im1, 2);
hold on;
numPoints = size(points1, 1); % points2 must have same # of points
% Note, we must offset by the width of the image
for i = 1 : numPoints
    plot(points1(i, 1), points1(i, 2), 'y+', points2(i, 1) + width, ...
         points2(i, 2), 'y+');
    line([points1(i, 1) points2(i, 1) + width], [points1(i, 2) points2(i, 2)], ...
         'Color', 'yellow');
end
like image 55
rayryeng Avatar answered Oct 03 '22 08:10

rayryeng


I personally needed to work with color images, so I figured I'd post the code by rayryeng for images of different sizes but adapted for color image implementation (note that points are now in row xy format instead of column xy format)

figure;
[rows1,cols1] = size(im1(:,:,1));
[rows2,cols2] = size(im2(:,:,1));
%// Create blank image
stackedImage = uint8(zeros(max([rows1,rows2]), cols1+cols2,3));
%// Place two images side by side
stackedImage(1:rows1,1:cols1,:) = im1;
stackedImage(1:rows2,cols1+1:cols1+cols2,:) = im2;
%// Code from before
imshow(stackedImage);
width = size(im1(:,:,1), 2);
hold on;
numPoints = size(points1, 2); % points2 must have same # of points
% Note, we must offset by the width of the image
for i = 1 : numPoints
    plot(points1(1, i), points1(2, i), 'b*', points2(1, i) + width, ...
         points2(2, i), 'r*');
    line([points1(1, i) points2(1, i) + width], [points1(2, i) points2(2, i)], ...
         'Color', 'green');
end
like image 39
Evan Smith Avatar answered Oct 03 '22 10:10

Evan Smith