4
$\begingroup$

See the picture. I'm trying to process this map drawing to be in a workable state where the drawing is black and everything else is white for further processing, but I drew the map on grid paper before deciding to do all that.

enter image description here

The lines are lightly blue, so I expected I could exploit that to remove it. So far I tried the following:

  • Picking a blue pixel belonging to the grid and remove (turn white) all pixels whose color is within some distance (euclidean, from LAB color space) of that pixel. -> This doesn't work because the colors on the grid change too much throughout the drawing: depending on which grid pixel I pick and the tolerance I use, some of the drawing gets picked up, or part of the grid remains.
  • Doing the same thing, but making it dynamic and making comparisons based on neighbouring pixels that have been "recognized" as part of the grid. This was attempting to counter subtle gradual changes in lighting. This doesn't work either, and somehow it ends up picking the entire white part of the drawing (not an issue because that should also go white, but still evidence that the idea as I implemented it doesn't follow my intuition). Depending on tolerance, here again some grid remain or part of the drawing goes away. It also fails to capture parts of the grid that are disconnected from the rest by the pencil drawing forming closed loops that don't get pierced through.
  • A few other unsuccessful variations like instead of picking a single "initial" pixel, picking all pixels within some delta of that one pixel and then using them as starting points for doing the dynamic approach.

I can keep throwing random ideas at the wall but I'm kinda stumped and every attempt now somehow seems to get me further away. I'm interested in managing to do it algorithmically such that I could easily apply the same algorithms to process another drawing the same way. Otherwise I could just take the loss and manually process it and remove the grid from a black and white mask after some thresholding.

Note that I'm an outsider to this, so I'm not familiar with advanced techniques; if you suggest something, I'll look it up to the best of my abilities, but I might be stumbling around a bit. I'm working on Python so far, with OpenCV.

$\endgroup$
3
  • 3
    $\begingroup$ hey, how realistic would the approach "start by getting a flatbed scanner's picture of this page rather than a camera's picture" be to you? I ask that because afterwards, all the blue lines would run in exactly two directions and you might be able to remove them based on that. $\endgroup$ Commented Apr 12 at 10:18
  • $\begingroup$ I haven't done these things in nearly 30 years so no answer here but for a more DSP approach I would try a notch filter in the frequency domain, possibly first transform the colorspace so the grid is a primary component. Maybe some wizard is awake and can try it! $\endgroup$ Commented Apr 12 at 18:49
  • 1
    $\begingroup$ @MarcusMüller it's pretty realistic, but the idea is to be able to do it on a whim all while at home without having to leave to get it scanned. Otherwise I might as well just do some thresholding and manually clean it in Paint. TimWescott's mention of using a blue transparent sheet does make scanning sound more attractive though. $\endgroup$ Commented Apr 14 at 5:27

4 Answers 4

7
$\begingroup$

This is possibly one case where a generalist tool can have useful results; in this case by processing the image with a visual language model (It's not "DSP" in the usual sense though, even though in the end it's just math)

Map with paper grid removed

This was "Nano Banana Pro" prompted with "Create a white background, flat, outline, no grid lines map. The map should be EXACTLY the same as what is drawn, just removing the grid lines and straightening.". This can be easily automated, Google offers an API to do it programmatically. It does have some disadvantages:

  • I would not trust it with exact dimensional accuracy
  • I would not trust it with accuracy in general, this map might not be the same as what you drew
$\endgroup$
1
  • $\begingroup$ This is looking neat, but yeah it does loses a bit of sharpness you can notice when zooming in on tiny islands on the drawing and comparing. Ideally I'd be able to do it offline and be able to reproduce the process myself, but then this is more me being picky and begging and trying to choose at the same time. $\endgroup$ Commented Apr 14 at 5:47
6
$\begingroup$

Back in the old days, that was a specific color of blue, and you could slap a blue transparent sheet on your copier (black and white - color was science fiction) and get a copy of just the lines. It works great with enough contrast (inked lines rather than pencil).

You can do this now by zeroing out the blue and just keeping red and green, then taking the magnitude of that. Expect ghost lines.

Expect skips and other nastyness if you used pencil. Sometimes you can work this out, sometimes you need to ink in the lines and take another picture. There's a technique where you can fix skips by swelling all the lines until the skips are bridged then eroding them back to a single pixel, but fixing things by hand is easier with a one-off.

To rid yourself of ghost lines, invert the image to white lines on "black", run a difference of Gaussians on it, threshold, and (if you want) invert it again.

I do this all the time, when I need to put a hand drawn sketch into a document. But the operations always need fiddling, and they're all available interactively in Gimp, so I just use that.

$\endgroup$
2
  • $\begingroup$ I've tried a little bit with python, without much success so far: zeroed the blue value, then making a black and white mask based on the norm of the resulting rgb vector. Either takes way too much, or too little, but no "almost there" sweet spot as different parts of the drawing react differently to each threshold value. It'd maybe work better with a different function to take into acount that the grid lines also seem to have more green. I'll try again to fiddle around with that. Might be looking into doing that in Gimp, as it would probably be faster to try and repeat. $\endgroup$ Commented Apr 14 at 5:41
  • $\begingroup$ Different parts of the drawing are reacting differently to each threshold value because you missed the part about doing edge enhancement with a difference of gaussians. The way that you took that picture leaves it shaded, you need to fix that with either a new picture - like Marcus's suggestion to use a flat scanner - or you need to fix the shading before you threshold. $\endgroup$ Commented Apr 14 at 14:59
1
$\begingroup$

This struck me as an interesting problem, but I'm not an image processing expert so I consulted Google Gemini. Turns out I got some rather promising results in python with OpenCV.

The AI suggested working in HSV space rather than RGB because it better decouples color information from brightness, making it more robust to variations in color and lighting in real-world photos. Once in HSV space, apply thresholds to detect a range of blue grid colors and use to generate a mask. Then apply OpenCV's inpaint() method to fill the masked gridlines with the surrounding background color.

Using a code snippet directly from the AI I got this result, which looks pretty good for no parameter tuning. It seems like this approach has potential and can be tuned/refined for a more optimal result. As you can see, some of the grids are still clearly visible, especially in areas further away from the map.

enter image description here

$\endgroup$
1
  • $\begingroup$ Honestly didn't think of using hue to pick, although I'm worried that the pencil drawing being black/greyish, the hue might end up being a bit "too random" not to be picked up? But then maybe I can work something out with the other values available. But with a quick look at your result, it seems that the drawing does only get cut on the grid at least. $\endgroup$ Commented Apr 14 at 5:52
0
$\begingroup$

Since the grid is very regular (if you scan it with a flatbed), some sort of 2D filtering might remove it. But I don't have any experience with 2D FFTs to know if that would work.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.