如何正确可视化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类用来对 ...
随机推荐
- LR参数化取值规则总结
我想使用参数化输入设置10个并发用户循环1000次,第一个用户使用参数列表中的前1000个参数(第依次循环使用第一个参数.第二次循环使用第二个参数,依次类推).第二个用户使用参数列表中的2001-30 ...
- [NOIP2017普及组]棋盘
题目 题目描述 有一个m × m的棋盘,棋盘上每一个格子可能是红色.黄色或没有任何颜色的.你现在要从棋盘的最左上角走到棋盘的最右下角. 任何一个时刻,你所站在的位置必须是有颜色的(不能是无色的),你只 ...
- 【JavaScript】数组
[声明一个数组]var a=[1,1,1]; [定义数组的长度]var a=new Array(2); [特殊数组]arguments[0][可以不用声明,当数组内没有东西时可以直接通过方法的参数自动 ...
- shell里连接数据库,将结果输出到变量
result=$(sqlplus -s 'ccc/ccc@21.96.34.34:1521'<<EOF ..... EOF )
- Java并发包1--线程的状态及常用方法
一.线程主要有以下几种状态: new(新建):线程刚刚被创建 runnable(就绪):新建的线程执行start方法进入就绪状态等待系统调度分配CPU,被分配了之后就进入运行中状态 blocked(阻 ...
- python3下爬取网页上的图片的爬虫程序
import urllib.request import re #py抓取页面图片并保存到本地 #获取页面信息 def getHtml(url): html = urllib.request.urlo ...
- mysql_study_4
索引 ALTER TABLE 表名字 ADD INDEX 索引名 (列名); CREATE INDEX 索引名 ON 表名字 (列名); 索引的效果就是加快查询速度,当表中数据不够多的时候是感受不出他 ...
- Holer服务端软件使用
用户可以下载 holer-server.zip 搭建自己的Holer服务端. 1. 搭建Holer服务端准备工作 (1) 准备一台Linux系统或者Windows系统主机: (2) 安装Java 1. ...
- CentOS7 安装极点五笔输入法
下载并解压vissible-ibus.tar.gz 或者这个vissible-ibus.tar.gz cp vissible.db /usr/share/ibus-table/tables/ cp v ...
- 【Linux】文件IO --- sync、fsync、fdatesync
在使用write函数向文件中写入数据的时候,并不是在调用了函数以后就直接把数据写入磁盘:操作系统在内核中设置了一块专门的缓冲区,数据会先被写入到内核的缓冲区中,等到缓冲区满了或者系统需要重新利用缓冲区 ...