3

I am trying to create a 1bpp image (1 bit per pixel) mask in C++ for a project, however I am stuck on how I can do this with OpenCV?

The code I am trying with is:

int WIDTH=640; int HEIGHT=480;
Mat mask = Mat::zeros( cv::Size( WIDTH, HEIGHT ), CV_8UC1 );
mask( Rect( 50,50,100,100 ) ) = 1;

I am not sure if this is the right way to create a 1 bpp mask though. Does anyone know if this is possible using OpenCV or in C++ at all?

jscs
  • 63,694
  • 13
  • 151
  • 195
Jaysinh Sagar
  • 33
  • 1
  • 4
  • `CV_8UC1` is used to create a single channel, 8 bit per pixel (unsigned char) image. – karlphillip Jan 16 '15 at 14:34
  • 1
    so is there any way to have 1 bit per pixel? – Jaysinh Sagar Jan 16 '15 at 14:59
  • 4
    There's no native data type for 1bpp. But you can use `CV_8UC1` as a black/white image: store value 0 for a black pixel, and 255 to represent a white pixel. It's how everybody does it. – karlphillip Jan 16 '15 at 15:04
  • I understand this and this is what I do in the example above but this mask does not work well with the library I am trying to use the mask for unless it is 1bpp mask. Thanks though! – Jaysinh Sagar Jan 16 '15 at 15:27
  • use `CV_8UC1` and use 1/8 of your wanted pixels, so each element in the cv::Mat is 8 elements in your 1 bit mask. You have to write some functions to access each single bit, but probably your library already provides them. – Micka Jan 16 '15 at 15:50
  • 3
    Is there a language that has a 1-bit datatype? – a-Jays Jan 17 '15 at 11:04
  • @a-Jays bitfields in C and C++ – phuclv Feb 03 '15 at 10:08
  • It seems OpenCV doesn't support 1-bit format. Personally I use ImageMagick to convert: `convert "grayscale.png" -depth 1 -monochrome "bitmap.xpm"`. I prefer to use xpm for text processing . – Lenik Jun 17 '16 at 13:31

0 Answers0