How to Remove Grid Lines from an Image

Hi all,
I am looking for guidance on how to remove graphing paper lines from a photograph on ImageJ (first attachment). The objective with the pictures is to calculate the total area occupied by the bugs. I am working on a Macbook Pro and an iMac. The workflow is as follows:
convert image to 8 bit > set scale/calibrate the image > change the threshold > analyze particles. The final product includes the areas of all the bugs but also the area occupied by the grid lines (second attachment). I have tried fiddling with the minimum/maximum pixel size filter and applying gaussian blurs, but nothing rids the photo of the lines entirely without compromising the integrity of the bugs. I have also painted out the lines but this is quite time consuming. I am wondering if there is an easier way to go about this. Any input would be appreciated!

1 Like

Hello @mimiag !

You can try mathematical morphology with horizontal/vertical or diamond elements. The choice of the structuring element here is driven by the orientation of the features to remove (i.e. the horizontal and vertical lines). The operation of interest here is a closing (dilation followed by erosion), which suppresses local minima.

Here’s what I get using the following operations:

  1. Conversion in 8-bits
  2. Grayscale closing with a diamond element with radius 2 (with the MorpholibJ package)
  3. Pseudo flat field correction because the image is not evenly illuminated (with the Biovoxxel package and 100 pixels )
  4. Manual threshold.
  5. The resulting binarized image may exhibit some small artefacts due to the intersections of the lines. I removed them with Area opening (MorpholibJ ).

Operations 2 and 3 can surely be inverted. I haven’t tried. Also, you can adjust the values of some parameters if needed.


Hi @Nicolas,
Amazing! This is really informative. I have downloaded the MorpholibJ package but there is no option to grayscale close with a diamond element with a radius of two. Below are the options provided.

Am I missing something? This is all very new to me so thanks again for your help!

1 Like

Hi again @Nicolas
We have figured out the morphological filters/MorpholibJ plugin and have managed to blur most of the grid.

However we remain confused about step 3 with the Biovoxxel package. When we do the pseudo flat field correction we get two images (attached). What are we supposed to do with them? and why are certain parts of the image highlighted? Is there a way to get all of the bugs equally highlighted?

thank you again for your help!!

1 Like

Hello again @mimiag

the pseudo flat field correction is for eliminating background variations so that no part of the background has similar gray values as your objects (I hope my phrasing is clear). In your second image, it is possible you don’t need it.
Anyhow, the flat fiel correction produces two images, one for the background (the one on the left), and the corrected image (on the right). This is the corrected image you want to binarize, in order to discriminate your insects from the background.
But, in the second example you present, I am pretty sure you don’t need this step. It is required only if you have uneven illumination, such as corners darker than the middle. If you can afford it, a good practice is to use some lighting fixture that produces flat illumination and no shadows, for example a ring light.
For my own information, why do you use grid paper?


Hi @Nicolas
Thanks for clearing that all up, it makes much more sense.
The grid paper was just what was available as an appropriate backdrop for scaling the bugs. Unfortunately we didn’t foresee the problem of having to edit it out when analyzing the images. In the future we will probably use blank paper with a small grid in the corner for calibration.
Anyway, thanks again so much for your help! Really appreciate it :slight_smile:

1 Like


Try using a Fourier Transform and then filter the high frequencies (bright frequencies that represent patterns in a picture).

It is really useful in order to remove horizontal/vertical patterns in the background as gridlines.

Here it’s how the FFT of your image looks like. The vertical and horizontal bright lines, i.e., the x and y axis, represent the pattern in the background. You have to remove it by drawing rectangles and filling them.

After removing the bright spots and drawing rectangles to filter this high frequencies that represent the background:

And here it’s the image after applying the Inverse FFT:

It might not be the better result (I did it in 2 mins.) but by more accurately modifying the original FFT image, i.e., more accurates rectangles rectangles, I’m sure you’ll get an image without such patterns.

Finally, make sure your rectanges fon fill the brisgth frequencies in the position (0,0) in the FFT image since the center of the FFT image contains the relevant information of the image.