我正在读一台给我Bayer16格式(GRGB)的相机,我在python中编写了以下代码,将它从bayer16修改为bayer8,然后使用OpenCV将其转换为RGB:
def _convert_GRGB_to_RGB(self, bayer16_image):
bayer8_image = bytearray()
# Convert bayer16 to bayer 8
for i in range(0, len(bayer16_image), 2):
data_byte = (bayer16_image[i] & 0xF0) >> 4
data_byte |= (bayer16_image[i+1] & 0x0F) << 4
bayer8_image.append(data_byte)
bayer8_image = numpy.frombuffer(bayer8_image, dtype=numpy.uint8).reshape((720, 1280))
# Use OpenCV to convert Bayer GRGB to RGB
return cv2.cvtColor(bayer8_image, cv2.COLOR_BayerGR2RGB)
在做了一些计时之后,for循环占用了大部分的运行时间并且效率极低(尽管我认为它不会分配任何空间,除非numpy为非常编辑制作副本)。我想知道如何整体改进这个功能,或者特别是for循环(因为它是这个功能中最慢的一部分)。
是否有人提供有关如何改进此Bayer16的提示和建议 - &gt; RGB转换,如果我要使用Python吗?
修改
我找到了一个使用numpy数组的解决方案,这使我的代码非常快:
def _convert_GRGB_to_RGB(self, data_bytes):
even = numpy.frombuffer(data_bytes[0::2], dtype=numpy.uint8)
odd = numpy.frombuffer(data_bytes[1::2], dtype=numpy.uint8)
# Convert bayer16 to bayer8
even = numpy.right_shift(even, 4)
odd = numpy.left_shift(odd, 4)
bayer8_image = numpy.bitwise_or(even, odd).reshape((720, 1280))
# Use OpenCV to convert Bayer GRGB to RGB
return cv2.cvtColor(bayer8_image, cv2.COLOR_BayerGR2RGB)
这个解决方案满足了我的需要,但如果有人有任何建议,我很想听到它们!
答案 0 :(得分:0)
你可以在你的numpyified代码中使用标准的python运算符,你也可以通过不切片data_bytes来获得加速(假设它是bytes
而不是一个numpy数组)
def _convert_GRGB_to_RGB(self, data_bytes):
data_bytes = numpy.frombuffer(data_bytes, dtype=numpy.uint8)
even = data_bytes[0::2]
odd = data_bytes[1::2]
# Convert bayer16 to bayer8
bayer8_image = (even >> 4) | (odd << 4)
bayer8_image = bayer8_image.reshape((720, 1280))
# Use OpenCV to convert Bayer GRGB to RGB
return cv2.cvtColor(bayer8_image, cv2.COLOR_BayerGR2RGB)
答案 1 :(得分:0)
作为猜测,您的颜色问题如下 - 您的GRBG
数据是这样的:
G0 B1 G2 ...
R0 G1 R2
数字表示uint16索引。 OpenCV需要编号
G0 B0 G1 R1 ...
R6 G6 R7 G7
你可以通过一些仔细的重塑和转置来解决这个问题:
data_bytes = np.frombuffer(data_bytes, dtype=np.uint8)
data = data.reshape(height / 2, width, 2) # a pair for each uint16
data = data.transpose((0, 2, 1)) #move the G/RB axis to be adjacent to the height axis
data = data.reshape(height, width) # collapse it
实施例
# manually constructed by hand
sample = ''.join([
'grbGgRbGgRbg'
'grBGGRBGGRbg'
'grBgGrBgGrbg'
])
width = height = 6
data = np.array(list(sample))
data = (data
.reshape(height / 2, width, 2)
.transpose((0, 2, 1))
.reshape(height, width)
)
# easy way to view the output
>>> data.view((np.str_,6))
array([['gbgbgb'],
['rGRGRg'],
['gBGBGb'],
['rGRGRg'],
['gBGBGb'],
['rgrgrg']],
dtype='<U6')