【发布时间】:2017-01-17 15:38:53
【问题描述】:
我目前正在研究 octave 的边缘检测器。来自 Java 和 Python 等其他编程语言,我习惯于在 for 循环中进行迭代,而不是对整个矩阵执行操作。现在在八度音程中,这会导致严重的性能下降,而且我很难弄清楚如何对我的代码进行矢量化。我有以下两段代码:
1)
function zc = ZeroCrossings(img, T=0.9257)
zc = zeros(size(img));
# Iterate over central positions of all 3x3 submatrices
for y = 2:rows(img) - 1
for x = 2:columns(img) - 1
ndiff = 0;
# Check all necessary pairs of elements of the submatrix (W/E, N/S, NW/SE, NE/SW)
for d = [1, 0; 0, 1; 1, 1; 1, -1]'
p1 = img(y-d(2), x-d(1));
p2 = img(y+d(2), x+d(1));
if sign(p1) != sign(p2) && abs(p1 - p2) >= T
ndiff++;
end
end
# If at least two pairs fit the requirements, these coordinates are a zero crossing
if ndiff >= 2
zc(y, x) = 1;
end
end
end
end
2)
function g = LinkGaps(img, k=5)
g = zeros(size(img));
for i = 1:rows(img)
g(i, :) = link(img(i, :), k);
end
end
function row = link(row, k)
# Find first 1
i = 1;
while i <= length(row) && row(i) == 0
i++;
end
# Iterate over gaps
while true
# Determine gap start
while i <= length(row) && row(i) == 1
i++;
end
start = i;
# Determine gap stop
while i <= length(row) && row(i) == 0
i++;
end
# If stop wasn't reached, exit loop
if i > length(row)
break
end
# If gap is short enough, fill it with 1s
if i - start <= k
row(start:i-1) = 1;
end
end
end
这两个函数都迭代子矩阵(或第二种情况下的行和子行),尤其是第一个似乎大大减慢了我的程序。
-
此函数采用像素矩阵 (
img) 并返回二进制 (0/1) 矩阵,其中找到零交叉点(对应的 3x3 邻域满足特定要求的像素)为 1。外部的 2 个 for 循环似乎应该可以以某种方式矢量化。我可以将主体放入它自己的函数中(将必要的子矩阵作为参数),但我不知道如何在所有子矩阵上调用此函数,将它们对应的(中心)位置设置为返回值。
如果内部 for 循环也可以向量化,则加分。
-
此函数从前一个输出中获取二进制矩阵,并填充其行中的空白(即将它们设置为 1)。间隙定义为一系列长度
现在我确定至少外循环(
LinkGaps中的那个)是可矢量化的。但是,link中的while循环再次对子向量进行操作,而不是单个元素,所以我不确定如何对其进行向量化。
【问题讨论】:
-
任务 1 很可能通过卷积来解决,其内核旨在检测零交叉。
标签: matlab octave vectorization