img = imread('right01.jpg'); gray_img = rgb2gray(img); sigma = 1.5; window_size = 5; k = 0.04; Ix = imfilter(gray_img, [-1 0 1; -2 0 2; -1 0 1], 'same'); Iy = imfilter(gray_img, [-1 -2 -1; 0 0 0; 1 2 1], 'same'); G = fspecial('gaussian', window_size, sigma); Ix2 = imfilter(Ix.^2, G, 'same'); Iy2 = imfilter(Iy.^2, G, 'same'); Ixy = imfilter(Ix.*Iy, G, 'same'); R = (Ix2.*Iy2 - Ixy.^2) - k*(Ix2 + Iy2).^2; threshold = 0.01; R_max = max(max(R)); R_threshold = R > threshold*R_max; R_nonmax = imregionalmax(R); figure; subplot(1,3,1); imshow(img); title('原始图像'); subplot(1,3,2); imshow(gray_img); title('灰度图像'); subplot(1,3,3); imshow(R_nonmax); title('Harris角点检测结果');出现错误使用 rgb2gray>parse_inputs MAP 必须为 m x 3 的数组,请对 RGB 和灰度图像使用 im2gray怎么解决
时间: 2023-08-26 11:03:37 浏览: 224
这个错误提示是因为你传入的图像是彩色图像,而 `rgb2gray` 函数需要的输入是一个 m x n x 3 的 RGB 图像。如果你想要将彩色图像转换为灰度图像,可以使用 `im2gray` 函数代替 `rgb2gray` 函数。修改代码如下:
```
img = imread('right01.jpg');
gray_img = im2gray(img);
sigma = 1.5;
window_size = 5;
k = 0.04;
Ix = imfilter(gray_img, [-1 0 1; -2 0 2; -1 0 1], 'same');
Iy = imfilter(gray_img, [-1 -2 -1; 0 0 0; 1 2 1], 'same');
G = fspecial('gaussian', window_size, sigma);
Ix2 = imfilter(Ix.^2, G, 'same');
Iy2 = imfilter(Iy.^2, G, 'same');
Ixy = imfilter(Ix.*Iy, G, 'same');
R = (Ix2.*Iy2 - Ixy.^2) - k*(Ix2 + Iy2).^2;
threshold = 0.01;
R_max = max(max(R));
R_threshold = R > threshold*R_max;
R_nonmax = imregionalmax(R);
figure;
subplot(1,3,1); imshow(img); title('原始图像');
subplot(1,3,2); imshow(gray_img); title('灰度图像');
subplot(1,3,3); imshow(R_nonmax); title('Harris角点检测结果');
```
这样就可以避免出现上述错误了。
阅读全文