由列转换十六进制值的行为二进制,垂直 [英] Convert rows of hexadecimal values to binary, vertically by column

查看:122
本文介绍了由列转换十六进制值的行为二进制,垂直的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据,用于输出一个非常有趣的格式数据的串行设备来工作。该装置具有256×256像素阵列,而每个像素具有的 14位值,读出用的移位寄存器。

I am working with data coming from a serial device which outputs its data in a very interesting format. The device has a 256x256 array of pixels, whereas each pixel has a 14-bit value, read-out with a shift register.

要显示的格式,我将举例说明,因为它会看,如果每个像素有一个 6位值:

To show the format, I will illustrate it as it would look if each pixel had a 6-bit value:


                             '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

要获得 6位第一像素值(像素#0),必须出现以下情况:

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


  1. 在每行的值需要被视为其二进制/位等效

  2. 垂直阅读,从每一行,6行向下对准的位(为一个的 6位输出值),结果在该象素的值

  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

这就是:


                             '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

现在,重复所有256列,然后移动到下一个6行和重复。

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

实际输出需要的像素值是256×256的阵列。实际数据集我需要过程是为每个像素14位 - 它3584x32(14位* 256像素= 3584行...和32个字节* 8比特/字节横跨= 32字节)

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?

鸭preciate帮助 - !谢谢

Appreciate the help - Thanks!

结果
修改

要回答有关所需的速度问题 - 理想的情况下,我想执行此至少10倍/秒,因为数据是在60倍/秒到来英寸因此,我认为我会需要避免常见的联接和字符串操作,因为我相信这是相当缓慢的。

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.

再次实数据集(3584x32)具有用于每个像素14位,所以它的3584x32

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

下面是一个使用Joran的方法,该方法〜2.6秒时执行与真实数据集提供我工作的功能,

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

那怎么可以做出更快的得到执行时间下来?谢谢!

How can that be made faster to get the execution time down? Thanks!

推荐答案

我不得不读了几次,但我认为伊夫得到它

I had to read it a few times but I think Ive got it

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

着它的速度说话......但它可能是合理的,如果你不这样做像一百万次第二...应该比anycase ...

cant speak to its speed ... but its probably reasonable if your not doing it like a million times a second ... should be much faster than the serial read in anycase ...

这篇关于由列转换十六进制值的行为二进制,垂直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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