ISP最简单步骤,计算得到一个sRGB色域图像
black level correction
linear
shading correction
white balance
CCM
gamma
经过以上步骤可以从raw图得到一个简单的sRGB格式的图片
其中里面的参数需要事先标定好。
import colour
from colour_demosaicing import demosaicing_CFA_Bayer_bilinear, demosaicing_CFA_Bayer_Malvar2004, \demosaicing_CFA_Bayer_Menon2007'''
1. 对raw图降噪
2. 当前raw图存在 比较大的 不合理 的噪声,需要从根源分析原因和解决
'''
import osimport cv2
import numpy as np
import matplotlib.pyplot as pltdef show_raw(im, r, g, b):fig = plt.figure()ax1 = fig.add_subplot(221)ax1.imshow(im)ax2 = fig.add_subplot(222)ax2.imshow(r)ax3 = fig.add_subplot(223)ax3.imshow(g)ax4 = fig.add_subplot(224)ax4.imshow(b, cmap='gray')plt.show()def read_raw(filename, height, width):# filename = r'D:\MedData\raw图像\20220511\raw\cap_frame_8977.raw'file = np.fromfile(filename, dtype=np.int16)print(len(file))# BGGRim = file.reshape([height, width])return imdef raw_to_rgb(raw):#b = raw[0::2, 0::2]g1 = raw[0::2, 1::2]g2 = raw[1::2, 0::2]r = raw[1::2, 1::2]b = b // 4r = r // 4g = (g1 + g2) // 8rgb = cv2.merge([r, g, b]).astype(np.uint8)return rgbdef raw_to_rgb_file(filename, height=800, width=1280):# filename = r'D:\MedData\raw图像\20220511\raw\cap_frame_8977.raw'raw = read_raw(filename, height, width)raw = raw.astype(np.uint16)rgb = raw_to_rgb(raw)rgb2 = cv2.medianBlur(rgb, 3)rgb3 = cv2.medianBlur(rgb, 5)rgb4 = cv2.medianBlur(rgb, 7)# plt.figure()# plt.imshow(rgb)# plt.show()show_raw(rgb, rgb2, rgb3, rgb4)cv2.imwrite(filename[:-4] + '_raw.jpg', rgb[..., ::-1])# cv2.imwrite(filename[:-4] + '_raw3.jpg', rgb2[...,::-1])# cv2.imwrite(filename[:-4] + '_raw5.jpg', rgb3[...,::-1])def black_sub(filename):raw_image = np.fromfile(filename, dtype=np.uint16)print(len(raw_image))raw_image = raw_image.reshape([height, width])# raw_image =raw_image>>6print('raw20x20 :\n', raw_image[20:30, 20:30])# black levelblack_level = 16white_level = 1023normalized_image = raw_image.astype(np.float32) - black_level# if some values were smaller than black levelnormalized_image[normalized_image < 0] = 0normalized_image = normalized_image / (white_level - black_level)print(normalized_image.min(), normalized_image.max())plt.figure()plt.imshow(normalized_image)plt.show()# demosciac# rgb = demosaicing_CFA_Bayer_Menon2007(normalized_image, "BGGR")rgb = demosaicing_CFA_Bayer_bilinear(normalized_image, "BGGR")rgb_save = rgb * 255rgb_save = np.clip(rgb_save, 0, 255)rgb_save = rgb_save.astype(np.uint8)cv2.imwrite(filename[:-4] + '_raw.png', rgb_save[..., ::-1])return normalized_imagedef convert_input(filename, show_im=0):h = 800w = 800# filename = r'D:\MedData\mydata2\raw\cap_input_frame_0013.rgb'data = np.fromfile(filename, dtype=np.uint8)print(len(data))# print(len(file))b = data[0::4]g = data[1::4]r = data[2::4]a = data[3::4]# print(a.dtype, r.dtype, len(a), len(r))a = np.array(a).reshape([h, w])r = np.array(r).reshape([h, w])g = np.array(g).reshape([h, w])b = np.array(b).reshape([h, w])# print(a[:10, :10], r[:10, :10], g[:10, :10], b[:10, :10])rgb = cv2.merge([r, g, b])if show_im:plt.figure()plt.imshow(rgb)plt.title('rgb')plt.show()filepath = filename[:-4] + '_rgb.png'cv2.imwrite(filepath, rgb[..., ::-1])return datadef apply_tone_map(x):# simple tone curvereturn 3 * x ** 2 - 2 * x ** 3if __name__ == "__main__":height = 800width = 800dir = r'D:\dataset\calib\rawdata'# dir = r'D:\dataset\calib\shading'dir = r'D:\dataset\calib'dir = r'D:\dataset\calib\seka'dir = r'D:\dataset\tmpdata'dir = r'D:\dataset\wang\ccm'dir = r'D:\dataset\noise_img'dir = r'D:\dataset\dang_yingxiangzhiliangceshi\rgb_IMG_vali'files = os.listdir(dir)for file in files:if file.endswith('.raw'):filename = os.path.join(dir, file)normalized_image = black_sub(filename)# if file.endswith('.rgb'):# filename = os.path.join(dir, file)# convert_input(filename, show_im=0)# filename = r'C:\Users\rp\Downloads\RAWimage\A.raw'# filename = r'D:\dataset\data_gain1_4_shading\cap_frame_06nl.raw'# filename = r'D:\dataset\data_gain1_5_test\cap_frame_05.raw'# filename = r'C:\Users\rp\Downloads\RAWimage\D65.raw'run_pipe_shading = 1if run_pipe_shading:# shading biaoding light# r_gain = np.loadtxt(r'D:\dataset\calib\shading\gains_mean_r.txt', dtype=np.float32)# g_gain = np.loadtxt(r'D:\dataset\calib\shading\gains_mean_g.txt', dtype=np.float32)# b_gain = np.loadtxt(r'D:\dataset\calib\shading\gains_mean_b.txt', dtype=np.float32)r_gain = np.loadtxt(r'D:\dataset\wang\shading\gains_mean_r.txt', dtype=np.float32)g_gain = np.loadtxt(r'D:\dataset\wang\shading\gains_mean_g.txt', dtype=np.float32)b_gain = np.loadtxt(r'D:\dataset\wang\shading\gains_mean_b.txt', dtype=np.float32)# valid_tabletable_h = 40table_w = 40r_gain = cv2.resize(r_gain, (table_h, table_w), interpolation=cv2.INTER_LINEAR)g_gain = cv2.resize(g_gain, (table_h, table_w), interpolation=cv2.INTER_LINEAR)b_gain = cv2.resize(b_gain, (table_h, table_w), interpolation=cv2.INTER_LINEAR)np.savetxt(r'D:\dataset\calib\shading\gains_mean_r40.txt', r_gain, fmt='%.4f')np.savetxt(r'D:\dataset\calib\shading\gains_mean_g40.txt', g_gain, fmt='%.4f')np.savetxt(r'D:\dataset\calib\shading\gains_mean_b40.txt', b_gain, fmt='%.4f')r_gain = cv2.resize(r_gain, (height//2, width//2), interpolation=cv2.INTER_LINEAR)g_gain = cv2.resize(g_gain, (height//2, width//2), interpolation=cv2.INTER_LINEAR)b_gain = cv2.resize(b_gain, (height//2, width//2), interpolation=cv2.INTER_LINEAR)normalized_image[0::2, 0::2] *= r_gainnormalized_image[1::2, 1::2] *= b_gainnormalized_image[0::2, 1::2] *= g_gainnormalized_image[1::2, 0::2] *= g_gainrgb = demosaicing_CFA_Bayer_bilinear(normalized_image, "BGGR")# brigtness_aline = 1# if brigtness_aline:# rgb *= 2.7rgb_save = rgb * 255rgb_save = np.clip(rgb_save, 0, 255)rgb_save = rgb_save.astype(np.uint8)cv2.imwrite(filename[:-4] + '_raw_shading.png', rgb_save[..., ::-1])#################################################run_pipe_wb = 1if run_pipe_wb:# raw降噪ksz = 5print(rgb.shape, rgb.dtype)rgb = np.clip(rgb, 0, 1).astype(np.float32)rgb1 = cv2.medianBlur(rgb, ksz)rgb2 = cv2.bilateralFilter(rgb, ksz, 20, 10)rgb3 = cv2.fastNlMeansDenoisingColored(rgb_save, None, 4, 5, 7, 20)rgb3 = rgb3 / 255rgb3 = rgb3.astype(np.float32)rgb = rgb1# # wbwb_gain_light = np.array([2.02527024, 1., 1.1903776])wb_gain_indoor = np.array([1.2, 1., 1.2])wb_gain_d65 = np.array([0.84, 1. , 0.78792666])# 有点奇怪wb_gain_d65 = np.array([2.0, 1., 1.5])# wb_gain = wb_gain[1] / wb_gainwb_gain = wb_gain_d65rgb_wb = rgb * wb_gain.reshape([1, 1, 3])rgb_save = rgb_wb * 255rgb_save = np.clip(rgb_save, 0, 255)rgb_save = rgb_save.astype(np.uint8)cv2.imwrite(filename[:-4] + '_raw_wb.png', rgb_save[..., ::-1])run_pipe_ccm = 1if run_pipe_ccm:# ccmccm_d65 = np.array([[1.5815986, - 0.10358352, 0.03942539],[-0.67781997, 1.3813084, - 0.6855073],[0.09622132, - 0.27772492, 1.6460818]])ccm_light = np.array([[1.7929738, -0.26765725, -0.08204214],[-0.7685349, 1.3916172, -0.40990552],[-0.02443887, -0.12395988, 1.4919477]])ccm_d65_2 = np.array([[ 1.6988674, -0.2256101 , -0.05143011],[-0.53877664 , 1.9854064 , -0.8857581 ],[-0.16009083 ,-0.7597964 ,1.9371881 ]])ccm_d65_3 = np.array([[ 1.3820341, -0.22968723, -0.07441172],[-0.2372965 , 1.6849331 , -0.5606966 ],[-0.1447376 , -0.45524588 , 1.6351084 ]])ccm = ccm_d65_3h, w, c = rgb_wb.shapergb_ccm = np.reshape(rgb_wb, (-1, 3)) @ ccmrgb_ccm = rgb_ccm.reshape([h, w, c])rgb_save = rgb_ccm * 255rgb_save = np.clip(rgb_save, 0, 255)rgb_save = rgb_save.astype(np.uint8)cv2.imwrite(filename[:-4] + '_raw_ccm3.png', rgb_save[..., ::-1])# gammaim_gamma = colour.cctf_encoding(rgb_ccm)rgb_save = im_gamma * 255rgb_save = np.clip(rgb_save, 0, 255)rgb_save = rgb_save.astype(np.uint8)cv2.imwrite(filename[:-4] + '_raw_gamma3.png', rgb_save[..., ::-1])# tone mappingim_tone = apply_tone_map(im_gamma)rgb_save = im_tone * 255rgb_save = np.clip(rgb_save, 0, 255)rgb_save = rgb_save.astype(np.uint8)cv2.imwrite(filename[:-4] + '_raw_tone3.png', rgb_save[..., ::-1])
ISP最简单步骤,计算得到一个sRGB色域图像相关推荐
- 简单阶乘计算 (本题要求实现一个计算非负整数阶乘的简单函数)
6.简单阶乘计算 本题要求实现一个计算非负整数阶乘的简单函数. 实现代码: int Factorial(const int N ) {int i,n=1;if(N>=0){for(i=0;i&l ...
- java计算课程学分绩点,一个简单的计算选修课程绩点的程序,欢迎大家指点下.
一个简单的计算选修课程绩点的程序,欢迎大家指点下. package 选修课程; /** * * @author Administrator */ public class Student { / ...
- 如何通过五个简单步骤成为更好的Stack Overflow用户
by Artem Stepanenko 由Artem Stepanenko 如何通过五个简单步骤成为更好的Stack Overflow用户 (How to become a better Stack ...
- sql 执行顺序_10个简单步骤,完全理解SQL
点击上方SQL数据库开发,关注获取SQL视频教程 SQL专栏 SQL数据库基础知识汇总 SQL数据库高级知识汇总 多年前收藏在笔记中的一篇文章,今天偶然翻出,重读了一遍,依然大有收获.分享出来,大家一 ...
- 为什么使用@tablename起别名产生的sql语句不能用_10个简单步骤,完全理解SQL
多年前收藏在笔记中的一篇文章,今天偶然翻出,重读了一遍,依然大有收获.分享出来,大家一起探讨. 以本文是为了以下读者而特地编写的: 1. 在工作中会用到 SQL 但是对它并不完全了解的人. 2. 能够 ...
- 使用Scrum进行敏捷项目管理的10个简单步骤
敏捷项目管理:如今,工作场所中无法逃脱."将其放入积压中." "我们将在下一个冲刺阶段进行处理." "与Scrum教练交谈." 敏捷项目管理 ...
- 使用 Engage 或 Workspace 创建 Monte Carlo 模拟的 4 个简单步骤
20 世纪 40 年代,研究原子弹的科学家应用 Monte Carlo 模拟计算了一个裂变铀原子引起另一个裂变反应的概率,这是该模拟的首次应用,自此以来已经取得了很大进展.今天我们将介绍如何使用 Mi ...
- 5个简单步骤掌握TensorFlow中的Tensor
在这篇文章中,我们将深入研究Tensorflow Tensor的实现细节.我们将在以下五个简单步骤中介绍与Tensorflow的Tensor中相关的所有主题: 第一步:张量的定义→什么是张量? 第二步 ...
- 安全职业生涯进阶:92 个简单步骤 破解 安全行业
文章出处 http://www.microsoft.com/china/technet/community/columns/secmvp/sv0706.mspx 每月安全 MVP 文章 - 2006 ...
最新文章
- 数据库中INFORMATION_SCHEMA的说明及使用
- 给table里面的添加图标_刺激!微信“变色”了!换个带quot;色quot;的图标吧
- QT的QRubberBand类的使用
- 计算机二级学习考试题,全国计算机等级考试一级Window复习题及答案
- 2080ti服务器支持什么系统,2080ti深度学习性能
- 浏览器缓存问题原理以及解决方案
- WebLogic启动失败:java.lang.AssertionError: Could not obtain the localhost address.
- python怎么跳转到某一行代码_Python中免验证跳转到内容页的实例代码
- 马斯克扎心了!猎鹰重型火箭核心助推器运输过程中坠海
- Vertx.vertx()初始框图和模块
- dataframe基本函数
- MT【337】糖水不等式
- WUST-CTF2020-WP
- CAD2014软件安装资料及教程
- python设置表格格式_python openpyxl表格样式设置
- python数学公式识别_python用re正则表达式实现数学公式计算
- html5新标签 figure 和 figcaption
- 『Java』Zip中Excel文件的解析
- html网页不随缩放而变形,html不随放大缩小而变形——initial-scale
- Aquarius 水瓶