如何正确可视化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魔法方法详解
魔法方法 含义 基本的魔法方法 __new__(cls[, ...]) 1.__new__是在一个对象实例化的时候所调用的第一个方法 2.它的第一个参数是这个类,其他的参数是用来直接传递给__ ...
- 大数据处理框架之Strom:DRPC
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 storm-0.9 一.DRPC DRPC:Distri ...
- lua tonumber
[1]应用tonumber函数 local function test(telnum) , )) == ) -- 5000~5999公司预留号码 , ) == ' or isLen or isRese ...
- 用python写栈
class StackFullError(Exception): pass class StackEmptyError(Exception): pass class Stack: def __init ...
- mysql数据库优化之索引的维护和优化
这里是一个工具,即pt-duplicate-key-checker工具 用来检查重复及冗余的索引 用法如下:pt-duplicate-key-checker -uroot -p密码 -h127. ...
- [WDS] Warnings while compiling. vue 项目运行控制台输出太多警告信息
vue项目运行控制台输出太多警告信息,我们需要vue 忽略警告, 解决方法:找到build/webpack.base.conf.js 文件 注释掉: // ...(config.dev.useEsl ...
- oracle数据库创建用户
--4.1 创建表空间 CREATE TABLESPACE mdm_data DATAFILE 'D:\soft\Oracle\oracl\oradata\mdm_data01.dbf' SIZE 3 ...
- 前端框架bootstrap(响应式布局)入门
Bootstrap,是基于HTML,CSS.javascript的前端框架 该框架已经预定义了一套CSS样式和与样式相对应的js代码(对应的样式有对应的特效.) 开发人员只需要编写HTML结构,添加b ...
- rabbitmq 3.7.8基于centos7部署文档
rabbitmq 3.7.8部署文档 安装erlang 安装依赖环境 yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel ope ...
- HDOJ1312 Red and black(DFS深度优先搜索)
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...