Don't know what is wrong with my output of this code
1 view (last 30 days)
Show older comments
function [MOVINGREG] = registerMarsImages(MOVING,FIXED)
% Convert images to grayscale if they are RGB
if size(MOVING, 3) == 3
MOVING = rgb2gray(MOVING);
end
if size(FIXED, 3) == 3
FIXED = rgb2gray(FIXED);
end
% Detect features in both images
points1 = detectSURFFeatures(MOVING, 'MetricThreshold', 1000);
points2 = detectSURFFeatures(FIXED, 'MetricThreshold', 1000);
% Extract features from both images
[features1, valid_points1] = extractFeatures(MOVING, points1);
[features2, valid_points2] = extractFeatures(FIXED, points2);
% Match features by using their descriptors
indexPairs = matchFeatures(features1, features2, 'MatchThreshold', 10, 'MaxRatio', 0.7);
% Retrieve locations of corresponding points for each image
matchedPoints1 = valid_points1(indexPairs(:, 1), :);
matchedPoints2 = valid_points2(indexPairs(:, 2), :);
% Estimate the transformation between the moving and fixed images
[tform, inlierIdx] = estimateGeometricTransform2D(matchedPoints1, matchedPoints2, 'projective', 'Confidence', 99.9, 'MaxNumTrials', 2000);
% Get the output limits for the transformation
[xLimitsMoving, yLimitsMoving] = outputLimits(tform, [1 size(MOVING, 2)], [1 size(MOVING, 1)]);
[xLimitsFixed, yLimitsFixed] = outputLimits(projective2d(eye(3)), [1 size(FIXED, 2)], [1 size(FIXED, 1)]);
% Determine the size of the panorama
xMin = min([xLimitsMoving xLimitsFixed]);
xMax = max([xLimitsMoving xLimitsFixed]);
yMin = min([yLimitsMoving yLimitsFixed]);
yMax = max([yLimitsMoving yLimitsFixed]);
width = round(xMax - xMin);
height = round(yMax - yMin);
% Create an empty panorama canvas
panorama = zeros(height, width, 'like', FIXED);
% Create reference objects for the panorama and the fixed image
panoramaView = imref2d([height width], [xMin xMax], [yMin yMax]);
fixedRef = imref2d(size(FIXED), [xLimitsFixed(1) xLimitsFixed(2)], [yLimitsFixed(1) yLimitsFixed(2)]);
% Warp the moving image into the panorama
registered = imwarp(MOVING, tform, 'OutputView', panoramaView);
% Overlay the fixed image onto the panorama
panorama = imwarp(FIXED, projective2d(eye(3)), 'OutputView', panoramaView, 'FillValues', 0);
panorama = max(panorama, registered);
% Resize the panorama to fit within 1024x1024
scaleFactor = min(1024 / width, 1024 / height);
panoramaScaled = imresize(panorama, scaleFactor);
% Create a 1024x1024 canvas
finalPanorama = zeros(1024, 1024, 'like', FIXED);
% Determine the position to center the scaled stitched image within the canvas
[scaledHeight, scaledWidth] = size(panoramaScaled);
xOffset = max(0, round((1024 - scaledWidth) / 2));
yOffset = max(0, round((1024 - scaledHeight) / 2));
% Place the scaled stitched image onto the 1024x1024 canvas
finalPanorama(yOffset + (1:scaledHeight), xOffset + (1:scaledWidth)) = panoramaScaled;
% Store the registered image
MOVINGREG.RegisteredImage = finalPanorama;
% Store the transformation object
MOVINGREG.Transformation = tform;
end
% Load the images
fixedImg = imread("sol_03333_opgs_edr_ncam_NLB_693387385EDR_F0921230NCAM00259M_.JPG");
leftImg = imread("sol_03333_opgs_edr_ncam_NLB_693387301EDR_F0921230NCAM00259M_.JPG");
% Register the two images
reg = registerMarsImages(leftImg, fixedImg)
figure; imshow(reg.RegisteredImage)
3 Comments
Accepted Answer
Cris LaPierre
on 19 Jun 2024
Edited: Cris LaPierre
on 19 Jun 2024
Your result has stitched all 3 images together. However, the function should be returning a single image (the moving one only).
"The grader is checking that your registration function can produce a result close to the expected solution registration function. You will still need to correctly warp and stitch the images together."
Make sure you are submitting code generated by the Registration Estimator app.
0 Comments
More Answers (0)
See Also
Categories
Find more on Geometric Transformation and Image Registration 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!