2

I'm looking to create a binary image in OpenCV where all the values are initialised to 0. I want it to have dimensions 2000x800.

So far I have:

Mat canvas(2000, 800, 1)

But I want it so that the values of each pixel can only be a zero or a one.

Regards.

Squashy Josh
  • 41
  • 1
  • 7

2 Answers2

6

OpenCV does not support bitfields i.e. Mat's of depth 1 bit. There is sound reasoning in this; operating on bitfields is markedly slower because a bit is not an intrinsic operating type for the hardware. Getpixel and setpixel would therefore need extra operations.

Mat canvas = Mat::zeros(800,600,CV_8UC1);

Will create a zero-initialized image of depth 8 bits; 7 bits will be wasted.

Boyko Perfanov
  • 3,007
  • 18
  • 34
1

From its documentation, you can declare:

Mat canvas(2000, 800, 1, Scalar(0));

to initiate and fill the canvas with 0.

Ha Dang
  • 1,218
  • 1
  • 10
  • 12