python - 将十六进制值的行转换为二进制,按列垂直

标签 python c++ optimization numpy bit-manipulation

我正在处理来自串行设备的数据,该设备以一种非常有趣的格式输出数据。该设备有一个 256x256 像素阵列,而每个像素都有一个 14 位值,用移位寄存器读出。

为了显示格式,我将以每个像素都有一个 6 位 值时的样子来说明它:

                             'Pixel #'
        0-8   9-16   17-24  25-32  33-40  41-48  48-56  57-64 ... 256
        --------------------------------------------------------------
    0   255    255    255    255    255    255    255    255  ...
    1   127    255    255    255    255    255    255    255  ...
    2   255    255    255    255    255    255    255    255  ...
    3   255    255    255    255    255    255    255    255  ...
    4   255    255    255    255    255    255    255    255  ...
    5   255    255    255    255    255    255    255    255  ...

    Note that the 2nd row starts with a value of 127

To get the 6-bit value of the 1st pixel (pixel # 0), the following must occur:

  1. Every value in row needs to be treated as its binary/bit equivalent
  2. Reading vertically, the aligned bits from each row, 6 rows down (for a 6-bit output value), results in the value of that pixel

That is:

                             'Pixel #'
          0-8     9-16     17-24    25-32  ...  256
        --------------------------------------------------------------
    0   *1*1111111 11111111 11111111 11111111 ...
    1   *0*1111111 11111111 11111111 11111111 ...
    2   *1*1111111 11111111 11111111 11111111 ...
    3   *1*1111111 11111111 11111111 11111111 ...
    4   *1*1111111 11111111 11111111 11111111 ...
    5   *1*1111111 11111111 11111111 11111111 ...

    Note that the 2nd row had a value of 127, which is 01111111 in binary
    --> Pixel 0 = 101111 = 47

Now, repeat that across all 256 columns, then move down to the next 6 rows and repeat.

The actual output needs to be an array of pixel values that is 256x256. The actual dataset I need to process is 14-bits for each pixel - it's 3584x32 (14-bits * 256 pixels = 3584 rows ... and 32 bytes * 8 bits/byte = 32 bytes across).

What would be the best way to process the dataset? Also, speed is of critical concern, so are there some high-speed functions that could be leveraged?

Appreciate the help - Thanks!


EDIT:

To answer the questions about the required speed - Ideally, I would like to perform this at least 10x/sec, as the data is coming in at 60x/sec. Therefore, I think I'd need to avoid the common 'joins' and string operations, as I believe those to be quite slow.

Again, the real dataset (3584x32) has 14-bits for each pixel, so it's 3584x32.

Here's the function I am working on, using Joran's method, which takes ~2.6 secs to execute when provided with the real dataset:

def GetFrame(RawData):
    if np.shape(RawData) == (3584, 32):
        ProcessedData = np.zeros((256, 256), dtype='int16')
        data_blocks = [RawData[d:d+14] for d in range(0, 3584, 14)]
        for p in range(256):
            data_bin_rows = ["".join(map(lambda val:"{0:08b}".format(val,), row)) for row in data_blocks[p]]
            ProcessedData[p][:] = [int("".join(v),2) for v in zip(*data_bin_rows)]
        return ProcessedData
    else:
        return False

如何才能更快地缩短执行时间?谢谢!

最佳答案

我不得不读了几遍,但我想我明白了

data = \
"""255    255    255    255    255    255    255    255
127    255    255    255    255    255    255    255
255    255    255    255    255    255    255    255
255    255    255    255    255    255    255    255
255    255    255    255    255    255    255    255
255    255    255    255    255    255    255    255"""

data_rows = [map(int,row.split()) for row in data.splitlines()]
data_bin_rows = ["".join(map(lambda val:"{0:08b}".format(val,),row)) for row in data_rows]
pixel_values = zip(*data_bin_rows)
print pixel_values[0],"=",int("".join(pixel_values[0]),2) #pixel0

不能说它的速度......但如果你不是每秒做一百万次它可能是合理的......无论如何应该比串行读取快得多......

关于python - 将十六进制值的行转换为二进制,按列垂直,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23479288/

相关文章:

optimization - 如何优化基于 cakephp 和 SQL 的 Web 应用程序?

python - 删除重复的 2D numpy.array

c++ - 柠檬 + re2c 没有得到正确的规则解析

c++ - 在 Arduino IDE 中使用正则表达式库时未定义对 `longjmp' 的引用

c++ - 在Qt中释放文件锁

python - OpenMDAO 的 SimpleGADriver 中自动计算的具有整数值的位

php - PHP 函数调用开销有多重要?

python - Pandas 数据框到没有索引的json

python - 访问在 sqlalchemy 中没有 key 的 json

python - 在 Python 中安装包时 pip install tr​​aceback 错误