I am trying to extract a bitmask from a QPixmap and pass it to OpenCV. My bitmask is created by “painting” operations.
My process so far has been:
-
Create a
QPixmap,QPixmap::fill(QColor(0,0,0,0))and use aQPainterwithQPainter::setPen(QColor(255,0,0,255))toQPainter::drawPoint(mouse_event->pos() -
When ready to extract the bitmask
QPixmap::toImage()thenQImage::createAlphaMask(), which is documented to returnQImage::Format_MonoLSB
I am now officially stuck though. I’m having trouble deciphering the documentation:
Each pixel stored in a QImage is represented by an integer. The size of the integer varies depending on the format. QImage supports several image formats described by the Format enum.
Monochrome images are stored using 1-bit indexes into a color table with at most two colors. There are two different types of monochrome images: big endian (MSB first) or little endian (LSB first) bit order.
…
The createAlphaMask() function builds and returns a 1-bpp mask from the alpha buffer in this image…
Also:
QImage::Format_MonoLSB — 2 —The image is stored using 1-bit per pixel. Bytes are packed with the less significant bit (LSB) first.
Could anyone help me clarify how to transfer this into a cv::Mat.
Also, am I supposed to read this that each pixel will be an unsigned char or will we be storing 8 pixels in a bit.
I’ve successfully managed to transfer a monochrome QImage to a cv::Mat. I hope the following code is helpful to others:
IMPORTANT EDIT: There was a major bug with this code.
bytesPerLineis byte aligned as well as word aligned on some machines. Thus the width() should be used with cur_byte