Python:读取12位压缩的二进制图像 [英] Python: reading 12 bit packed binary image

查看:228
本文介绍了Python:读取12位压缩的二进制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自GigE相机的12位压缩图像.这是一个小端文件,每个3字节保存2个12位像素. 我正在尝试使用python读取此图像,并且尝试了以下操作:

I have a 12 bit packed image from a GigE camera. It is a little-endian file and each 3 bytes hold 2 12-bit pixels. I am trying to read this image using python and I tried something like this:

import bitstring
import numpy

with open('12bitpacked1.bin', 'rb') as f:
    data = f.read()
ii=numpy.zeros(2*len(data)/3)
ic = 0

for oo in range(0,len(data)/3):
    aa = bitstring.Bits(bytes=data[oo:oo+3], length=24)
    ii[ic],ii[ic+1] = aa.unpack('uint:12,uint:12')
    ic=ic+2

b = numpy.reshape(ii,(484,644))

简而言之:我读取了3个字节,将它们转换为位,然后将它们解压缩为两个12位整数.

In short: I read 3 bytes, convert them to bits and then unpack them as two 12-bit integers.

但是,结果与应有的结果有很大不同.看起来图像被分为四个部分,每个部分都扩大到完整的图像大小,然后重叠.

The result is, however, very different from what it should be. It looks like the image is separated into four quarters, each of them expanded to full image size and then overlapped.

我在做什么错了?

更新:以下是测试文件:

12位压缩

12位普通

它们不会完全相同,但是它们应该显示相同的图像. 12位法线的uint16像素为12位.

They will not be identical, but they should show the same image. 12-bit normal has 12-bit pixel as uint16.

with open('12bit1.bin', 'rb') as f:
    a = numpy.fromfile(f, dtype=numpy.uint16)

b = numpy.reshape(a,(484,644))

推荐答案

使用此代码

for oo in range(0,len(data)/3):
  aa = bitstring.Bits(bytes=data[oo:oo+3], length=24)

您正在读取字节data[0:3]data[1:4],...,您真正想要的可能是:

you are reading bytes data[0:3], data[1:4], ... What you actually want is probably this:

for oo in range(0,len(data)/3):
  aa = bitstring.Bits(bytes=data[3*oo:3*oo+3], length=24)

更加紧凑的是:

for oo in range(0,len(data)-2,3):
  aa = bitstring.Bits(bytes=data[oo:oo+3], length=24)

这篇关于Python:读取12位压缩的二进制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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