Search code examples
imageimagemagickimage-manipulationimagemagick-converthough-transform

Imagemagick: How to remove grid lines and dark regions from an image?


I have a microscopic image with grid of straight lines (not vertical or horizontal).

Grid of straight lines

I know that ImageMagick have -hough-lines operator that can detect straight lines. How to use it to remove such lines?


Solution

  • Here is one way to process your image in Imagemagick.

    (Note that -hough-lines is only accurate to 1 degree. So it can find the lines, but they will not be aligned well enough to use them for removal.)

    There are two major steps. First find the angle of rotation. Second rotate the image and then use morphology to remove the horizontal and then the vertical lines.

    Input:

    enter image description here

    First step does a division normalization to even out the background white, then does an automatic threshold, then does a -deskew to get the rotation angle. I have included +write xxx.png to save the images step-by-step. You can remove these +write xxx.png, but not the -write mpr:xxx.

    angle=`magick gridded_ellipse.jpg -write mpr:img \
    \( mpr:img -blur 0x99 \) +swap -compose divide -composite +write gridded_ellipse_div_normalize.png \
    -auto-threshold triangle +write gridded_ellipse_threshold.png -deskew 40% +write gridded_ellipse_thresh_deskew.png -format "%[deskew:angle]" info:`
    

    Division Normalized:

    enter image description here

    Division Normalized and Thresholded:

    enter image description here

    Division Normalized, Thresholded and Deskewed:

    enter image description here

    Second Step starts with the previous division normalized image and rotates it. Then it takes the previous deskewed image and apply a horizontal morphology close, negates it to make the lines white on black and adds that to the rotated image. Then it applies a vertical morphology close to the deskewed image, negates it to make the lines white on black and adds that to the previous result from the horizontal processing. This forms the final result.

    magick gridded_ellipse_div_normalize.png -rotate $angle +write gridded_ellipse_norm_rot.png +write mpr:rot +delete \
    \( gridded_ellipse_thresh_deskew.png +write mpr:deskew -morphology close rectangle:50x1 -negate +write gridded_ellipse_thresh_morph_horiz.png  \
    mpr:rot -compose plus -composite +write gridded_ellipse_no_horiz.png -write mpr:no_horiz \) \
    \( mpr:deskew -morphology close rectangle:1x50 -negate +write gridded_ellipse_thresh_morph_vert.png  \
    mpr:no_horiz -compose plus -composite +write gridded_ellipse_result.png \) \
    null:
    

    Division Normalized and Rotated Input:

    enter image description here

    Division Normalized, Rotated and Thresholded Input:

    enter image description here

    Morphology Close Horizontal:

    enter image description here

    Morphology Close Horizontal Added To Rotated Image:

    enter image description here

    Morphology Close Vertical:

    enter image description here

    Morphology Close Vertical Added To Previous Result - Final Image:

    enter image description here