2017-04-20 4 views
1

enter image description herewie Region zu trennen, die nicht andere Region enthalten

Ich habe Xc, Yc, zC Matrix gibt es nur drei Farbkanäle in der Handlung rot, grün und blau. Die zC-Matrix hat Werte entweder 1 für Blau, 2 für Grün oder 3 für Rot.

Ich möchte nur grün zu isolieren, die blau enthält, aber nicht das grün, das um es herum schwimmt.

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); 

Antwort

1

haben Sie Zugriff auf die Image Processing Toolbox Unter der Annahme, die Funktionen verwenden imfill und regionprops, hier eine Lösung:

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)); 

Dies gibt Ihnen eine Reihe von linearer Indizes greenIndex für alle Pixel in dem grünen Bereich die blaue Regionen umschließen.

1

ist hier eine andere Lösung, die Sie auf die Bildverarbeitung Toolbox haben Zugriff gelten folgende Voraussetzungen:

% 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;