Getting whole line by combining its parts

匆匆过客 提交于 2019-12-21 17:31:11

问题


I have an image and my aim is to get whole line which is shown with red line. I am working with matlab and I don't want to use IM2 = imdilate(IM,SE) function.

Is there any function or method to do that?

The image:

Note: Sorry for bad red line. I drew it with paint.

Edit:

The original image is below:


回答1:


Here's what I have after using imdilate at an intermediate step -

%// 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

Please remember that the motive behind removing the biggest blob at the start of the codes is that without that being removed, we would have gotten the biggest blob being attached to the island blobs that we were trying to connect/combine and thus would have messed up the desired output.

Associated function (taken from 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;

Result -



来源:https://stackoverflow.com/questions/26818240/getting-whole-line-by-combining-its-parts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!