如何正确可视化RAW(ARW,DNG,raw等格式)图像?
为了正确可视化RAW图像,需要做好:白平衡、提亮以及色彩映射。
import numpy as np
import struct
from PIL import Image
import rawpy
import glob
import os def conv(v):
s = 0
for i in range(len(v)):
s += i * v[i]
v[i] = s
return v def diff(v):
n = len(v)
v_diff = np.zeros([len(v)])
for i in range(n-1):
v_diff[n-1-i] = v[n-1-i] - v[n-1-i-1]
return v_diff def 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-7 def 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 gray_world_balance(x):
mean_ = np.maximum(np.mean(x, axis=(0, 1)), 1e-6)
ratio_ = mean_.mean() / mean_
return np.minimum(x * ratio_, 1.0) def show_bbf(path):
print('====== %s =====' % path)
max_level = 1023
black_level = 64
height = 3024
width = 4032
if path.endswith('dng') or path.endswith('DNG'):
raw = rawpy.imread(path)
im = raw.raw_image_visible.astype(np.float32)
res_path = str.replace(path, '.dng', '.jpg')
elif path.endswith('raw') or path.endswith('RAW'):
raw = open(path, 'rb').read()
raw = struct.unpack('H'*int(len(raw)/2), raw)
im = np.float32(raw)
res_path = str.replace(path, '.raw', '.jpg')
else:
assert False
im = im.reshape(height, width)
im = np.maximum(im - black_level, 0) / (max_level - black_level)
# AMPLIFICATION
# n = 256
# std = 0.05
# sample_rate = 1
# extreme_dark_ratio = 13 * std / 0.05
# light_scene_ratio = 4.0 * std / 0.05
# light_threshold = 0.04
# decay_in_light_ratio = 0.3
# light_radius = 5
# total = height * width / (sample_rate*sample_rate) / 4
#
# bins = np.arange(n + 1) / (n - 1)
# hists = [None] * 5
# hists[0], _ = np.histogram(im[0:1512:sample_rate, 0:2016:sample_rate], bins)
# hists[1], _ = np.histogram(im[0:1512:sample_rate, 2016:4032:sample_rate], bins)
# hists[2], _ = np.histogram(im[1512:3024:sample_rate, 0:2016:sample_rate], bins)
# hists[3], _ = np.histogram(im[1512:3024:sample_rate, 2016:4032:sample_rate], bins)
# hists[4] = (hists[0] + hists[1] + hists[2] + hists[3])/4
# convs = [None] * 5
# min_conv = 255
# max_conv = 0
# final_ratio = 1.0
# is_dark = False
#
# for i in range(5):
# hists[i] = hists[i] / total
# convs[i] = conv(hists[i][0:n])
# print(convs[i][-1])
# if convs[i][-1]<min_conv:
# min_conv = convs[i][-1]
# if convs[i][-1]>max_conv:
# max_conv = convs[i][-1]
#
# print('min=%.6f, max=%.6f' % (min_conv, max_conv))
#
# hist_conv = convs[4]
# hist_diff = diff(hist_conv)
# ratio = std / (hist_conv[-1] / n)
# print("Normal ratio=%.6f" % ratio)
# if ratio < 1:
# print("Daylight scene found!")
# final_ratio = 1.0
# # check if exists high contrast scene
# if min_conv < 3 and max_conv/(min_conv + 1e-7) > 2.2:
# print('high contrast scene detected!')
# final_ratio = min(2.0, ratio)
# else:
# if max(hist_diff[-light_radius:]) > light_threshold:
# print('Light Found')
# if ratio > extreme_dark_ratio:
# final_ratio = ratio * decay_in_light_ratio
# print('Extreme Dark')
# else:
# final_ratio = min(light_scene_ratio, ratio)
# else:
# if ratio > extreme_dark_ratio:
# print('Extreme Dark')
# is_dark = True
# final_ratio = extreme_dark_ratio*extreme_dark_ratio*extreme_dark_ratio/(ratio*ratio)
# if 4 < final_ratio < 6:
# final_ratio = 4
# else:
# final_ratio = ratio
# if ratio < 1.0:
# final_ratio = 1.0
# if 5 < final_ratio < 12 and not is_dark:
# final_ratio *= 0.7
# im = np.minimum(im * final_ratio, 1.0)
# print('ratio=%.6f' % final_ratio)
im = np.expand_dims(im, axis=2)
H = im.shape[0]
W = im.shape[1]
out = np.concatenate((
im[0:H:2, 1:W:2, :],
(im[0:H:2, 0:W:2, :] + im[1:H:2, 1:W:2, :])/2.0,
im[1:H:2, 0:W:2, :]),
axis=2)
if path.endswith('dng') or path.endswith('DNG'):
wb = np.array(raw.camera_whitebalance[:3], np.float32)
wb = wb / wb[1]
out = np.minimum(out * wb, 1.0)
else:
out = gray_world_balance(out)
out = np.minimum(out * 0.2 / out[:, :, 1].mean(), 1.0)
out = HDR(out, 0.35)
Image.fromarray(np.uint8(out*255)).save(res_path) def dng2raw(path):
raw = rawpy.imread(path)
im = raw.raw_image_visible.astype(np.ushort)
h, w = im.shape
res_path = str.replace(path, '.dng', '.raw')
with open(res_path, 'wb')as fp:
for i in range(h):
for j in range(w):
a = struct.pack('<H', im[i, j])
fp.write(a) def decode_sony(path):
if os.path.split(path)[-1].split('.')[-1] != 'ARW':
print('Error: image type not match!')
exit(1)
im_ = rawpy.imread(path)
data_ = im_.raw_image_visible.astype(np.float32)
h_, w_ = data_.shape
save_path_ = str.replace(path, '.ARW', '.JPG')
data_ = np.maximum(data_ - 512, 0) / (16383 - 512)
data_ = np.expand_dims(data_, axis=2)
data_ = np.concatenate((
data_[0:h_:2, 0:w_:2, :],
(data_[0:h_:2, 1:w_:2, :] + data_[1:h_:2, 0:w_:2, :]) / 2.0,
data_[1:h_:2, 1:w_:2, :]),
axis=2)
# white balance
wb = np.array(im_.camera_whitebalance[:3], np.float32)
wb = wb / wb[1]
data_ = np.minimum(data_ * wb, 1.0)
data_ = np.minimum(data_ * 0.2 / data_[:, :, 1].mean(), 1.0)
data_ = HDR(data_, 0.35)
Image.fromarray(np.uint8(data_ * 255)).save(save_path_) def decode_sony_rawpy(path):
if os.path.split(path)[-1].split('.')[-1] != 'ARW':
print('Error: image type not match!')
exit(1)
im_ = rawpy.imread(path)
save_path_ = str.replace(path, '.ARW', '.JPG')
Image.fromarray(im_.postprocess(use_camera_wb=True)).save(save_path_) if __name__ == '__main__':
# files = glob.glob('D:/data/report_dng/*.dng')
# for i in range(len(files)):
# show_bbf(files[i]) # files = glob.glob('D:/data/Sony/long/00057_00_10s.ARW')
# im = rawpy.imread(files[0])
# Image.fromarray(im.postprocess(use_camera_wb=True)).show() # files = glob.glob('D:/data/Sony/long/*.ARW')
# for i in range(len(files)):
# decode_sony_rawpy(files[i]) # files = glob.glob('D:/data/Sony/long/RAW/30s/*.ARW')
# for i in range(len(files)):
# decode_sony(files[i]) # files = glob.glob('D:/data/Sony/dataset/illu_detect/day/*.dng')
# for i in range(len(files)):
# dng2raw(files[i]) # files = glob.glob('C:/Users/Administrator/Desktop/ILLU/*.dng')
files = glob.glob('D:/data/LightOnOff/*.dng')
for i in range(len(files)):
# dng2raw(files[i])
show_bbf(files[i])
展示图片:
如何正确可视化RAW(ARW,DNG,raw等格式)图像?的更多相关文章
- mac电脑 上强大的RAW图像处理工具 ——RAW Power
苹果电脑曾经有一款名为Aperture的照片处理应用,最终因为苹果软件策略的更好与升级,这款应用已经被苹果砍掉.但Aperture的开发者们并未放弃这款应用,在Mac OS上推出了一款名为RAW Po ...
- Data Flow ->> Raw File Source & Raw File Destination
Raw File Source & Raw File Destination一般用在当有某个package在导入数据或者处理数据需要花费非常长的时间的情况下,可以通过把一些处理好的数据先存到r ...
- qcow2、raw、vmdk等镜像格式
转自 http://www.prajnagarden.com/?p=248 http://blog.csdn.net/starshine/article/details/8179483 转者言:对pr ...
- qcow2、raw、vmdk等镜像格式的比较和基本转换
注:本文转自http://www.cnblogs.com/feisky/archive/2012/07/03/2575167.html 云计算用一个朋友的话来说:”做云计算最苦逼的就是得时时刻刻为 ...
- Raw qcow qcow2 vhd-vpc虚拟磁盘格式间相互转换
- python可视化库 Matplotlib 00 画制简单图像
1.下载方式:直接下载Andaconda,简单快捷,减少准备环境的时间 2.图像 3.代码:可直接运行(有详细注释) # -*- encoding:utf-8 -*- # Copyright (c) ...
- 大疆无人机 Android 开发总结——视频解码
DJI_Mobile_SDK是大疆为开发者提供的开发无人机应用的开发接口,可以实现对无人机飞行的控制,也可以利用无人机相机完成一些视觉任务.目前网上的开发教程主要集中于DJI 开发者社区,网上的资源非 ...
- RAW格式
一.什么是RAW文件?RAW文件主要是一种记录了数码相机传感器的原始信息,同时伴随着一些由相机所产生的一些元数据(metadata,诸如IS0的设置.快门速度.光圈值.白平衡等)的文件.不同的相机制造 ...
- Android学习记录:SQLite数据库、res中raw的文件调用
SQLite数据库是一种轻量级的关系型数据库. 在android中保存数据或调用数据库可以利用SQLite. android中提供了几个类来管理SQLite数据库 SQLiteDatabass类用来对 ...
随机推荐
- 我的python思考
1.因为例如线性代数之类的数学题较难解决,会耽误我很长时间,所以我希望课程涉及关于数学的库的使用:因为各种考试,例如英语四六级甚至研究生考试各种单词或者关键词都会有使用频率,所以我希望涉及爬虫的应用. ...
- JDK8到JDK12各个版本的重要特性整理
JDK8新特性 1.Lambda表达式 2.函数式编程 3.接口可以添加默认方法和静态方法,也就是定义不需要实现类实现的方法 4.方法引用 5.重复注解,同一个注解可以使用多次 6.引入Optiona ...
- subversion实用命令整理
使用命令在仓库中创建目录 使用命令查看仓库中的内容 使用命令删除仓库中的内容 创建目录 svn mkdir --parents -m 'makeSubDirectory' file:///path/t ...
- 【题解】Luogu P2081 [NOI2012]迷失游乐园
原题传送门 这是当时冬令营课上讲的题,咕咕咕到了现在 如果这题没有环套树的话,就很套路了 需要两个数组up[i]和down[i],down[i]表示从i点第一步向下走的期望距离,up[i]表示从i点第 ...
- FIT2096 Assignment 2 2019
FIT2096 Assignment 2 2019_v1.2.docx 9/04/2019 11:34 AMPage 1 | 4FIT2096 Assignment 2 2019Transformat ...
- [Python数据挖掘]第5章、挖掘建模(下)
四.关联规则 Apriori算法代码(被调函数部分没怎么看懂) from __future__ import print_function import pandas as pd #自定义连接函数,用 ...
- 2018年-2019年第二学期第七周C#学习个人总结
在本周我又学习了,第五章面向对象高级中的5.5异常和5.6命名空间和程序集.在5.5异常中我知道了异常层次结构:所有的异常类都继承自Exception类.由于发生了异常程序立即终止无法再继续向下执行. ...
- tomcat启动项目时一直在跑,项目没起来
1. 在整合maven项目时我遇到一个问题,tomcat启动项目总是报超时,后来我把timeout调到180秒,还是启动超时.看了不是timeout时间短的问题. 2.弄了一天也没解决,后来请大神给看 ...
- 629D - Babaei and Birthday Cake
题意:给定n个圆柱体的半径和高,输入顺序即圆柱体的编号顺序.现在规定,只有编号和体积均大于另一个圆柱体,才能放到另一个圆柱体的体积上面.求能叠加的最大体积是多少. 酝酿了我三天,才理解.自己敲个代码, ...
- Git设置文件或目录忽略跟踪的三种方式
1. 共享的忽略设置方式 本地仓库根目录,创建.gitignore文件,并编辑正则匹配需要忽略的文件或目录. .gitignore文件需要上传到仓库,同时会影响到他人,共享忽略设置 注意: .giti ...