imagematlabimage-processingbinary-image

Filter Binary Image to keep only the lines


I have the following image initial image and after I've converted it to binary binary image, I want in some way to keep only the 3 lines shown and remove the other objects. I've tried several things using region props etc in Matlab but I didnt manage to filter out these areas. Any idea?


Solution

  • Hough transform may be the first way to refer, but in case you are not satisfied with results, you can try to use morphological opening with lines as structuring elements. Since your lines are directed to several different directions, you should try any possible directions and then merge yielding images. If you have a prior knowledge about directions, you can make use of it and filter by direction too.

    An example code block may look like this:

    my_img = imread('img.png'); % get your image here
    opened=[];
    se_length = 25; % chose in pixels, according to your need
    for derece = 50:2:70 % use 0:3:180 for all directions
        se_line = strel('line', se_length, derece);
        opened = cat(3, opened, imopen(my_img, se_line));
    end
    
    max_opened = max(opened, [], 3); % merge images based on maximum (or your own method)
    

    Morphological opening can be used to filter many shapes. Just select the structuring element according to your need. The above scripts give the following result for your specific image example:

    opened and maximized image