i reading camera gives me bayer16 format (grgb) , wrote following code in python modify bayer16 bayer8, , use opencv convert rgb:
def _convert_grgb_to_rgb(self, bayer16_image): bayer8_image = bytearray() # convert bayer16 bayer 8 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 convert bayer grgb rgb return cv2.cvtcolor(bayer8_image, cv2.color_bayergr2rgb)
after doing timing, loop takes of running time , extremely inefficient (although think not allocate space, unless numpy makes copy edit). wondering how improve function whole, or loop in particular (as slowest part of function, order of magnitude).
does have tips , advice how improve bayer16 -> rgb conversion if use python please?
edit:
i found solution using numpy array makes code pretty fast:
def _convert_grgb_to_rgb(self, data_bytes): = numpy.frombuffer(data_bytes[0::2], dtype=numpy.uint8) odd = numpy.frombuffer(data_bytes[1::2], dtype=numpy.uint8) # convert bayer16 bayer8 = numpy.right_shift(even, 4) odd = numpy.left_shift(odd, 4) bayer8_image = numpy.bitwise_or(even, odd).reshape((720, 1280)) # use opencv convert bayer grgb rgb return cv2.cvtcolor(bayer8_image, cv2.color_bayergr2rgb)
this solution satisfies need if has suggestion, i'm curious hear them!
your can use standard python operators in numpyified code, you'll speedup not slicing data_bytes (assuming it's bytes
, not numpy array)
def _convert_grgb_to_rgb(self, data_bytes): data_bytes = numpy.frombuffer(data_bytes, dtype=numpy.uint8) = data_bytes[0::2] odd = data_bytes[1::2] # convert bayer16 bayer8 bayer8_image = (even >> 4) | (odd << 4) bayer8_image = bayer8_image.reshape((720, 1280)) # use opencv convert bayer grgb rgb return cv2.cvtcolor(bayer8_image, cv2.color_bayergr2rgb)
Comments
Post a Comment