Aligning 2 images for removing noise from an object.

1 view (last 30 days)
So I have this 2 images: https://imgur.com/a/0K93iEA and want to overlay them so that the bright light which covers the book disappears. At the momemt I realized DLT to one image (I must do this) so it has the same angle as the other one but I don't know hot to overlay the objects exactly as the function imfuse() doesn't seem to workout for me. I attached my code below.
im1 = (imread("img1.jpeg"));
im2 = (imread("img2.jpeg"));
I = (im1 + im2)/2;
%figure(1); imshow(I);
figure(1); imshow(im1);
%x1 = []; y1 = [];
%for j=1:4
% zoom on;
% pause();
% zoom off;
% [x1(j),y1(j)]=ginput(1);
% zoom out;
%end
%save('pt','x1','y1');
figure(2); imshow(im2);
%x2 = []; y2 = [];
%for j=1:4
% zoom on;
% pause();
% zoom off;
% [x2(j),y2(j)]=ginput(1);
% zoom out;
%end
%save('pt2','x2','y2');
load('pt');
load('pt2');
M = [];
for i=1:4
M = [ M ;
x1(i) y1(i) 1 0 0 0 -x2(i)*x1(i) -x2(i)*y1(i) -x2(i);
0 0 0 x1(i) y1(i) 1 -y2(i)*x1(i) -y2(i)*y1(i) -y2(i)];
end
% soluciono el sistema
[u,s,v] = svd( M );
H = reshape( v(:,end), 3, 3 )';
H = H / H(3,3);
% fi DLT
RO = imref2d(size(im1)); % marc de referència
B = imwarp(im2, projective2d(inv(H')));
figure(3); imshow(B); %B is im2 rotated to the angle of im1
%_________________________________________________________%

Answers (1)

VINAYAK LUHA
VINAYAK LUHA on 24 Nov 2023
Edited: VINAYAK LUHA on 24 Nov 2023
Hi Miguel,
I understand that you have two images of a book with differing orientations and locations of noise. Further you want to align the book object in both the images and eliminate the existing noise.
Here is how you can align the object in the two images and eliminate noise from them-
  1. Acquire the homography transformation matrix between "image1" and "image2".
  2. Use the obtained matrix to transform "image2" to align with "image1", resulting in the transformed image named "warpedImage".
  3. To reduce noise, create a new image from "image1" and "warpedImage" such that white pixels in "image1" are replaced with corresponding pixels from "warpedImage", and white pixels in "warpedImage" are replaced with corresponding pixels from "image1".
The following code demonstrates the aforementioned method-
% Read the two images
image1 = imread('i1.jpeg');
image2 = imread('i2.jpeg');
% Detect and extract features from both images
points1 = detectSURFFeatures(rgb2gray(image1));
points2 = detectSURFFeatures(rgb2gray(image2));
[features1, validPoints1] = extractFeatures(rgb2gray(image1), points1);
[features2, validPoints2] = extractFeatures(rgb2gray(image2), points2);
% Match features between the two images
indexPairs = matchFeatures(features1, features2);
matchedPoints1 = validPoints1(indexPairs(:, 1), :);
matchedPoints2 = validPoints2(indexPairs(:, 2), :);
% Estimate the homography matrix using RANSAC
[tform, inlierPoints1, inlierPoints2] = estimateGeometricTransform(matchedPoints2, matchedPoints1, 'projective');
% Visualize the homography
figure;
showMatchedFeatures(image2, image1, inlierPoints1, inlierPoints2, 'montage');
title("showMatchedFeatures")
outputView = imref2d(size(image1));
warpedImage = imwarp(image2, tform, 'OutputView', outputView);
figure;
imshowpair(image1, warpedImage, 'montage');
title("showMatchedFeatures")
title("warpedImage")
% Convert the images to grayscale
grayImage1 = rgb2gray(image1);
grayWarpedImage1 = rgb2gray(warpedImage);
% Create a binary mask for pixels that are white in image1
whiteMask1 = grayImage1 == 255;
% Create a binary mask for pixels that are white in warpedImage2
whiteMask2 = grayWarpedImage1 == 255;
% Create the new image by combining pixels based on the masks
newImage = image1;
newImage(whiteMask1) = warpedImage(whiteMask1);
newImage(whiteMask2) = image1(whiteMask2);
% Display the new image
figure;
imshow(newImage);
title('Noise corrected Image');
Find the screenshots of the obtained result as attachments to this answer.
Further, refer to the following documentations to know more about the functions used in the above code -
  1. https://www.mathworks.com/help/vision/ref/detectsurffeatures.html
  2. https://www.mathworks.com/help/vision/ref/extractfeatures.html
  3. https://www.mathworks.com/help/vision/ref/matchfeatures.html
  4. https://www.mathworks.com/help/images/ref/imref2d.html
  5. https://www.mathworks.com/help/images/ref/imwarp.html
Hope this helps you in understanding how to align the object in the two images and eliminate noise from them.
Regards,
Vinayak Luha

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!