获取每个连接组件的最大值的索引

Getting the indices of the max value of each connected component

给定一张图像 img,我在 Matlab 中使用 bwconncomp 获得了连通分量。从返回的连接组件中,我可以获得每个组件的最大值。但是,如何获得最大值的索引?我想知道图像中最大值实际出现的位置。

这是我目前所拥有的伪代码:

 cc = bwconncomp(img)
 % iterate through the length(cc.PixelIdxList)
      value(it) = max(cc.PixelIdxList{:,it})
      x(it) = ?
      y(it) = ?

如何获得 xy

像这样:

 ind(it) = max(cc.PixelIdxList{:,it})
 [x(it),y(it)] = ind2sub(size(img),ind(it))

实际上PixelIdxList gives you the linear index of the pixel, and not its value, and you can use ind2sub将其转换为下标索引。

要获取值本身,请键入:

value(it) = img(ind(it));