Moravec检测角点,给出matlab代码。
时间: 2023-11-22 15:55:24 浏览: 40
Moravec角点检测是一种计算机视觉算法,用于检测图像中的角点。下面是一段MATLAB代码实现:
```matlab
function [cim, r, c] = Moravec(im, w, threshold)
% MORAVEC Corner Detector.
% [CIM, R, C] = MORAVEC(IM, W, THRESHOLD) detects corners in image IM
% using the Moravec corner detector. W is the window size used for
% feature extraction (typically W=3 or 5). THRESHOLD is a threshold
% value between 0 and 1 used to suppress weak corners. CIM is a binary
% image marking the corner locations. R and C are column vectors
% containing the row and column coordinates of the detected corners.
%
% Example:
% im = imread('cameraman.tif');
% [cim, r, c] = Moravec(im, 3, 0.2);
% figure; imshow(im); hold on; plot(c,r,'ys'); hold off;
%
% See also: harris, corner, detectHarrisFeatures, detectMinEigenFeatures,
% detectFASTFeatures, detectSURFFeatures, detectMSERFeatures,
% detectBRISKFeatures, detectORBFeatures, detectHOGFeatures,
% extractHOGFeatures, matchFeatures, estimateGeometricTransform,
% imregister, vision.PointTracker.
% Check input arguments
error(nargchk(2, 3, nargin));
if nargin < 3
threshold = 0.1;
end
% Compute second moments
Ix = conv2(im, [-1 0 1], 'same');
Iy = conv2(im, [-1; 0; 1], 'same');
Ixx = Ix .^ 2;
Iyy = Iy .^ 2;
Ixy = Ix .* Iy;
% Compute corner response
cim = zeros(size(im));
r = [];
c = [];
offset = floor(w / 2);
for i = offset+1:size(im,1)-offset
for j = offset+1:size(im,2)-offset
Sxx = sum(sum(Ixx(i-offset:i+offset,j-offset:j+offset)));
Syy = sum(sum(Iyy(i-offset:i+offset,j-offset:j+offset)));
Sxy = sum(sum(Ixy(i-offset:i+offset,j-offset:j+offset)));
cim(i,j) = min(Sxx,Syy);
if cim(i,j) > threshold * max(cim(i-offset:i+offset,j-offset:j+offset), [], 'all')
r = [r; i];
c = [c; j];
end
end
end
end
```
该算法首先计算图像的二阶矩矩阵,然后计算每个像素点的角点响应值。最后,使用阈值将弱角点抑制掉,输出结果为二进制图像(标记角点位置)和角点坐标。