【发布时间】:2018-04-19 03:15:29
【问题描述】:
我正在尝试在 Matlab 中实现 FAST 角点检测算法,我知道已经有一个预构建的版本。我不知道为什么我的实现似乎检测到甚至没有接近边缘或者只是不好的特征。
readImage1 = imread('img1.png');
im1 = rgb2gray(readImage1);
img1 = medfilt2(im1);
rowStart = 0.1 * size(img1, 1);
rowStart = fix(rowStart);
rowEnd = 0.9 * size(img1, 1);
rowEnd = fix(rowEnd);
colStart = 0.1 * size(img1, 2);
colStart = fix(colStart);
colEnd = 0.9 * size(img1, 2);
colEnd = fix(colEnd);
阅读我的图像,执行中值蓝色。我只希望特征检测从图像边缘开始一定距离。
array = [ ; ];
for c = colStart:colEnd
for r = rowStart:rowEnd
%get the intesity of the pixel
intensity = img1(r, c);
temp = [r,c];
intensity1 = img1((r+3), c);
intensity5 = img1(r+1, c+3);
intensity9 = img1(r-3, c);
intensity13 = img1(r+1, c-3);
threshold = 70;
count = IntensityCount(intensity1, intensity9, intensity5, intensity13, intensity, threshold);
if count >= 3
array = [array; temp];
end
end
end
然后,对于每个像素,将像素的强度与其最重要的 4 个相邻像素进行比较。如果检测到是角点,则保存到 Nx2 数组中。
在此之后,执行非最大抑制。
[rows, columns] = size(array);
discardArray = [ ; ];
for index = 2:rows
currX = array(index);
currY = array(index, 2);
prevX = array(index - 1);
prevY = array(index - 1, 2);
if(adjacencyCheck(currX, currY, prevX, prevY))
currScore = pixelScore(img1, currX, currY);
prevScore = pixelScore(img1, prevX, prevY);
if(currScore > prevScore)
temp = [prevX, prevY];
discardArray = [discardArray; temp];
else
temp = [currX, currY];
discardArray = [discardArray; temp];
end
end
end
discardArray = unique(discardArray, 'rows');
finalArray = setdiff(array,discardArray, 'rows');
然后显示图像上的点。
[rows, columns] = size(finalArray);
for index = 1:rows
img1 = insertMarker(img1, [finalArray(index), finalArray(index,2)], 'x');
end
imshow(img1);
用于强度检查和非最大抑制的函数。
function number = IntensityCount(int1, int9, int5, int13, origIntensity, thresh)
number = 0;
if abs(int1 - origIntensity) > thresh
number = number + 1;
end
if abs(int9 - origIntensity) > thresh
number = number + 1;
end
if abs(int5 - origIntensity) > thresh
number = number + 1;
end
if abs(int13 - origIntensity) > thresh
number = number + 1;
end
end
%Get the score by sum of absolute differences between the pixel and its neighbours.
function scoreNumber = pixelScore(img, r, c)
intOriginal = img(r,c);
intensity1 = img((r+3), c);
intensity3 = img(r+3, c-1);
intensity5 = img(r+1, c+3);
intensity7 = img(r-1, c+3);
intensity9 = img(r-3, c);
intensity11 = img(r-3, c-1);
intensity13 = img(r+1, c-3);
intensity15 = img(r-1, c-3);
scoreNumber = abs(intOriginal - intensity1) + abs(intOriginal - intensity3)+ abs(intOriginal - intensity5) + abs(intOriginal - intensity7) + abs(intOriginal - intensity9) + abs(intOriginal - intensity11) + abs(intOriginal - intensity13) + abs(intOriginal - intensity15);
end
%Check pixel adjacency using Euclidean distance.
function isAdjacent = adjacencyCheck(x1, y1, x2, y2)
xDist = x1 - x2;
yDist = y1 - y2;
dist = (xDist.^2 + yDist.^2).^0.5;
isAdjacent = dist <= 4;
end
为什么我的算法会检测到这么多不良特征?
【问题讨论】:
-
@CrisLuengo insertMarker 是 MatLab 中的一个函数。 mathworks.com/help/vision/ref/insertmarker.html
-
一些笔记:
intensity5 = img1(r+1, c+3),为什么是+1?array = [array; temp];每次追加内容时都需要一个副本,尝试预先分配数组(您可以猜测可以获得多少点,然后在最后裁剪数组),或者在二进制图像中标记点的一个列表。[finalArray(index), finalArray(index,2)]与finalArray(index,:)相同。 -
我确定你知道 FAST 是如何工作的,但如果你不知道,我需要将像素强度与距离中心 3px 的圆中的一定数量的像素进行比较。在纸上追踪像素位置,看来我需要重新检查位置。我会考虑将图像标记到二进制图像上,因为我认为我可以使用 find() 再次查找这些像素。改变这些价值观是可行的,我现在得到了不错的角落。谢谢!
-
我想知道
+1因为对称。如果您要采样 4 个点,我会采样(r+3, c)、(r, c+3),(r-3, c)` 和(r, c-3)。您使用r+1两次,导致四个方向的非各向同性采样。