MATLAB代码优化:提高角度差异识别准确性
MATLAB代码优化:提高角度差异识别准确性
本文提供MATLAB代码优化建议,以提高角度差异识别的准确性。代码示例涵盖特征点提取、匹配、几何变换估计等步骤,并通过调整参数和方法提升识别效果。
原始代码:
close all;
clear all;
clc;
tic
%num=calculate_number(x,y);
% Read images
image1 = imread('1550.64 nm - LP31 mode/01.BMP');
image2 = imread('1550.64 nm - LP31 mode/13.BMP');
% Convert to grayscale
grayImage1 = rgb2gray(image1);
grayImage2 = rgb2gray(image2);
% Perform edge detection
edgeImage1 = edge(grayImage1, 'Canny');
edgeImage2 = edge(grayImage2, 'Canny');
% Extract feature points
points1 = detectSURFFeatures(edgeImage1, 'MetricThreshold', 100, 'NumOctaves', 3);
points2 = detectSURFFeatures(edgeImage2, 'MetricThreshold', 100, 'NumOctaves', 3);
% Extract feature descriptors
[features1, validPoints1] = extractFeatures(edgeImage1, points1);
[features2, validPoints2] = extractFeatures(edgeImage2, points2);
% Match feature points
indexPairs = matchFeatures(features1, features2, 'Prenormalized', true);
%,'Method', 'RANSAC', 'MaxRatio', 0.5, 'MatchThreshold', 10
matchedPoints1 = validPoints1(indexPairs(:, 1));
matchedPoints2 = validPoints2(indexPairs(:, 2));
% Estimate geometric transformation
[tform,~,~] = estimateGeometricTransform(matchedPoints1, matchedPoints2, 'affine', 'MaxNumTrials', 1000);
%inlierPoints1,inlierPoints2
%~,~
%, 'MaxNumTrials', 10000
% Calculate angle difference
angleDifference = atan2d(tform.T(2, 1), tform.T(1, 1));
% Extract rotation matrix
%rotationMatrix = tform.T(1:2, 1:2);
% Convert rotation matrix to Euler angles
%eulerAngles = rotm2eul(rotationMatrix);
% Calculate rotation angle in degrees
%rotationAngle = rad2deg(eulerAngles(1));
% Display grayimages and angle difference
figure;
subplot(1, 2, 1);
imshow(edgeImage1);
title('Image 1');
subplot(1, 2, 2);
imshow(edgeImage2);
title('Image 2');
suptitle('Edge Images');
figure;
showMatchedFeatures(image1, image2, matchedPoints1,matchedPoints2);
title('Matched Points');
legend('Image 1', 'Image 2');
fprintf('Angle Difference: %.2f degrees
', angleDifference);
toc
disp(['运行时间:',num2str(toc)]);
优化后的代码:
close all;
clear all;
clc;
tic
% Read images
image1 = imread('1550.64 nm - LP31 mode/01.BMP');
image2 = imread('1550.64 nm - LP31 mode/13.BMP');
% Convert to grayscale
grayImage1 = rgb2gray(image1);
grayImage2 = rgb2gray(image2);
% Perform edge detection
edgeImage1 = edge(grayImage1, 'Canny');
edgeImage2 = edge(grayImage2, 'Canny');
% Extract feature points
points1 = detectSURFFeatures(edgeImage1, 'MetricThreshold', 500, 'NumOctaves', 5); % 调整MetricThreshold和NumOctaves参数
points2 = detectSURFFeatures(edgeImage2, 'MetricThreshold', 500, 'NumOctaves', 5); % 调整MetricThreshold和NumOctaves参数
% Extract feature descriptors
[features1, validPoints1] = extractFeatures(edgeImage1, points1);
[features2, validPoints2] = extractFeatures(edgeImage2, points2);
% Match feature points
indexPairs = matchFeatures(features1, features2, 'Prenormalized', true, 'Method', 'Approximate', 'MaxRatio', 0.7); % 使用Approximate匹配方法,并调整MaxRatio参数
matchedPoints1 = validPoints1(indexPairs(:, 1));
matchedPoints2 = validPoints2(indexPairs(:, 2));
% Estimate geometric transformation
[tform, inlierPoints1, inlierPoints2] = estimateGeometricTransform(matchedPoints1, matchedPoints2, 'affine', 'MaxNumTrials', 10000); % 增加MaxNumTrials的值以增加迭代次数
% Calculate angle difference
angleDifference = atan2d(tform.T(2, 1), tform.T(1, 1));
% Display gray images and matched points
figure;
subplot(1, 2, 1);
imshow(edgeImage1);
title('Image 1');
subplot(1, 2, 2);
imshow(edgeImage2);
title('Image 2');
suptitle('Edge Images');
figure;
showMatchedFeatures(image1, image2, inlierPoints1, inlierPoints2);
title('Matched Points');
legend('Image 1', 'Image 2');
fprintf('Angle Difference: %.2f degrees
', angleDifference);
toc
disp(['运行时间:',num2str(toc)]);
优化说明:
- 调整
MetricThreshold和NumOctaves参数:这两个参数分别控制SURF特征点检测的阈值和尺度空间层数。通过调整参数,可以提取更多或更精确的特征点。 - 使用
Approximate匹配方法:该方法比默认的RANSAC方法更快,但准确性略低。可以根据实际情况调整MaxRatio参数,以平衡速度和准确性。 - 增加
MaxNumTrials的值:该参数控制几何变换估计的迭代次数。增加迭代次数可以提高估计的准确性。
进一步优化建议:
- 尝试使用不同的特征点检测方法,例如SIFT或ORB。
- 使用RANSAC方法进行特征点匹配,以过滤掉错误匹配。
- 使用其他几何变换模型,例如仿射变换或透视变换。
- 对图像进行预处理,例如降噪或增强对比度,以提高特征点提取和匹配的准确性。
希望以上代码优化建议对您有所帮助。您可以根据需要,根据注释中的提示进行进一步调整和修改。尝试不同的参数值和方法,以找到适合您数据的最佳设置。
原文地址: https://www.cveoy.top/t/topic/RH3 著作权归作者所有。请勿转载和采集!