2014-03-19 193 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

相关问题