img = imread('image2.png'); gray_img = rgb2gray(img); subplot(121);imshow(gray_img);title('原图'); threshold = 20; % 阈值 radius = 3; % 半径 corners = detectSusan(gray_img, threshold, radius); % 检测角点 edges = detectEdge(gray_img, threshold, radius); % 检测边缘 subplot(122);imshow(gray_img);title('Susan算子'); hold on; [y, x] = find(corners); plot(x, y, 'r.', 'MarkerSize', 10); [y, x] = find(edges); plot(x, y, 'b.', 'MarkerSize', 10); function corners = detectSusan(img, threshold, radius) % Susan算子检测角点 [m, n] = size(img); corners = zeros(m, n); for i = radius+1:m-radius for j = radius+1:n-radius window = img(i-radius:i+radius, j-radius:j+radius); center = window(radius+1, radius+1); count = sum(abs(window(:)-center)<threshold) - 1; if count < 15 corners(i, j) = 1; end end end end function edges = detectEdge(img, threshold, radius) % Susan算子检测边缘 [m, n] = size(img); edges = zeros(m, n); for i = radius+1:m-radius for j = radius+1:n-radius window = img(i-radius:i+radius, j-radius:j+radius); center = window(radius+1, radius+1); count = sum(abs(window(:)-center)<threshold) - 1; if count >= 15 && count <= 18 edges(i, j) = 1; end end end end原理
时间: 2024-04-28 22:19:23 浏览: 109
tuxiangfenge.zip_matlab图片分割_otsu segmentation
这段代码实现了Susan算子来检测图像中的角点和边缘。Susan算子是一种基于灰度共生矩阵的角点检测算法,其原理是将图像中的每个像素与其周围的像素进行比较,统计相似像素的数量来判断该像素是否为角点或边缘。具体来说,对于每个像素点,以其为中心取一个固定大小的窗口,统计窗口中与中心像素相似的像素数量,若数量低于一个阈值则认为该像素为角点,若数量在另一范围内则认为是边缘。代码中的两个函数分别实现了检测角点和边缘的功能,最后在图像上绘制出检测结果。
阅读全文