图像识别-性能问题
我的 matlab 脚本存在性能问题。
我想做的是找到一条水平线,沿该线的强度值方差最小。简单的实现如下。问题是如何重写它以提高性能。
% img is some previously loaded image
height = size(img,1);
width = size(img,2);
smallestVar = inf;
smallestXline = [];
smallestYline = [];
for i=1:height,
for j=1:width,
for k=i+1:height,
xline = [j j];
yline = [i k];
variance = var(improfile(img,xline,yline));
if variance < smallestVar
smallestVar = variance;
smallestXline = xline;
smallestYline = yline;
end
end
end
end
I've got a performance problem in my matlab script.
What I'm trying to do is to find a horizontal line for which variance of intensity values along it is the smallest. The naive implementation is below. The question is how to rewrite it to boost performance.
% img is some previously loaded image
height = size(img,1);
width = size(img,2);
smallestVar = inf;
smallestXline = [];
smallestYline = [];
for i=1:height,
for j=1:width,
for k=i+1:height,
xline = [j j];
yline = [i k];
variance = var(improfile(img,xline,yline));
if variance < smallestVar
smallestVar = variance;
smallestXline = xline;
smallestYline = yline;
end
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它只是一条水平线,即矩阵的所有行,您可以这样做:
其中
val(1)
将包含最小方差和idx(1)
对应的行索引。如果您正在寻找任何可以扫描的直线,请查看所谓的跟踪变换。If it is just a horizontal line, i.e. all rows of a matrix, you can do it with:
Where
val(1)
will contain the minimum variance andidx(1)
the corresponding row index. If you are looking for any straight line to scan along, have a look at the so called Trace Transform.这是一种更快的方法(根据您对问题的描述)。变量行将具有方差最小的行号:
This is a faster way of doing it (based on your description of the problem). Variable row will have the row number which has the minimum variance: