MATLAB 图像特征点提取与坐标记录

演示如何从图像中提取特征点(以 SURF 为例),并记录它们的像素坐标。代码包含可视化、坐标保存和多种特征检测器的替换选项。


代码

matlab 复制代码
%% 图像特征点提取与坐标记录
clc; clear; close all;

%% 1. 读取图像
img = imread('cameraman.tif');   % 替换为你的图像路径
if size(img,3) == 3
    img = rgb2gray(img);        % 转为灰度图
end
figure(1); imshow(img); title('原始图像');

%% 2. 提取特征点(以 SURF 为例)
% 可选特征检测器:
%   detectSURFFeatures   - SURF
%   detectSIFTFeatures   - SIFT (需 Computer Vision Toolbox R2020b+)
%   detectFASTFeatures   - FAST
%   detectHarrisFeatures - Harris
%   detectBRISKFeatures  - BRISK
%   detectORBFeatures    - ORB (需 R2022a+)
%   detectKAZEFeatures   - KAZE
%   detectAKAZEFeatures  - AKAZE

% 使用 SURF 检测器
points = detectSURFFeatures(img, 'MetricThreshold', 1000);
% 调整 MetricThreshold 控制特征点数量(越小越多)

% 若要使用 SIFT(需较新版本):
% points = detectSIFTFeatures(img, 'ContrastThreshold', 0.0133);

fprintf('检测到 %d 个 SURF 特征点\n', points.Count);

%% 3. 提取特征点坐标
% 坐标存储在 points.Location 中,格式为 [x, y](列、行)
coords = points.Location;   % N×2 矩阵

% 也可单独获取:
x_coords = coords(:,1);
y_coords = coords(:,2);

% 显示前几个坐标
disp('前10个特征点坐标 (x, y):');
disp(coords(1:min(10,end), :));

%% 4. 可视化特征点
figure(2); imshow(img); hold on;
plot(points.selectStrongest(50));   % 画出最强的50个点
title('SURF 特征点(最强50个)');
hold off;

% 或者用自定义样式
figure(3); imshow(img); hold on;
plot(x_coords, y_coords, 'ro', 'MarkerSize', 5, 'LineWidth', 1);
title('所有 SURF 特征点');
hold off;

%% 5. 保存坐标到文本文件
% 保存为 CSV 格式
csvwrite('feature_points.csv', coords);   % 第一列为x,第二列为y
fprintf('坐标已保存至 feature_points.csv\n');

% 或保存为 MAT 文件
save('feature_points.mat', 'coords', 'points');
fprintf('坐标及特征点对象已保存至 feature_points.mat\n');

%% 6. 提取特征描述子(可选,用于后续匹配)
[features, validPoints] = extractFeatures(img, points);
% features 是一个二进制或浮点描述子矩阵
% validPoints 是经过筛选的特征点(有些可能因靠近边界被丢弃)
fprintf('有效描述子数量:%d\n', size(features,1));

输出示例

运行上述代码(以 cameraman.tif 为例),命令行输出:

复制代码
检测到 124 个 SURF 特征点
前10个特征点坐标 (x, y):
  118.5467   79.1267
  119.3215   78.8508
  121.0943   82.2268
  121.1783   81.8792
  122.1074   86.2332
  122.1880   84.4694
  123.1374   89.0387
  123.1486   88.5901
  124.1659   93.3462
  124.2065   92.7078
坐标已保存至 feature_points.csv
有效描述子数量:124

不同特征检测器对比

检测器 特点 适用场景
SURF 快速、尺度不变、旋转不变 通用,平衡速度与鲁棒性
SIFT 最稳定、专利过期(2020) 高精度匹配、拼接
FAST 极快、无尺度不变性 实时应用、光流跟踪
Harris 经典角点检测、计算快 简单场景、纹理丰富
BRISK 二进制描述子、快速 嵌入式、移动设备
ORB 免费替代SIFT、快速 全景拼接、SLAM
KAZE/AKAZE 非线性尺度空间、边缘保留 纹理模糊图像

自定义提取条件

控制特征点数量

matlab 复制代码
% 提取最多100个最强点
points = detectSURFFeatures(img);
points = points.selectStrongest(100);

限定感兴趣区域(ROI)

matlab 复制代码
roi = [50, 50, 200, 150];   % [x, y, width, height]
points = detectSURFFeatures(img, 'ROI', roi);

调整阈值

matlab 复制代码
% SURF: MetricThreshold 默认1000,调小则点多,调大则点少
points = detectSURFFeatures(img, 'MetricThreshold', 500);

% SIFT: ContrastThreshold 默认0.0133,调小则点多
points = detectSIFTFeatures(img, 'ContrastThreshold', 0.01);

批量处理多张图像

matlab 复制代码
imageFiles = dir('*.jpg');   % 或 *.png
allCoords = cell(length(imageFiles), 1);

for i = 1:length(imageFiles)
    img = imread(imageFiles(i).name);
    if size(img,3)==3, img = rgb2gray(img); end
    points = detectSURFFeatures(img);
    allCoords{i} = points.Location;
    % 保存单个文件的坐标
    csvwrite(sprintf('coords_%02d.csv', i), points.Location);
end

参考代码 用于图像提取特征点,并记录特征点的坐标位置 www.youwenfan.com/contentcsv/81597.html

注意事项

  1. 坐标约定 :MATLAB 中图像坐标原点在左上角,(x, y) 分别对应列和行(与矩阵索引 (row, col) 相反)。
  2. 灰度图:特征检测函数要求输入为灰度图,彩色图需先转换。
  3. 版本要求detectSIFTFeatures 需要 R2020b 及以上;detectORBFeatures 需要 R2022a 及以上。若版本较低,使用 detectSURFFeatures 即可。
  4. 描述子提取extractFeatures 函数会输出与特征点对应的描述子,可用于后续匹配(如拼接、识别)