Detecting and measuring gridlines with skimage

I’m trying to detect the gridlines in images like this:

I have 10,000 of these images, so I can’t tweak thresholding on each one.

These images are pictures of copepods photographed over a 1mm square grid; the ultimate goal is to be able to use these photographs to measure the copepods.

The problem currently at hand is how to reliably identify and measure the grid.

What I’ve tried so far

Working in scikit-image, I can perform edge-detection with canny, then find the lines using a hough transformation, with mixed results.

Next I identify the intersections between the lines, select an intersection at random, and attempt to measure the side length of the grid in pixels.

Here’s an example of a success:

Here are some examples of failures:



Challenges

Because canny finds both edges of each gridline, hough often finds more than one line per gridline. Having extra lines leads to having multiple intersections in close proximity, making it more difficult to identify the grid size.

Is there a way to get edge detection and/or hough to give me only one line for each gridline?
OR
Is there a good way to merge the lines that I get out of hough?
OR
Is there some other way to solve this problem that I haven’t thought of?

Thank you!

Hi @liocoa welcome to the forum! Have you tried corner detection by any chance? https://scikit-image.org/docs/stable/auto_examples/features_detection/plot_corner.html#sphx-glr-auto-examples-features-detection-plot-corner-py

With some adjusted value of threshold_rel this could be a good way to detect the intersections of the grid.

As for the canny / hough approach, the thresholds for canny seem to be quite high (ie you don’t get a lot of points for the edges), you could try to lower the canny threshold and to increase the threshold in hough_line_peaks instead. If you want to merge close parallel lines or lines separated by a very small angle, you can adjust the min_distance and min_angle parameters of hough_line_peaks, did you already try this?

I’d take a look at the min_distance and min_angle parameters of hough_line_peaks.

Once you have one fitted line per grid-line, you can run a small optimization to find the optimal line parameters. You can use profile_line to calculate the variance along the line (should be minimized with a good alignment), or otherwise sum the profile and see when it makes the sharpest peak.

This is a fun problem; I look forward to seeing what you come up with!

Thank you both for your insight! I’ve started playing around with all of your suggestions. So far min_distance and min_angle are really helping clean up the mess of lines, but it’s still not working as well as I’d like. I’ll keep working and report back!

I have the vague suspicion that there lies a paper in solving this problem well. You can imagine analyzing the hough histogram, and with the correct penalties and weighting figure out the best grid. You want to do some kind of optimization where you want to find peaks, but peaks have to be consistent with peaks at a 90° angle.