使用单色QImage [英] Working With Monochrome QImage

查看:296
本文介绍了使用单色QImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从QPixmap中提取一个位掩码,并将其传递给OpenCV.我的位掩码是通过绘画"操作创建的.

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. 创建QPixmap, QPixmap::fill(QColor(0,0,0,0))并使用QPainterQPainter::setPen(QColor(255,0,0,255))QPainter::drawPoint(mouse_event->pos()

准备提取位掩码QPixmap::toImage()然后提取QImage::createAlphaMask(),该文件记录为返回QImage::Format_MonoLSB

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:

QImage中存储的每个像素都由一个整数表示.整数的大小取决于格式. QImage支持Format枚举描述的几种图像格式.

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.

使用1位索引将单色图像存储到最多具有两种颜色的颜色表中.单色图像有两种不同类型:大字节序(MSB优先)或小字节序(LSB优先).

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.

...

createAlphaMask()函数从该图像的alpha缓冲区中生成并返回一个1-bpp的蒙版...

The createAlphaMask() function builds and returns a 1-bpp mask from the alpha buffer in this image...

也:

QImage :: Format_MonoLSB--​​2 ---图像每像素使用1位存储.字节首先用最低有效位(LSB)打包.

QImage::Format_MonoLSB --- 2 ---The image is stored using 1-bit per pixel. Bytes are packed with the less significant bit (LSB) first.

任何人都可以帮助我阐明如何将其转移到cv :: Mat中.

Could anyone help me clarify how to transfer this into a cv::Mat.

另外,我应该读这篇文章吗,每个像素将是一个unsigned char,或者我们将一次存储8个像素.

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.

推荐答案

我已经成功地将单色QImage传输到cv :: Mat.我希望以下代码对其他人有帮助:

I've successfully managed to transfer a monochrome QImage to a cv::Mat. I hope the following code is helpful to others:

重要编辑:此代码存在一个主要错误.在某些机器上, bytesPerLine是字节对齐的,也是字对齐的.因此,width()应该与cur_byte

IMPORTANT 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
            }
        }
     }
 }

这篇关于使用单色QImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆