Qimage到2D二进制数组

问题描述 投票:1回答:1

我正在研究一种机制,该机制将允许我从QImage :: FormatMono标志创建的QImage中获取基于constBits()的二进制数据填充的二维数组。

我希望正方形应该是这样的:

1111
1001
1001
1111

但是我却得到这个:

0001
0010
0100
1000

我不太想象如何一点一点地使用内存或如何使用MSB压缩的字符串。

这是我用来获取1像素值并将其表示为二进制的代码:

uint ConnectedChecker::pixel(const QImage& img, const int x, const int y) const
{
    const uchar mask = 0x80 >> (x % 8);
    return img.constBits()[x*y / 8] & mask ? 1 : 0;
}

和循环,我用它来填充数组:

int* _in;

for(uint i = 0; i < _rowCount; ++i) {
   for(uint j = 0; j < _columnCount; ++j) {
       *((_in + i*_columnCount) + j) = pixel(image, i, j);
   }
}
arrays qt bit-manipulation bit-shift qimage
1个回答
0
投票

公式不正确,您需要类似的东西

int a = y*img.bytesPerLine();
return (img.constBits()[a + x/8] >> (x & 7)) & 1;

bytesPerLine()成员函数需要考虑可能在每条扫描线末端添加的QImage填充。

旁注:在标识符的开头使用下划线_是个坏主意(例如,它可能导致全局标识符或大写名称出现技术问题)。这也很丑陋。你为什么要这样做?

© www.soinside.com 2019 - 2024. All rights reserved.