MATLAB:通过二值图像中的像素位置查找连通分量

MATLAB: find connected component by pixel location in binary image

我有一个二进制图像,我使用 bwconncomp 和 regionprops 将其划分为感兴趣的区域。我有一行像素,我想将其放置在二值图像上...并找到 interest/connected 分量的哪个区域中的行像素最多。所以在附图中,我在 bwimage 上使用 bwconncomp,我得到组件 1、2、3。然后我有一个对应于蓝线的像素列表。我想找出哪个连接组件中的蓝线最多(#1)。

我想应该是……

line=(some pixel values);
cc=bwconncomps(bwimage);
tempvar=[];

for i=1:length(bwconncomps)
     tempvar=find(cc.PixelIdxList{i}==line);
end

[~ answer]=max(tempvar);

您可以使用 ismember 来确定 PixelList 中有多少项出现在沿线的像素列表中。然后您可以使用 max 来确定拥有最多的组件。

% Indices of pixels that are along the line of interest
linePixels = [1, 2, 3, 4, ...]  

% Determine the number of pixels within each component that are on this line
nPerComponent = cellfun(@(inds)sum(ismember(inds, linePixels)), {cc.PixelIdxList});

% Find the component with the most points on this line
[~, answer] = max(nPerComponent);