I wrote code in Python programming language. This code should first open the raw file and output it as an image. Next, you need to split the pixels into RGB values and calculate the 3x3 matrix for color correction. But before that, you need to do a black level correction. The picture shows the approximate operation of the algorithm. Raw file is 14 bit and has RG pattern. Link to raw file: text enter image description here The code:
import numpy as np
from PIL import Image
def open_raw_file(raw_file_path, width, height):
with open(raw_file_path, 'rb') as file:
raw_data = file.read()
# Assuming raw data is in RGB format, each pixel consists of 3 bytes (R, G, B)
# Adjust the data format based on your specific raw file format if necessary
image_data = np.frombuffer(raw_data, dtype=np.uint8)
image_data = image_data.reshape((height, width, 3))
return image_data
def apply_black_level_correction(image_data, black_level):
corrected_image_data = np.maximum(image_data - black_level, 0)
return corrected_image_data
def split_into_channels(image_data):
red_channel = image_data[:, :, 0]
green_channel = image_data[:, :, 1]
blue_channel = image_data[:, :, 2]
return red_channel, green_channel, blue_channel
def calculate_color_correction_matrix(image_data):
# Assuming a 3x3 color correction matrix is required
# Calculate the matrix based on your specific algorithm or requirements
color_correction_matrix = np.eye(3) # Identity matrix for demonstration purposes
return color_correction_matrix
def save_image(image_data, output_path):
image = Image.fromarray(image_data)
image.save(output_path)
# Main code
raw_file_path = 'macbeth_2592x1944_RG.raw'
output_image_path = 'output_image13.jpg'
width = 640 # Adjust as per your raw file dimensions
height = 480 # Adjust as per your raw file dimensions
black_level = 20 # Adjust the black level value as per your requirement
# Open raw file and convert it into an image
image_data = open_raw_file(raw_file_path, width, height)
# Apply black level correction
corrected_image_data = apply_black_level_correction(image_data, black_level)
# Split into RGB channels
red_channel, green_channel, blue_channel = split_into_channels(corrected_image_data)
# Calculate color correction matrix
color_correction_matrix = calculate_color_correction_matrix(corrected_image_data)
# Perform color correction
red_channel_corrected = np.dot(red_channel, color_correction_matrix[0])
green_channel_corrected = np.dot(green_channel, color_correction_matrix[1])
blue_channel_corrected = np.dot(blue_channel, color_correction_matrix[2])
# Merge color channels back into image
output_image_data = np.stack((red_channel_corrected, green_channel_corrected, blue_channel_corrected), axis=2)
# Save the corrected image
save_image(output_image_data, output_image_path)
I got this error:
Traceback (most recent call last):
File "C:\Users\DC\PycharmProjects\pythonProject4\lab13.py", line 43, in <module>
image_data = open_raw_file(raw_file_path, width, height)
File "C:\Users\DC\PycharmProjects\pythonProject4\lab13.py", line 11, in open_raw_file
image_data = image_data.reshape((height, width, 3))
ValueError: cannot reshape array of size 5038848 into shape (480,640,3)
So i try to run this code with:
- As I said raw file is 14 bit, but I run it with 8 bit (didnt help).
- I tried with size: w:640, h:480 and w:2592, h:1944 (original raw size) (Didnt help).
- I tried to change reshape function changing image_data = image_data.reshape((height, width, 3)) to image_data = image_data.reshape(height, width). None of that helped me.