python opencv 查看MIPI RAW10图像

MIPI RAW10

紧凑型存储格式RAW

根据 CSI-2 协议,RAW10数据packed后数据流如下图
在这里插入图片描述
在图像处理中,通常需要将紧凑型转换为非紧凑型。

非紧凑型存储格式RAW

非紧凑型是指将sensor输出的raw10数据按16bit存储。对于raw10数据在内存中的存储排列方式,以4字节的内存片段为例,数据的存储方式如下所示:
在这里插入图片描述

代码实现

import cv2
import numpy as np

def bit10_2_bit16(list_10_bytes):
    list_10 = []
    for i in range(len(list_10_bytes)):
        list_10.append(list_10_bytes[i])
    list_16 = []
    for i in range(int(len(list_10) / 5)):
        index_10 = 5 * i
        for j in range(4):
            pixel = int(list_10[index_10 + j]) * 4 + int((list_10[index_10 + 4] >> 2 * j) & 0x03)
            list_16.append(pixel & 0xFF)
            list_16.append((pixel >> 8) & 0xFF)
    return list_16

def display_raw(raw_list, width=3840, height=2160, bit=10):
    # 将原始图像数据转换为 NumPy 数组
    raw_image = np.frombuffer(bytes(raw_list), dtype=np.uint16)

    # 数据转为 8bit 显示
    if bit > 8:
        raw_image = raw_image >> bit - 8

    # 调整数组的形状以匹配图像的宽度和高度
    raw_image = raw_image.reshape((height, width))

    # 解码 RGGB 格式图像并将其转换为 RGB 图像
    rgb_image = np.zeros((height, width, 3), dtype=np.uint8)

    # 简单示例:将 G 通道视为原始图像的全部内容
    rgb_image[:, :, 1] = raw_image

    rgb_image = cv2.resize(rgb_image, (640, 480))  # 缩小rgb_image
    # 显示 RGB 图像
    cv2.imshow('RGB Image', rgb_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()



if __name__ == '__main__':
    # 打开 RAW 图像文件并读取其内容
    with open("raw10.raw", 'rb') as file:
        # 读取原始图像数据
        raw_data = file.read()
    
    # RAW10 紧凑型转非紧凑型
    list_16 = bit10_2_bit16(raw_data)
    
    # 使用opencv 显示 raw 图
    display_raw(list_16, 3840, 2160, 10)

效果

在这里插入图片描述

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值