2

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:
Img after cv2 conversion

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?

Rotem
  • 30,366
  • 4
  • 32
  • 65

1 Answers1

3

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:
enter image description here

Rotem
  • 30,366
  • 4
  • 32
  • 65
  • WOW! This is great! Thanks a lot. I got few following questions if you have time Could you explain why in the `imshow` you multiply the image by 16? And also, Is the white balancing something needed when you convert from bayern patterns? And last question, which parameter do you modify in your script to change the exposure of the image? – XxcoralloxX Jun 21 '21 at 08:07
  • 1
    `cv2.imshow` does not support display 12 bits range correctly (when range is [0, 4095]). When the type is `np.uint8` the range assumed to be [0, 255] (255 is white). When the type is `np.uint16`, the range assumed to be [0, 65535] (65535 is white). We want 4095 to be white, so we need to multiply by (about) 16. Without it the displayed image is going to be very dark. – Rotem Jun 21 '21 at 09:01
  • 1
    I used simple white balance just to get a nicer result (without it the image is greenish). White balance and Demosaicing are not directly coupled. They are two different stages of raw image processing. Tuning the brightness and contrast is also a stage of raw image processing. I am sorry, but I am not going to teach you all the stages of raw image processing. – Rotem Jun 21 '21 at 09:09
  • That's already a lot of info! Thanks again – XxcoralloxX Jun 21 '21 at 09:19