在matlab中查找像素位置
我需要扫描(一半)二进制图像并仅存储(或标记)第一个黑色像素的位置。我在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 技术交流群。

为了避免循环,您还可以使用 Matlab 的
find
函数:这意味着“找到 I 中第一个等于 1 的元素”。 (
find(I==1,k,first)
查找前 k 个元素)。(顺便说一句 - 文档似乎没有提及他们是否在找到第一个索引后停止扫描矩阵(如果这就是所要求的) - 我认为是这样,出于效率目的?)。
To avoid the loop, you could also use Matlab's
find
function: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?).