简体   繁体   中英

Find Bayer pattern format from a byte file

I am struggling a bit with a problem, I was wondering if someone with more experience might notice what am I doing wrong:

I have a binary file of 6,266,880 bytes that contain an image saved with an unknown Bayer pattern .

About the image I know that it's format is 2176x1920 pixels, and that it has a bit_per_pixel = 12.

I would like to discover which one is the Bayer format used to save the image .
I thought to convert it with cv2.cvtColor(src, cv2.COLOR_BayerGR2BGR) using all the conversion options provided by the cv2 library for Bayer input, which are:

  cv::COLOR_BayerBG2BGR
  cv::COLOR_BayerGB2BGR 
  cv::COLOR_BayerRG2BGR
  cv::COLOR_BayerGR2BGR 

Until I would have found the one that provide as output a "clean" image.

However, I am always getting something dirty like this:
cv2转换后的img

Here's the code that I am using:

import numpy as np
import matplotlib.pyplot as plt
import cv2

pixels = np.fromfile("0000.raw", dtype = 'uint8')

""" CONVERT THE BYTE STREAM, EVERY PIXEL HAS 12 BIT, SO BYTE HAS TO BE SPLITTED AND PUTTED IN A UINT16 VARIABLE"""
data = pixels
data1 = data.astype(np.uint16)
data1[::3] = data1[::3]*256 + data1[1::3] // 16
data1[1::3] = (data[1::3] & 0x0f)*16 + data[2::3]
result = np.ravel(data1.reshape(-1,3)[:,:2]) 


img = result.reshape(2176, 1920)
convertedImage = cv2.demosaicing(img_scaled, cv2.COLOR_BayerGR2BGR) 
cv2.imshow("tmp", convertedImage) 
cv2.waitKey(0) 

Also, Here there are 10 samples of the same image saved as raw file, and for each of them a json with their properties

Any idea on what else to try to convert it? Or is there some other approach to find the Bayer format?

The 12 bits are packed: Every 3 bytes applies packed 2 (12 bits) pixels.
I managed to unpack the pixels by trial and error.

Here is the code:

import numpy as np
import cv2

cols, rows = 1920, 2176

pixels = np.fromfile("0000.raw", np.uint8)

""" CONVERT THE BYTE STREAM, EVERY PIXEL HAS 12 BIT, SO BYTE HAS TO BE SPLITTED AND PUTTED IN A UINT16 VARIABLE"""
data = pixels
data1 = data.astype(np.uint16)

result = np.zeros(data.size*2//3, np.uint16)

# 12 bits packing: ######## ######## ########
#                  | 8bits| | 4 | 4  |  8   |
#                  |  lsb | |msb|lsb |  msb |
#                  <-----------><----------->
#                     12 bits       12 bits

result[0::2] = ((data1[1::3] & 15) << 8) | data1[0::3]
result[1::2] = (data1[1::3] >> 4) | (data1[2::3] << 4)
bayer_im = np.reshape(result, (rows, cols))

bgr = cv2.cvtColor(bayer_im, cv2.COLOR_BayerBG2BGR)
cv2.imshow('bgr', bgr*16)

# "White balance":
bgr[:, :, 0] = np.minimum(bgr[:, :, 0].astype(np.float32)*1.8, 4095).astype(np.uint16)
bgr[:, :, 2] = np.minimum(bgr[:, :, 2].astype(np.float32)*1.67, 4095).astype(np.uint16)

cv2.imshow('bayer_im', bayer_im*16)
cv2.imshow('bgr WB', bgr*16)
cv2.waitKey()
cv2.destroyAllWindows()

  • The cv2.COLOR_BayerBG2BGR gives the best result.
  • I amplified the blue and the red channels (simple "White Balance").
  • The image is composed of two images - the top with high exposure, and the bottom with low exposure. The purpose it to get an HDR frame by combining the two images.
    Producing an HDR image exceeds the scope of my answer.

Result:
在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM