4

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: enter image description here

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

Edit:

The original image is below:

kkuilla
  • 2,226
  • 3
  • 34
  • 37
ffttyy
  • 853
  • 2
  • 19
  • 49
  • 1
    Could you link us to the original image too (one without the red line)? And what if we could get you the red line itself, but as a white line of course, would that be okay? – Divakar Nov 08 '14 at 14:54
  • 1
    haha I was about to ask the exact same question @Divakar :) – Benoit_11 Nov 08 '14 at 15:00
  • @Divakar I edited my question with link of original image. Of course I want to get white line :) Red one is just to show the direction of output white line. – ffttyy Nov 08 '14 at 15:03
  • By the way, I do think that `imdilate` would be needed here as an intermediate step, would that be okay? – Divakar Nov 08 '14 at 15:22

1 Answers1

4

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 -

enter image description here

Community
  • 1
  • 1
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • nice great job! +1 from me :) – Benoit_11 Nov 08 '14 at 15:42
  • 1
    @Benoit_11 Thanks!! It's been sometime I had something on IP done! And good to get some feedback from the pundits of IP on SO ;) – Divakar Nov 08 '14 at 15:47
  • Thank you for your excellent answer. I have difficulty to understand the codes of 'biggest_blob(BW)' function. Could you add some comments to these codes? – ffttyy Nov 08 '14 at 18:30
  • @sigara See if the added comments makes it easier on you? :) – Divakar Nov 08 '14 at 18:50
  • @sigara Check out the original solution from where that biggest blob finding code was taken, that was an interesting problem too - http://stackoverflow.com/a/22515304/3293881 – Divakar Nov 08 '14 at 18:58
  • Things are more clear for me now :) Thank you for all of your help. – ffttyy Nov 08 '14 at 20:07
  • +1 - Very nice Divakar :). Would you consider using `imclearborder` to remove all of those blobs along the border of the image as an alternative? Of course you would still need it to select the biggest blob out of those that are not connected to the border. – rayryeng Nov 09 '14 at 00:39
  • @rayryeng Thanks bud! Well I did think about that, but then I realised it would have removed that bottom right island that touches the boundary as well, whose thinned part we need in the final output. BTW thanks for proposing that tool imclearborder, could be useful in future! :) – Divakar Nov 09 '14 at 04:25
  • @Divakar oh yes of course. I missed that part of the boundary. Cool! – rayryeng Nov 09 '14 at 04:48