2014-03-19 188 views
2

我正在嘗試裁剪下面圖像的頂部,左側,底部和右側邊界。在圖像中裁剪頂部,左側,底部和右側邊界(MATLAB)

Image to Crop

所以,基本上,我在尋找創造的東西,會採取像上面輸入和輸出這些圖像:

North Crop

West Crop

South Crop

East Crop

在MATLAB中可以使用houghlines檢測直線和它們的尺寸,但是如何找到圖像中凹凸部分的位置?我嘗試使用regionpropsextrema屬性,但它不會檢測凹曲線(只給出凸曲線的極值點)。我需要找出凹/凸曲線中的最低/最高點,但我是不知道如何去做。儘管如此,我還是有一個步驟。一旦我知道他們,我可以很容易地使用imcrop來劃分出相應的邊界。

+0

這是你的真實形象?那麼你的形象真的是黑色和白色?因爲如果是這樣,就像找到直線的交點一樣簡單,然後找到相應子陣列中的最後/第一個白色像素;無需訴諸功能檢測。 –

+0

哦,哇...我會試試,但Divakar的方法功能完美。謝謝你的提示;深夜編碼會讓人頭腦發熱! – user1106340

+0

@ user1106340我想這就是我所做的,簡單的數學,沒有複雜的措施。 – Divakar

回答

2

代碼

%%// Tolerance in percentage for the outliers/noise in the image because 
%%// of which the edges are not perfectly vertical or horizontal and 
%%// the ovalish blobs are not "round" enough 
f=2; 

%%// Read in your image 
img = im2bw(imread('patt1.png')); 

%%// Main processing 
sum1 = sum(img,1); 
box_startx = find(sum1>0.33*size(img,1),1); 
box_stopx = size(img,2) - find(fliplr(sum1)>0.33*size(img,1),1) + 1; 

sum2 = sum(img,2)'; %' 
box_starty = find(sum2>0.33*size(img,2),1); 
box_stopy = size(img,1) - find(fliplr(sum2)>0.33*size(img,2),1) + 1; 

blob_leftx = find(sum1>(1-0.01*f)*max(sum1),1); 
blob_rightx = size(img,2) - find(fliplr(sum1)>(1-0.01*f)*max(sum1),1) + 1; 

blob_topy = find(sum2>(1-0.01*f)*max(sum2),1); 
blob_bottomy = size(img,1) - find(fliplr(sum2)>(1-0.01*f)*max(sum2),1) + 1; 

top1 = img(1:blob_topy,box_startx+1:box_stopx); 
left1 = img(box_starty:box_stopy-1,1:blob_leftx); 
bottom1 = img(blob_bottomy:end,box_startx:box_stopx); 
right1 = img(box_starty:box_stopy,blob_rightx:end); 

%// Debug 
figure, 
subplot(2,2,1);imshow(top1) 
subplot(2,2,2);imshow(bottom1) 
subplot(2,2,3);imshow(left1) 
subplot(2,2,4);imshow(right1) 

輸出

enter image description here

相關問題