图像识别-性能问题

发布于 2024-12-12 18:06:33 字数 617 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生一个溪 2024-12-19 18:06:34

如果它只是一条水平线,即矩阵的所有行,您可以这样做:

sigma = var(matrix, 0, 2);
[val, idx] = sort(sigma, 'ascend');

其中 val(1) 将包含最小方差和 idx(1)对应的行索引。如果您正在寻找任何可以扫描的直线,请查看所谓的跟踪变换。

If it is just a horizontal line, i.e. all rows of a matrix, you can do it with:

sigma = var(matrix, 0, 2);
[val, idx] = sort(sigma, 'ascend');

Where val(1) will contain the minimum variance and idx(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.

一抹苦笑 2024-12-19 18:06:34

这是一种更快的方法(根据您对问题的描述)。变量行将具有方差最小的行号:

[~, row] = min(var(img, 0, 2));

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:

[~, row] = min(var(img, 0, 2));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文