Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何使用循环查找像素标签的索引_Matlab_Loops_Image Processing_Matrix - Fatal编程技术网

Matlab 如何使用循环查找像素标签的索引

Matlab 如何使用循环查找像素标签的索引,matlab,loops,image-processing,matrix,Matlab,Loops,Image Processing,Matrix,我有这个分割图像,我需要找到所有标记为“20”的像素的索引 我知道我可以很容易地用代码做到这一点: img = [00 00 00 00 00 00 00 00; 00 20 00 00 00 20 00 00; 00 00 30 00 00 00 00 00; 10 10 10 00 20 00 00 00; 10 10 10 40 40 40 40 40; 10 10 10 40 40 40 20 40; 1

我有这个分割图像,我需要找到所有标记为“20”的像素的索引 我知道我可以很容易地用代码做到这一点:

img = [00 00 00 00 00 00 00 00;
       00 20 00 00 00 20 00 00;
       00 00 30 00 00 00 00 00;
       10 10 10 00 20 00 00 00;
       10 10 10 40 40 40 40 40;
       10 10 10 40 40 40 20 40;
       10 10 10 40 40 40 40 40];

[img_row, img_col] = find(img==20)
imgIdx             = sub2ind(size(img), img_row, img_col);
这将返回感兴趣像素的所有索引的向量。但是,我更希望一个接一个地找到这些像素,我知道:

imgIdx = find(img==20, 1)

将返回这些像素中第一个像素的索引。因此,有没有一种方法可以使用循环找到这些像素的其余部分?非常感谢您的任何帮助/建议/建议。非常感谢。

当然,在通常包含10个数千到数百万个元素的图像中循环是没有效率的。但如果你坚持,总有一个循环解决方案

for ii = 1:numel(img)
    if img(ii) == 20
        % do_the_thing
    end
end
尽管如此,即使我必须循环执行
%的操作,我也会在获得所有索引后执行:

imgIdx = find(img == 20);
for ii = 1:numel(imgIdx)
    % imgIdx(ii) !!!
    % do_the_thing
end

为什么要使用循环btw?除此之外,
imgIdx=find(img==20)
将返回与这两行返回相同的结果:
[img\u行,img\u列]=find(img==20)‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍  <代码>imgIdx=sub2ind(大小(img)、img\u行、img\u列)最好的选择是您已经在做的方式。在imgIdx上循环,而不是在find上循环。@oma11,遵循erfan的建议:不要使用第一个代码示例,使用第二个。另外,如果有可能避免这种循环,那就去做吧。谢谢@erfan和Stewie Griffin。我怀疑第一个解决方案是否有效,第二个几乎就像我试图避免的那样。因此,我最好描述一下我最初尝试做的事情。请参阅原始帖子的编辑版本。谢谢,对不起,伙计们!erfan提出的第二个解决方案非常有效。我只是不知道这有多容易。我想这一切都需要一点耐心/时间来解决这些简单的问题。谢谢@erfan。。。我真的很感激。