Find the size of maze corridors in a image


So I am building a application to solve mazes one of the options is upload a picture and it will solve it. However upon solving the maze the output will look like this.

maze

I would like to figure out how to make my program find the proper corridor size and have the solution look like this with the pathway completely full

enter image description here

My data is put into a array with 1's representing the walls and 0's the spaces like this. So far I have thought about trying to find the smallest distance between 1's but that runs into problems with circular mazes and writing on the maze. I have thought about filling the distance between the walls but that runs into problems at intersections.

I am drawing on the image using

image.setRGB(x, y, Color.RED.getRGB());

with the image being a BufferedImage.

I am truly all out of ideas and don't know how to come at this problem any help would be appreciated.


Answers:


Each square in your grid has a certain size. Say wsq * hsq for "width of square times height of square".

Given your much more fine-grained (x, y), you can find in which square it is by dividing x by wsq and y by wsh:

int xsq = x / wsq;
int ysq = y / ysq;

The area to paint red would be from (xsq * wsq, ysq * hsq) and have width/height (wsq, hsq). and you could paint that red, but it would mean that you paint over the walls. So you have to adjust the area you're going to fill with red color by the size of the walls. If the walls are all two pixels thick, you need to add 1 to the x and the y coordinate of the square, and substract 2 from the widht and the height.

And you could fill it again (with a Graphics2D) for every time that you are now calling image.setRGB or you could remember which squares that you already filled.

Note: since you are working with regular-sized squares, you can also optimize your maze-solving algorithm to work in a grid of squares of size (wsq, hsq) rather than the individual pixels in the image.