通过组合其各个部分来获得整线

时间:2014-11-08 14:42:43

标签: matlab image-processing line

我有一张图片,我的目标是获得用红线显示的整条线。我正在使用matlab,我不想使用IM2 = imdilate(IM,SE)函数。

是否有任何功能或方法可以做到这一点?

图片: enter image description here

注意:抱歉红线不好。我用油漆画了它。

编辑:

原始图片如下:

1 个答案:

答案 0 :(得分:4)

这是我在中间步骤中使用imdilate后所拥有的内容 -

%// Read in image and convert to a binary one
im = imread('Line.jpg');
bw = im2bw(im);

%// There seems to be a thin white boundary across the image, make it false(black)
bw1 = false(size(bw));
bw1(5:end-5,5:end-5) = bw(5:end-5,5:end-5);

bw1(biggest_blob(bw1)) = 0; %// remove biggest blob (bottom left corner one)

SE = strel('disk', 11, 8); %// structuring element for dilation
bw2 = imdilate(bw1,SE); %// dilate the image
bw3 = bwmorph(bw2,'thin',Inf); %// thin it
out = biggest_blob(bw3); %// out of many thinned lines, select the biggest one

请记住,在代码开头删除最大blob背后的动机是,如果没有被移除,我们会得到最大的斑点附着在我们试图连接/组合的岛屿blob上,因此搞砸了所需的输出。

相关功能(取自Select largest object in an image) -

function out = biggest_blob(BW)

%// Find and labels blobs in the binary image BW
[L, num] = bwlabel(BW, 8); 

%// Count of pixels in each blob, basically this should give the area of each blob
counts = sum(bsxfun(@eq,L(:),1:num)); 

%// Get the label(ind) cooresponding to blob with the maximum area 
%// which would be the biggest blob
[~,ind] = max(counts);

%// Get only the logical mask of the biggest blob by comparing all labels 
%// to the label(ind) of the biggest blob
out = (L==ind);

return;

结果 -

enter image description here