Computer Vision Experiment 3

Self-programming uses the Harris algorithm to perform corner detection on lena.png and cameraman.tif pictures (or other pictures).

The following is the implementation of the MATLAB code for corner detection of lena.png using the Harris algorithm:

% 读取图片
im = imread('lena.png');
figure, imshow(im), title('Original Image');

% 转换为灰度图像
grayIm = rgb2gray(im);
figure, imshow(grayIm), title('Gray Image');

% 计算x、y方向上的Sobel梯度
dx = [-1 0 1; -2 0 2; -1 0 1];
dy = dx';
Ix = conv2(double(grayIm), dx, 'same');
Iy = conv2(double(grayIm), dy, 'same');

% 计算矩阵M中每个元素的值
wSize = 3; % 窗口大小(3x3)
k = 0.04; % Harris参数
M11 = conv2(Ix.^2, ones(wSize), 'same');
M12 = conv2(Ix.*Iy, ones(wSize), 'same');
M22 = conv2(Iy.^2, ones(wSize), 'same');
detM = M11.*M22 - M12.^2;
traceM = M11 + M22;
R = detM - k*traceM.^2;

% 非极大值抑制
thres = 1e-6 * max(R(:));
Rmax = ordfilt2(R, wSize^2, ones(wSize)); % 对于每个像素,取其3x3邻域内最大的R值
Rmax = (R == Rmax) & (R > thres); % 与原始R值相同且大于阈值的为角点

% 显示结果
figure, imshow(im), title('Harris Corners');
hold on
[y, x] = find(Rmax);
plot(x, y, 'ro');

Result display

insert image description here
The following is the implementation of MATLAB code for corner detection of cameraman.tif using the Harris algorithm:

% 读取图片
im = imread('cameraman.tif');
figure, imshow(im), title('Original Image');

% 计算x、y方向上的Sobel梯度
dx = [-1 0 1; -2 0 2; -1 0 1];
dy = dx';
Ix = conv2(double(im), dx, 'same');
Iy = conv2(double(im), dy, 'same');

% 计算矩阵M中每个元素的值
wSize = 3; % 窗口大小(3x3)
k = 0.04; % Harris参数
M11 = conv2(Ix.^2, ones(wSize), 'same');
M12 = conv2(Ix.*Iy, ones(wSize), 'same');
M22 = conv2(Iy.^2, ones(wSize), 'same');
detM = M11.*M22 - M12.^2;
traceM = M11 + M22;
R = detM - k*traceM.^2;

% 非极大值抑制
thres = 1e-6 * max(R(:));
Rmax = ordfilt2(R, wSize^2, ones(wSize)); % 对于每个像素,取其3x3邻域内最大的R值
Rmax = (R == Rmax) & (R > thres); % 与原始R值相同且大于阈值的为角点

% 显示结果
figure, imshow(im), title('Harris Corners');
hold on
[y, x] = find(Rmax);
plot(x, y, 'ro');

Experimental results
insert image description here

Programming uses the SUSAN algorithm to detect the corners of the lena.png and cameraman.tif pictures (or other pictures), compare the results with the results achieved by the Harris algorithm, and analyze the differences between them. Further implement the Moravec algorithm by itself, and compare the differences of the three corner detection algorithms.

The following is the implementation of the MATLAB code for corner detection of lena.png using the SUSAN algorithm:

 读取图片
im = imread('lena.png');
figure, imshow(im), title('Original Image');

% 转换为灰度图像
grayIm = rgb2gray(im);
figure, imshow(grayIm), title('Gray Image');

% 求解平均灰度值
wSize = 7; % 窗口大小(7x7)
threshold = 0.04; % 阈值
A = fspecial('average', wSize);
meanI = imfilter(double(grayIm), A, 'replicate');

% 计算像素与邻域像素之间的差异
diffIm = abs(double(grayIm) - meanI);
mask = ones(wSize);
mask(4, 4) = 0; % 排除中心点

% 计算响应值
C = threshold * ones(size(diffIm));
C(diffIm > C) = diffIm(diffIm > C);
response = sum(sum(C .* mask));

% 显示结果
figure, imshow(im), title('SUSAN Corners');
hold on
[y, x] = find(diffIm > threshold);
plot(x, y, 'ro');

The following is the implementation of the MATLAB code for corner detection of cameraman.tif using the SUSAN algorithm:

% 读取图片
im = imread('cameraman.tif');
figure, imshow(im), title('Original Image');

% 求解平均灰度值
wSize = 256; % 窗口大小(7x7)
threshold = 27; % 阈值
A = fspecial('average', wSize);
meanI = imfilter(double(im), A, 'replicate');

% 计算像素与邻域像素之间的差异
diffIm = abs(double(im) - meanI);
mask = ones(wSize);
mask(4, 4) = 0; % 排除中心点

% 计算响应值
C = threshold * ones(size(diffIm));
C(diffIm > C) = diffIm(diffIm > C);
response = sum(sum(C .* mask));

% 显示结果
figure, imshow(im), title('SUSAN Corners');
hold on
[y, x] = find(diffIm > threshold);
plot(x, y, 'ro');

insert image description here

The following is the implementation of the MATLAB code for corner detection of lena.png using the Moravec algorithm:

% 读取图片
im = imread('lena.png');
figure, imshow(im), title('Original Image');

% 转换为灰度图像
grayIm = rgb2gray(im);
figure, imshow(grayIm), title('Gray Image');

% 计算窗口大小和平移量
wSize = 7; % 窗口大小(7x7)
shifts = [-1 -1; -1 0; -1 1; 0 -1; 0 1; 1 -1; 1 0; 1 1]; % 平移量

% 计算响应值
threshold = 10000; % 阈值
response = zeros(size(grayIm));
for i = wSize+1 : size(grayIm, 1)-wSize
    for j = wSize+1 : size(grayIm, 2)-wSize
        S = zeros(8, 1);
        for k = 1 : size(shifts, 1)
            diffIm = double(grayIm(i:i+wSize-1, j:j+wSize-1)) - ...
                     double(grayIm(i+shifts(k,1):i+wSize-1+shifts(k,1), j+shifts(k,2):j+wSize-1+shifts(k,2)));
            S(k) = sum(diffIm(:).^2);
        end
        response(i,j) = min(S);
    end
end

% 非极大值抑制
thres = 1e-6 * max(response(:));
Rmax = ordfilt2(response, wSize^2, ones(wSize)); % 对于每个像素,取其3x3邻域内最大的响应值
Rmax = (response == Rmax) & (response > thres); % 与原始响应值相同且大于阈值的为角点

% 显示结果
figure, imshow(im), title('Moravec Corners');
hold on
[y, x] = find(Rmax);
plot(x, y, 'ro');

Among them, wSize represents the window size, shifts represents the translation amount, and threshold represents the response value threshold.

insert image description here

% 读取图片
im = imread('cameraman.tif');
figure, imshow(im), title('Original Image');

% 转换为灰度图像
grayIm = im2double(im);
figure, imshow(grayIm), title('Gray Image');

% 计算窗口大小和平移量
wSize = 7; % 窗口大小(7x7)
shifts = [-1 -1; -1 0; -1 1; 0 -1; 0 1; 1 -1; 1 0; 1 1]; % 平移量

% 计算响应值
threshold = 10000; % 阈值
response = zeros(size(grayIm));
for i = wSize+1 : size(grayIm, 1)-wSize
    for j = wSize+1 : size(grayIm, 2)-wSize
        S = zeros(8, 1);
        for k = 1 : size(shifts, 1)
            diffIm = grayIm(i:i+wSize-1, j:j+wSize-1) - ...
                     grayIm(i+shifts(k,1):i+wSize-1+shifts(k,1), j+shifts(k,2):j+wSize-1+shifts(k,2));
            S(k) = sum(diffIm(:).^2);
        end
        response(i,j) = min(S);
    end
end

% 非极大值抑制
thres = 1e-6 * max(response(:));
Rmax = ordfilt2(response, wSize^2, ones(wSize)); % 对于每个像素,取其3x3邻域内最大的响应值
Rmax = (response == Rmax) & (response > thres); % 与原始响应值相同且大于阈值的为角点

% 显示结果
figure, imshow(im), title('Moravec Corners');
hold on
[y, x] = find(Rmax);
plot(x, y, 'ro');

insert image description here

Next, we compare the results and differences of the three corner detection algorithms.

First of all, both the Harris algorithm and the SUSAN algorithm are detection methods based on local features, which perform well when dealing with images with rich noise and texture, but may be affected when dealing with large-scale and low-texture images. The difference is that the Harris algorithm calculates the value of each element in the matrix M, while the SUSAN algorithm calculates the difference between a pixel and neighboring pixels. Therefore, the Harris algorithm can better capture corner features, while the SUSAN algorithm is more suitable for feature extraction in circular areas.

Secondly, the Moravec algorithm calculates the difference between the pixel in the window and the pixel after translation, and then selects the smallest difference value as the response value. Since this algorithm does not consider the correlation between pixels, it performs well in dealing with noise and smooth images, but in images with strong texture or more noise, it is easy to produce a large number of wrong corner results, and it is not effective for rotation and Scale transformations are not robust.

To sum up, different corner detection algorithms are suitable for different types of images and application scenarios. If you need more accurate and robust corner detection results, you can consider using the Harris algorithm; if you need to process low-texture images or feature extraction in circular areas, you can use the SUSAN algorithm; if you need to quickly implement corner detection and noise and If the smooth image has better adaptability, Moravec algorithm can be selected.

Guess you like

Origin blog.csdn.net/weixin_51209821/article/details/130885689