如何分隔不包含其他区域的区域

时间:2017-04-20 17:00:42

标签: matlab

enter image description here

我有xC,yC,zC矩阵,红色,绿色和蓝色的图中只有三个颜色通道。 zC矩阵的值为1表示蓝色,2表示绿色,3表示红色。

我想只隔离包含蓝色但不包含绿色的绿色。

inb=zC==1;
xCb = xC(inb);   %isolate blue
yCb = yC(inb);

ing=zC==2;     %isolate green


xCg = xC(ing);
yCg = yC(ing);


inFg = inpolygon(xCb,yCb,xCg,yCg);  %tried inpolygon

in=zC==2;
zg = zC(in);  %This is not correct
zgV = zg(~inFg);

2 个答案:

答案 0 :(得分:1)

假设您可以访问Image Processing Toolbox以使用函数imfillregionprops,这是一个解决方案:

blueMask = (zC == 1);   % Mask of blue regions
greenMask = (zC == 2);  % Mask of green regions

% Fill "holes" in the green mask, then get a list of pixel indices for each filled blob:
greenCC = regionprops(imfill(greenMask, 'holes'), 'PixelIdxList');

% Find which blobs have pixel indices that index any blue pixels:
blobIndex = cellfun(@(index) any(blueMask(index)), {greenCC.PixelIdxList});

% Collect pixel indices from these filled blobs:
greenIndex = vertcat(greenCC(blobIndex).PixelIdxList);

% Remove the indices of the filled holes:
greenIndex = greenIndex(greenMask(greenIndex));

这将为包含蓝色区域的绿色区域中的所有像素提供一组线性索引greenIndex

答案 1 :(得分:1)

这是另一种假设您可以访问图像处理工具箱的解决方案:

% find green pixels with neighboring blue pixels. The linear indices will
% be stored in inds
zC_ = zeros(size(zC)+2);
zC_(2:(end-1),2:(end-1)) = zC;
neighboors = (zC==2) & ...
    ((zC_(1:(end-2),1:(end-2))==1) |...
    (zC_(1:(end-2),2:(end-1))==1) |...
    (zC_(1:(end-2),3:end)==1) |...
    (zC_(2:(end-1),1:(end-2))==1) |...
    (zC_(2:(end-1),3:end)==1) |...
    (zC_(3:end,1:(end-2))==1) |...
    (zC_(3:end,2:(end-1))==1) |...
    (zC_(3:end,3:end)==1));
inds = find(neighboors);

% find all connected regions of green pixels which are in the vector inds.
% result will be stored in inds2.
conn = bwlabeln(zC==2);
inds2 = ismember(conn(:),unique(conn(inds)));

% create an output image with 0's anywhere beside the regions you need are
% interested in
out = zeros(size(zC));
out(inds2) = 1;