Binary Image manipulation

Hi everybody!

This is my first post and I hope I write it in the correct category and appropiately.

I have a binary image (that I can export to any format) and I would like to measure the distance of every single black pixel (or white pixel) to a determined image point (like the origin (0,0) for example). But I have the following problems:

1) How should I tell to my C++ compilator (I am using Code::Blocks, but CodeLite or Geany should work as well) to take the binary image and convert it in an array of [X]x[Y] pixels where their values are just 1 and 0 depending if they are black or white? Is there any function or library that does it automaticaly?

2) Once the image ist converted to an array, how can I visualize it as a matrix or array of 1 and 0?

3) How can I output the result of the manipulated binary image (just in case that I would change something from it)?

If there is a good tutorial about how to work with this kind of date, it would be nice to know about it :)

Thank you very much in advance!
I've worked with images before, so I might be able to help. But first, could you explain the current representation of your image? For example, is it a 3 * X * Y array where each pixel is represented by three sequential elements for the R, G, and B channels? There are plenty of C++ libraries for image manipulation, but it shouldn't be difficult to do something like that manually. And when you say output - do you mean to the screen, or to a file?
Hi JellyFox,
thx for your answer.

So, the images are just binarized images. No color, or even grey scale, just black and white (1 or 0).

Exactly, when I say output I mean both, to screen it and to write it to a file.

Thank you again.
I don't think you need to physically convert the image into an array. It just needs to be treated as though it is one.

You can do this by defining a function which takes x and y values as input parameters, and then inside the body of the function, do whatever manipulations are required.

For example the image will have some sort of start address, and a specified number of bytes per line (that is, per y), and a specified number of bytes per pixel (per x value). So you can reach an individual pixel this way, and then do whatever you like (for example read or write the pixel value).

Hope this helps.
Thank you very much for your answer Chevril,

I thought that it was necessary to transform it as an array.

I think I can manage to use X and Y values as input parameters, but what I do not know how to do is to "read" the image pixel value.

Is there any easy example or tutorial about how to do it?

Thank you again for your help guys.
Well, it depends on the pixel format.

All you've said so far is "I have a binary image (that I can export to any format)".
Where does this image come from? Do you read it from a file (what is the image type of the file?). Or do you create the image in your program (how do you create it?).
Hi again Chevril,

yes, you are absolutely right, I am sorry about the lack of information.

The pictures are taken with a camera and stored as 8-bit greyscale images (each pixel has a value between 0-255) and are in *.bmp format.

I use a software ImageJ (or FIJI) to crop the image, modifying its size, and to binarize it, that means that if each pixel value is higher than a determined value (that I select in function of my convinience) it automaticly takes the new value 255 (white), and if it's the same or lower than this determined value, it automaticaly takes the new pixel value 0 (Black).

These binarised pictures are stored as *tif files, but I can transform them to any other image format, I do not know which one is better for C++ (png, jpg, bmp...).

The algorithm that I am supposed to write, should read the picture from the file, and then calculate the distance of every single (255) white pixel to the origin or to another determined position. The problem is that I do not how to do to read the pictures from the file.

I hope that I provided enough information and that it was clear, I am sorry about the lack of information before.

I really appreciate your patience and help.
I would just use boost for that. boost.GIL can read your tiffs, (also pngs and jpegs), and it has plenty of ways to iterate pixels and do calculations. What do you want to do with the distances? Output as text?
Hi Cubbi,

thank you for your answer as well.

Exact, I just want to know the distance of every each selected pixel (white in this case, or with 255 value) to a determined point of the picture.

The output should be a table with the number of pixels that are selected (i.e. the ones with value of 255), the position of it (X,Y) and the distance to the selected point (like for example the origin 0,0), but could be any other inside the picture).

It can't be difficult, the only problem I have is that I never managed C++ to read images, that's why I have no idea about what library or function I have to include or call.

It would be a nice help if I could read a tutorial in a book or in the net with some examples, but I can't find it.

Thank you very much again!

PS: I am not suggesting at all to solve the whole problem, just how to manage with the input/output and reading of the image :)
Actually, I'd agree that in general, using some sort of library (boost or otherwise) is the simplest approach.

If however you did choose to do the whole thing using explicit code, the one thing I'd recommend is to make sure the input image file is uncompressed. That way, once you get past the header and into the pixel data, things are relatively straightforward. But really, I wouldn't recommend doing things the hard way (not using a library) unless you are very interested in studying the innards of image files.
Cool!

Thank you very much.

I will try to manage with that library for the moment.

I have already seen that they have their own tutorials.

Do you know other libraries that use images as input/output variables?

Topic archived. No new replies allowed.