为了正确可视化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等格式)图像?的更多相关文章

  1. mac电脑 上强大的RAW图像处理工具 ——RAW Power

    苹果电脑曾经有一款名为Aperture的照片处理应用,最终因为苹果软件策略的更好与升级,这款应用已经被苹果砍掉.但Aperture的开发者们并未放弃这款应用,在Mac OS上推出了一款名为RAW Po ...

  2. Data Flow ->> Raw File Source & Raw File Destination

    Raw File Source & Raw File Destination一般用在当有某个package在导入数据或者处理数据需要花费非常长的时间的情况下,可以通过把一些处理好的数据先存到r ...

  3. qcow2、raw、vmdk等镜像格式

    转自 http://www.prajnagarden.com/?p=248 http://blog.csdn.net/starshine/article/details/8179483 转者言:对pr ...

  4. qcow2、raw、vmdk等镜像格式的比较和基本转换

    注:本文转自http://www.cnblogs.com/feisky/archive/2012/07/03/2575167.html   云计算用一个朋友的话来说:”做云计算最苦逼的就是得时时刻刻为 ...

  5. Raw qcow qcow2 vhd-vpc虚拟磁盘格式间相互转换

  6. python可视化库 Matplotlib 00 画制简单图像

    1.下载方式:直接下载Andaconda,简单快捷,减少准备环境的时间 2.图像 3.代码:可直接运行(有详细注释) # -*- encoding:utf-8 -*- # Copyright (c) ...

  7. 大疆无人机 Android 开发总结——视频解码

    DJI_Mobile_SDK是大疆为开发者提供的开发无人机应用的开发接口,可以实现对无人机飞行的控制,也可以利用无人机相机完成一些视觉任务.目前网上的开发教程主要集中于DJI 开发者社区,网上的资源非 ...

  8. RAW格式

    一.什么是RAW文件?RAW文件主要是一种记录了数码相机传感器的原始信息,同时伴随着一些由相机所产生的一些元数据(metadata,诸如IS0的设置.快门速度.光圈值.白平衡等)的文件.不同的相机制造 ...

  9. Android学习记录:SQLite数据库、res中raw的文件调用

    SQLite数据库是一种轻量级的关系型数据库. 在android中保存数据或调用数据库可以利用SQLite. android中提供了几个类来管理SQLite数据库 SQLiteDatabass类用来对 ...

随机推荐

  1. highcharts 大数据 String+,StringBuilder,String.format运行效率比较

    实现String字符串相加的方法有很多,常见的有直接相加,StringBuilder.append和String.format,这三者的运行效率是有差异的,String是final类型的,每次相加都会 ...

  2. webpack4 坑收集:html-webpack-plugin在多页面时,无法将optimization.splitChunks提取的公共块,打包到页面中

    问题描述:  有2个页面index.html和product.html,用html-webpack-plugin和optimization.splitChunks的基本配置如下 { template: ...

  3. Restful levels and Hateoas

    RESTful: Rest是一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等 ...

  4. Oarcle之单行函数(下)

    1.字符函数 ltrim 去除字符串左边指定字符,如果不设定第二个参数,则默认去除空格 rtrim去除字符串右边指定字符,如果不设定第二个参数,则默认去除空格 例如:select ltrim (‘a  ...

  5. 为什么越来越多的人偏爱go语言

    如果你是一个开发者或者程序员,你大概应该听过Go语言或者Golang语言.当然,如果没有听过也没关系,看到这篇文章的同学,就说明你对Golang是关注的,只需要这一点就够了.今天来聊聊关于Golang ...

  6. java面向对象总结(二)

    Java 封装 实现Java封装的步骤 java面向对象值继承 概念: 继承的格式: 类和类之间的关系: 继承的特点: 继承的优缺点 继承的好处: 继承的缺点: 继承的注意事项: 使用继承的步骤: J ...

  7. centos6删除mysql安装

    1.查看已经安装了的mysql包: 2.卸载mysql: 3.查看剩下的mysql安装包: 4.逐个删除剩下的mysql安装包: 5.删除完后再次查看,以确保已删除干净: 6.删除自己安装的mysql ...

  8. 类成员(static)和final修饰符

    在Java类里只能包含成员变量.方法.构造器.初始化块.内部类(包括接口.枚举)5种成员,类成员是用static来修饰的,其属于整个类. 当使用实例来访问类成员时,实际上依然是委托给该类来访问类成员, ...

  9. LeetCode Weekly Contest 118

    要死要死,第一题竟然错误8次,心态崩了呀,自己没有考虑清楚,STL用的也不是很熟,一直犯错. 第二题也是在室友的帮助下完成的,心态崩了. 970. Powerful Integers Given tw ...

  10. mysql批量导出单结构与结构数据表脚本

    由于一个库里面不需要导出全部, 只需要导出一部分指定的数据表结构与数据 那么就写了一个比较简单而且为了能偷懒的小shell #!/bin/bash #************************* ...