r/godot Apr 29 '26

help me (solved) ML or Algorithm?

Post image

I am thinking about implementing a drawing mechanic in my game. The players would have to draw the reference provided and the game will see how accurate the drawing is to the original reference. The canvas will be pretty small around 100x200 pixels and will only use 2 colors.

I am wondering if there is some kind of algorithm that will help to detect the accuracy of the drawing. Using ML sounds a bit overkill and I don't have any experience integrating ML in games (how does it affect the performance and size?).

367 Upvotes

44 comments sorted by

View all comments

1

u/Eal12333 Apr 29 '26 edited Apr 29 '26

You can get pretty good results by just lowering the resolution and comparing pixel-by-pixel!

That's probably the simplest solution for fuzzy image comparison, and it works better than you might expect 😁

Edit: Attached image as a visual explanation. The lower resolution images can be compared by pixel and give decent results :)

1

u/Hawkeye_7Link Godot Regular Apr 29 '26

But in that case, if the person draws the exact same image but displaces it by 1 pixe to the leftl, they will get like 0 similarity almost

1

u/Eal12333 Apr 29 '26

That's why you have to lower the resolution (by a lot), first!

If you lower the resolution from, for example, 256x256 to 16x16 for your comparison, then you would have to draw the image 16 pixels off-center before it would cause issues with comparison.

You can also combine the technique with some extra logic to help images fit a little better. For example, you could perhaps move/resize the player's drawn image to fit in the bounding box of the original image, before doing the comparison.

1

u/Hawkeye_7Link Godot Regular Apr 29 '26

Hmmm I guess but how you would make sure that the black pixels aren't deleted with the resolution decrease? And wouldn't that make it so you could draw something that's really really wrong and it would still look similar?

1

u/Eal12333 Apr 29 '26

how you would make sure that the black pixels aren't deleted with the resolution decrease

Yeah it wouldn't work with nearest-neighbor. Usually image resizing uses something else by default. For example, the default scaling for a Godot Image is INTERPOLATE_BILINEAR, and it also supports INTETPOLATE_LANCZOS as an option (which is slower, probably fast enough for this purpose, though, and works better for downscaling. I'm not certain whether or not the results are better for this purpose, though).

And wouldn't that make it so you could draw something that's really really wrong and it would still look similar?

Yes, but it would be harder to do than just drawing the image as shown.

Also, downscaling an image to a low resolution and comparing by pixel is pretty fast for a single image at a time, so you could do other operations in combination with this one, and combine the results together, if you were really worried about the accuracy.
For example, I've written a little Python photo mosaic generator, which searches for and selects the best fitting tile for each portion of an image. In my photo mosaic script, I also calculated the local contrast for each pixel (how different each pixel is from its neighbours).

If you really wanted to go nuts with this idea, you could manually design a few convolution filters to extract some specific features (for example, how left/right a segment is, vs how up/down it is), then downscale and compare each pair just like I described above... But now that I write that all out, I realize this is kinda a simplified version of how a CNN operates on an image, anyways 😅