MATLAB从桌面示例图像文件夹中读取一张彩色图像,转换为灰度图,使用cornerHarris函数提取图像角点,比较不同阈值设定下提取角点效果(如0.01、0.02),上述结果显示在同一界面。可用的完整代码
时间: 2024-11-22 14:34:21 浏览: 38
在MATLAB中,你可以通过以下步骤完成这个任务:
1. 首先,你需要导入图像。假设图片文件名为`image.jpg`,你可以使用`imread`函数来读取:
```matlab
img = imread('image.jpg'); % 替换 'image.jpg' 为你实际的图像文件名
```
2. 将彩色图像转换为灰度图,可以使用`rgb2gray`函数:
```matlab
gray_img = rgb2gray(img);
```
3. 使用`cornerHarris`函数来检测角点。这是一个局部特征检测函数,需要设置阈值参数。例如,你可以分别尝试0.01和0.02作为阈值:
```matlab
% 设置不同的阈值并计算角点
threshold_1 = 0.01;
corners_1 = cornerHarris(gray_img, threshold_1);
threshold_2 = 0.02;
corners_2 = cornerHarris(gray_img, threshold_2);
```
4. 现在你需要在一个图形窗口中显示原始图像以及两个不同阈值下的角点结果。这可以通过`imagesc`和`hold on/off`来实现:
```matlab
figure; % 创建新窗口
subplot(1, 3, 1) % 分割窗口,第一行三列的第一个区域
imagesc(img); % 显示原图
title('Original Image');
subplot(1, 3, 2)
imagesc(gray_img); % 显示灰度图
hold on; % 保持当前图像状态以便叠加角点
scatter(corners_1(:,2), corners_1(:,1), 'filled', 'MarkerSize', 5, 'Color', 'red');
title(['Threshold: ' num2str(threshold_1)]);
subplot(1, 3, 3)
imagesc(gray_img); % 同样显示灰度图
hold on;
scatter(corners_2(:,2), corners_2(:,1), 'filled', 'MarkerSize', 5, 'Color', 'green');
title(['Threshold: ' num2str(threshold_2)]);
hold off; % 清除叠加
% 别忘了显示所有轴
axis equal;
```
运行这段代码后,你会看到一个包含三个部分的图像窗口,展示了原始图像、0.01阈值下的角点和0.02阈值下的角点。
阅读全文
相关推荐


















