See In The Dark之RAW图片读取-rawpy

2024-01-27 20:20
文章标签 读取 图片 dark raw see rawpy

本文主要是介绍See In The Dark之RAW图片读取-rawpy,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:处理不同相机的RAW格式图片,并放入卷积运算,输入到see in the dark模型进行夜间识别

1. 保存RAW图片为RGB的JPG格式

import rawpy
import imageio
import numpy as np# .ARW为索尼Sony相机RAW格式
# .CR2为佳能canon相机RAW格式
raw = rawpy.imread('IMG_0001.CR2')# use_camera_wb 是否执行自动白平衡,如果不执行白平衡,一般图像会偏色
# half_size 是否图像减半
# no_auto_bright 不自动调整亮度
# output_bps bit数据, 8或16
img = raw.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=False, output_bps=16)# img = np.float32(img / (2**16-1)*255.0)
# img = np.asarray(img,np.uint8)imageio.imsave('take.jpg', img)

2. bayer阵列解析

拜耳阵列(Bayer pattern)分为GBRG、GRBG、BGGR、RGGB四种模式:
在这里插入图片描述
使用rawpy把不同模式下raw解析为四通道:

import rawpy
import numpy as np
from PIL import Image
import imageio
import exifreaddef gray_ps(rgb):return np.power(np.power(rgb[:, :, 0], 2.2) * 0.2973 + np.power(rgb[:, :, 1], 2.2) * 0.6274+ np.power(rgb[:, :, 2], 2.2) * 0.0753, 1 / 2.2) + 1e-7def do_HDR(x, curve_ratio):gray_scale = np.expand_dims(gray_ps(x), axis=-1)gray_scale_new = np.power(gray_scale, curve_ratio)return np.minimum(x * gray_scale_new / gray_scale, 1.0)def adjust_out_matrix(RAW_path, out=None):raw = open(RAW_path, 'rb')exif_info = exifread.process_file(raw, details=False, strict=True)orientation_str = 'EXIF Orientation'if exif_info.__contains__('Image Orientation'):orientation_str = 'Image Orientation'orientation_info = exif_info[orientation_str].printableif orientation_info == 'Rotated 180':if out is None:return Trueelse:if out.shape[2] == 3:out0 = out[:, :, :1]out1 = out[:, :, 1:2]out2 = out[:, :, 2:3]out = np.concatenate((out2, out1, out0), 2)elif out.shape[2] == 4:out0 = out[:, :, :1]out1 = out[:, :, 1:2]out2 = out[:, :, 2:3]out3 = out[:, :, 3:4]out = np.concatenate((out3, out2, out1, out0), 2)else:raisereturn np.flip(out)elif orientation_info == 'Horizontal (normal)':if out is None:return Falseelse:return outelse:raise# pack Bayer image to 4 channels
def pack_raw(raw_path, white_balance=True, auto_bright=True, HDR=True, save_JPEG=True):raw = rawpy.imread(raw_path)im = raw.raw_image_visible.astype(np.float32)# subtract the black level# 16383(2^14) is the camera's maximal pixel value, you can get it by "np.max(raw.raw_image)" . Ensure full exposure!im = np.maximum(im - raw.black_level_per_channel[0], 0) / (16383 - raw.black_level_per_channel[0])im = np.expand_dims(im, axis=2)H = im.shape[0]W = im.shape[1]if raw.raw_pattern[0, 0] == 0:  # RGGBout = np.concatenate((im[0:H:2, 0:W:2, :],im[0:H:2, 1:W:2, :],im[1:H:2, 1:W:2, :],im[1:H:2, 0:W:2, :]), axis=2)elif raw.raw_pattern[0, 0] == 2:  # BGGRout = np.concatenate((im[1:H:2, 1:W:2, :],im[0:H:2, 1:W:2, :],im[0:H:2, 0:W:2, :],im[1:H:2, 0:W:2, :]), axis=2)elif raw.raw_pattern[0, 0] == 1 and raw.raw_pattern[0, 1] == 0:  # GRBGout = np.concatenate((im[0:H:2, 1:W:2, :],im[0:H:2, 0:W:2, :],im[1:H:2, 0:W:2, :],im[1:H:2, 1:W:2, :]), axis=2)elif raw.raw_pattern[0, 0] == 1 and raw.raw_pattern[0, 1] == 2:  # GBRGout = np.concatenate((im[1:H:2, 0:W:2, :],im[0:H:2, 0:W:2, :],im[0:H:2, 1:W:2, :],im[1:H:2, 1:W:2, :]), axis=2)# 启用白平衡可以防止图片偏蓝或者偏红if white_balance:wb = np.array(raw.camera_whitebalance, np.float32)wb[3] = wb[1]wb = wb / wb[1]out = np.minimum(out * wb, 1.0)if auto_bright:mean_G = (out[:, :, 1].mean() + out[:, :, 3].mean()) / 2.0out = np.minimum(out*0.2/mean_G, 1.0)out = adjust_out_matrix(raw_path, out)# 检测RAW格式解析后的图片是否正常if save_JPEG:out0 = out[:, :, 0:1]out1 = out[:, :, 1:2]out2 = out[:, :, 2:3]out3 = out[:, :, 3:4]out_JPEG = np.concatenate((out0, (out1 + out3) / 2., out2), axis=2)if HDR:out_JPEG = do_HDR(out_JPEG, 0.35)Image.fromarray(np.uint8(out_JPEG * 255)).save('result.jpg')return outif __name__ == '__main__':raw = rawpy.imread('IMG_0001.CR2')np_channel = pack_raw('IMG_0001.CR2', auto_bright=False, HDR=False)img = raw.postprocess(use_camera_wb=True, half_size=False, no_auto_bright=True, output_bps=16)imageio.imsave('rawpy.jpg', img)

3. 苹果相机iphone 13 pro max 的RAW图片

	raw = rawpy.imread("IMG_0001.DNG")im = raw.raw_image_visible.astype(np.float32)  # (h, w, c) = (h, w, 4)# 手机竖直拍摄需要加上下面两行,横向拍摄不需要if vertical:out = np.swapaxes(im,1,0)		# (h w) axis change 20220808out = out[:,::-1,:]   			# Horizontal mirror 20220810

4. 小米12pro 的RAW图片

def pack_raw(raw):#pack Bayer image to 4 channelsim = raw.raw_image_visible.astype(np.float32)im = np.maximum(im - 63,0)/ (255 - 63) #subtract the black levelim = np.expand_dims(im,axis=2)img_shape = im.shapeH = img_shape[0]W = img_shape[1]out = np.concatenate((im[0:H:2,0:W:2,:],im[0:H:2,1:W:2,:],im[1:H:2,1:W:2,:],im[1:H:2,0:W:2,:]), axis=2)# 手机竖直拍摄需要加上下面两行,横向拍摄不需要if vertical:out = np.swapaxes(out,1,0)    # (h w) axis change 20220808out = out[:,::-1,:]           # Horizontal mirror 20220810print("pack_raw out.shape: ", out.shape)return outraw = rawpy.imread("IMG_0001.DNG")
im = raw.raw_image_visible.astype(np.float32)  # (h, w)
input_images = np.expand_dims(pack_raw(raw),axis=0)

这篇关于See In The Dark之RAW图片读取-rawpy的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/651376

相关文章

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati