如何在图像中裁剪顶部,左侧,底部和右侧边界?

时间:2014-03-19 04:56:16

标签: image matlab image-processing crop edge-detection

我正在尝试裁剪下方图片的上,左,下,右边界。

Image to Crop

所以,基本上,我正在寻找能够将上面的图像作为输入并输出这些图像的东西:

North Crop

West Crop

South Crop

East Crop

可以使用MATLAB中的houghlines检测直线及其尺寸,但如何在图像中找到凸片和凹片的位置?我尝试使用regionpropsextrema属性,但它不会检测到凹曲线(只给出了凸曲线的极值点。)我需要找出最低点/最高点。凹/凸曲线,但我不知道如何去做。尽管如此,我还是迈出了一步;我知道后,我可以轻松地使用imcrop裁剪出相应的边界。

1 个答案:

答案 0 :(得分: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