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

相关文章

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

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷

c/c++的opencv实现图片膨胀

《c/c++的opencv实现图片膨胀》图像膨胀是形态学操作,通过结构元素扩张亮区填充孔洞、连接断开部分、加粗物体,OpenCV的cv::dilate函数实现该操作,本文就来介绍一下opencv图片... 目录什么是图像膨胀?结构元素 (KerChina编程nel)OpenCV 中的 cv::dilate() 函

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

Java如何根据文件名前缀自动分组图片文件

《Java如何根据文件名前缀自动分组图片文件》一大堆文件(比如图片)堆在一个目录下,它们的命名规则遵循一定的格式,混在一起很难管理,所以本文小编就和大家介绍一下如何使用Java根据文件名前缀自动分组图... 目录需求背景分析思路实现代码输出结果知识扩展需求一大堆文件(比如图片)堆在一个目录下,它们的命名规