在matlab中查找像素位置

发布于 12-29 17:58 字数 279 浏览 4 评论 0原文

我需要扫描(一半)二进制图像并仅存储(或标记)第一个黑色像素的位置。我在matlab中尝试过这个。但代码显示了所有黑色像素的位置。我的代码是这样的。

I= imread('binary image');

imshow(I);

[r c] =size(I);

for j=1:c/2

    for i=1:r

        if(I(i,j)==1)
        [i j]    
        end
    end
end

有什么建议可以改进这个...

I need to scan a (half of) binary image and store (or mark) only the position of first black pixel. I tried this out in matlab. But the code is displaying the positions of all the black pixels. My code is something like this.

I= imread('binary image');

imshow(I);

[r c] =size(I);

for j=1:c/2

    for i=1:r

        if(I(i,j)==1)
        [i j]    
        end
    end
end

Any suggestions to improve this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

给妤﹃绝世温柔2025-01-05 17:58:04

为了避免循环,您还可以使用 Matlab 的 find 函数:

[i,j] = find(I==1,1,'first')

这意味着“找到 I 中第一个等于 1 的元素”。 (find(I==1,k,first) 查找前 k 个元素)。

(顺便说一句 - 文档似乎没有提及他们是否在找到第一个索引后停止扫描矩阵(如果这就是所要求的) - 我认为是这样,出于效率目的?)。

To avoid the loop, you could also use Matlab's find function:

[i,j] = find(I==1,1,'first')

This means "find the first element of I being equal to 1". (find(I==1,k,first) finds th first k elements).

(As an aside -- docs don't seem to mention whether they stop scanning the matrix after the first index found (if that's all that's requested) -- I'd assume so, for efficiency purposes?).

御守2025-01-05 17:58:04

您需要退出循环:

found = 0; % a flag
for j=1:c/2
  for i=1:r

    if(I(i,j)==1)
        [i j]
        found = 1;
        break; % stop the inner loop  
    end

    if (found)
        break; % stop the outer loop
    end
  end
end

You need to exit from the loops:

found = 0; % a flag
for j=1:c/2
  for i=1:r

    if(I(i,j)==1)
        [i j]
        found = 1;
        break; % stop the inner loop  
    end

    if (found)
        break; % stop the outer loop
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文