Binary image creation

How to create a binary image to the level of bits, not byte, exemple store 8 pixels in a byte ?

you could use PNG fileformat for doing such a task!
if you want to store 8 pixels in a byte in memory you could use array of bool because bool could only store 0 or 1, true or false, you could say true = white, false = black.
for example:

int main()
	std::vector<bool> binImage = { 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, .... };

what do you want to do with it ?

Image processing on binary image. But the image size is high that’s why I am trying to save memory. Thx

opencv’s datastructures / algorithms are using bytes, not bits here.

so why, again ?

OpenCV does not support that format in memory. One pixel always uses at least one byte.

Some file formats may support this packing. OpenCV uses libraries for that (libpng, …)

if you’re worried about memory consumption… perhaps look into using memory-mapped files. That’s not supported directly by OpenCV but you can make such a mapping and then wrap a cv::Mat around it.

That requires the file contents to be uncompressed.

I wouldn’t expect a std::vector<bool> to pack its values, even though it might. in any case, that’s no longer a 2D thing but an 1D thing.