-2

I would like to write an algorithm in C++ that solves mazes. I would like to import an png or a bit image that contains only black and white pixels, my goal is to convert the image into a 2d array containing true and false values.

My main problem is importing of the image itself, something I have no idea how to do properly.

I'd be even glad to learn how to implement it by myself if this is a viable option. If the simplest solution is downloading a library, I'm happy to do so as well.

EDIT:

Ive yet to find a library which allows me to include a filename and somehow get a 2d array.

download openCV (aint sure it is the right library) as well as "LoadPNG" library

  • Does this answer your question? [Reading an image file in C/C++](https://stackoverflow.com/questions/2076475/reading-an-image-file-in-c-c) – yasht Nov 17 '19 at 06:13
  • You can use the OpenCV library also to read images and do manipulations with it in C++. https://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html – yasht Nov 17 '19 at 06:14
  • I recommend `stb_image` library. – HolyBlackCat Nov 17 '19 at 08:34
  • OpenCV and every other library that reads images will return a 2D array. If you think they don’t, then what is a 2D array for you? If your answer is `vector` you’re doing it very wrong! – Cris Luengo Nov 17 '19 at 15:03

1 Answers1

0

The answer is dependent on the image format you wish to use. If you are working with jpg or png there are some nice cpp header libraries that loads them into memory decoded for you into rgba pixel array (you can search for png header library cpp in google for this). If you don’t have a specific format you care about and you are the one creating the files then you can easily just write a decompressed bin file that contains 0s and 1s for true and false filled fields. And from there you can employ easy tricks to compress that data easily and efficiently.

Diniden
  • 1,005
  • 6
  • 14