Constantin Constantin - 1 month ago 12
C++ Question

Working With Monochrome QImage

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:


  1. Create a
    QPixmap,
    QPixmap::fill(QColor(0,0,0,0))
    and use a
    QPainter
    with
    QPainter::setPen(QColor(255,0,0,255))
    to
    QPainter::drawPoint(mouse_event->pos()

  2. When ready to extract the bitmask
    QPixmap::toImage()
    then
    QImage::createAlphaMask()
    , which is documented to return
    QImage::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.

Answer

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. bytesPerLine is byte aligned as well as word aligned on some machines. Thus the width() should be used with cur_byte

QImage mask; //Input from wherever
cv::Mat workspace;
if(!mask.isNull() && mask.depth() == 1)
{
    if(mask.width() != workspace.cols || mask.height() != workspace.rows)
        workspace.create(mask.height(), mask.width(), CV_8UC1);

    for(int i = 0; i < mask.height(); ++i)
    {
        unsigned char * cur_row = mask.scanLine(i);
        //for(int cur_byte = 0, j = 0; cur_byte < mask.bytesPerLine(); ++cur_byte) wrong
        for(int cur_byte = 0, j = 0; j < mask.width(); ++cur_byte)
        {
            unsigned char pixels = cur_row[cur_byte];
            for(int cur_bit = 0; cur_bit < 8; ++cur_bit, ++j)
            {
                if(pixels & 0x01) //Least Significant Bit
                    workspace.at<unsigned char>(i, j) = 0xff;
                else
                    workspace.at<unsigned char>(i, j) = 0x00;
                 pixels = pixels >> 1; //Least Significant Bit
            }
        }
     }
 }
Comments