2016-04-09 12 views
2

私は次のイメージを持っていて、中央の長方形のオブジェクトを分割したいと思います。セグメント化するために次のコードを実装しましたが、オブジェクトを分離できません。イメージ内の長方形のオブジェクトを分離するために、どのような機能やアプローチをとることができますか?この例では長方形のオブジェクトを分離するための最良の方法

im = imread('image.jpg'); 

% convert image to grayscale, 
imHSV = rgb2hsv(im); 
imGray = rgb2gray(im); 
imSat = imHSV(:,:,2); 
imHue = imHSV(:,:,1); 
imVal = imHSV(:,:,3); 

background = imopen(im,strel('disk',15)); 

I2 = im - background; 

% detect edge using sobel algorithm 
[~, threshold] = edge(imGray, 'sobel'); 
fudgeFactor = .5; 
imEdge = edge(imGray,'sobel', threshold * fudgeFactor); 
%figure, imshow(imEdge); 

% split image into colour channels 
redIM = im(:,:,1); 
greenIM = im(:,:,2); 
blueIM = im(:,:,3); 

% convert image to binary image (using thresholding) 
imBlobs = and((imSat < 0.6),(imHue < 0.6)); 
imBlobs = and(imBlobs, ((redIM + greenIM + blueIM) > 150)); 
imBlobs = imfill(~imBlobs,4); 
imBlobs = bwareaopen(imBlobs,50); 

figure,imshow(imBlobs); 

enter image description here enter image description here

答えて

4

、あなたは長方形が良好な初期マスクを構築するために、そのコーナーの全てに青い含まれているという事実を活用することができます。

  1. しきい値を使用して、画像内の青色の位置を特定し、初期マスクを作成します。
  2. この初期マスクが与えられると、最小および最大演算を使用してそのコーナーを見つけます。
  3. 四角形を受け取るために、四隅を線で接続します。
  4. imfillを使用して矩形を塗りつぶします。

コードの例:

% convert image to binary image (using thresholding) 
redIM = im(:,:,1); 
greenIM = im(:,:,2); 
blueIM = im(:,:,3); 
mask = blueIM > redIM*2 & blueIM > greenIM*2; 
%noise cleaning 
mask = imopen(mask,strel('disk',3)); 

%find the corners of the rectangle 
[Y, X] = ind2sub(size(mask),find(mask)); 
minYCoords = find(Y==min(Y)); 
maxYCoords = find(Y==max(Y)); 
minXCoords = find(X==min(X)); 
maxXCoords = find(X==max(X)); 
%top corners 
topRightInd = find(X(minYCoords)==max(X(minYCoords)),1,'last'); 
topLeftInd = find(Y(minXCoords)==min(Y(minXCoords)),1,'last'); 
p1 = [Y(minYCoords(topRightInd)) X((minYCoords(topRightInd)))]; 
p2 = [Y(minXCoords(topLeftInd)) X((minXCoords(topLeftInd)))]; 
%bottom corners 
bottomRightInd = find(Y(maxXCoords)==max(Y(maxXCoords)),1,'last'); 
bottomLeftInd = find(X(minYCoords)==min(X(minYCoords)),1,'last'); 
p3 = [Y(maxXCoords(bottomRightInd)) X((maxXCoords(bottomRightInd)))]; 
p4 = [Y(maxYCoords(bottomLeftInd)) X((maxYCoords(bottomLeftInd)))]; 

%connect between the corners with lines 
l1Inds = drawline(p1,p2,size(mask)); 
l2Inds = drawline(p3,p4,size(mask)); 
maskOut = mask; 
maskOut([l1Inds,l2Inds]) = 1; 

%fill the rectangle which was created 
midP = ceil((p1+p2+p3+p4)./4); 
maskOut = imfill(maskOut,midP); 

%present the final result 
figure,imshow(maskOut); 

最終結果: final result

中間結果(1-後の閾値撮影、2-後に行を追加): enter image description here

* drawline機能は、drawline webpage

関連する問題