OpenCV+计算摄影

图像去噪

  • cv.fastNlMeansDenoising()-处理单个灰度图像
  • cv.fastNlMeansDenoisingColored()-处理彩色图像。
  • cv.fastNlMeansDenoisingMulti()-处理在短时间内捕获的图像序列(灰度图像)
  • cv.fastNlMeansDenoisingColoredMulti()-与上面相同,但用于彩色图像。

常用参数为:

  • h:决定滤波器强度的参数。较高的h值可以更好地消除噪点,但同时也可以消除图像细节。(可以设为10)
  • hForColorComponents:与h相同,但仅用于彩色图像。(通常与h相同)
  • templateWindowSize:应为奇数。(建议设为7)
  • searchWindowSize:应为奇数。(建议设为21)

图像修补

我们需要创建一个与输入图像大小相同的掩码,其中非零像素对应于要修复的区域。

c 复制代码
import numpy as np
import cv2 as cv
img = cv.imread('messi_2.jpg')
mask = cv.imread('mask2.png',0)
dst = cv.inpaint(img,mask,3,cv.INPAINT_TELEA)
cv.imshow('dst',dst)
cv.waitKey(0)
cv.destroyAllWindows()

高动态范围

将曝光图像加载到列表中

将曝光合成HDR图像

在此阶段,我们将曝光序列合并为一张HDR图像,显示了OpenCV中的两种可能性。 第一种方法是Debevec,第二种方法是Robertson。 请注意,HDR图像的类型为float32,而不是uint8,因为它包含所有曝光图像的完整动态范围。

c 复制代码
import cv2 as cv
import numpy as np
# 将曝光图像加载到列表中
img_fn = ["img0.jpg", "img1.jpg", "img2.jpg", "img3.jpg"]
img_list = [cv.imread(fn) for fn in img_fn]
exposure_times = np.array([15.0, 2.5, 0.25, 0.0333], dtype=np.float32)
# 将曝光合成HDR图像
merge_debevec = cv.createMergeDebevec()
hdr_debevec = merge_debevec.process(img_list, times=exposure_times.copy())
merge_robertson = cv.createMergeRobertson()
hdr_robertson = merge_robertson.process(img_list, times=exposure_times.copy())

色调图HDR图像

我们将32位浮点HDR数据映射到0...1范围内。实际上,在某些情况下,该值可以大于1或小于0,因此请注意,我们稍后将必须裁剪数据以避免溢出。

c 复制代码
# 色调图HDR图像
tonemap1 = cv.createTonemap(gamma=2.2)
res_debevec = tonemap1.process(hdr_debevec.copy())

使用Mertens融合曝光

在这里,我们展示了一种替代算法,用于合并曝光图像,而我们不需要曝光时间。我们也不需要使用任何色调映射算法,因为Mertens算法已经为我们提供了0...1范围内的结果。

c 复制代码
# 使用Mertens融合曝光
merge_mertens = cv.createMergeMertens()
res_mertens = merge_mertens.process(img_list)

转为8-bit并保存

为了保存或显示结果,我们需要将数据转换为0...255范围内的8位整数。

c 复制代码
# 转化数据类型为8-bit并保存
res_debevec_8bit = np.clip(res_debevec*255, 0, 255).astype('uint8')
res_robertson_8bit = np.clip(res_robertson*255, 0, 255).astype('uint8')
res_mertens_8bit = np.clip(res_mertens*255, 0, 255).astype('uint8')
cv.imwrite("ldr_debevec.jpg", res_debevec_8bit)
cv.imwrite("ldr_robertson.jpg", res_robertson_8bit)
cv.imwrite("fusion_mertens.jpg", res_mertens_8bit)
相关推荐
程序员cxuan2 分钟前
本地跑一个 Qwen 3.8,你将拥有一个 Opus 4.6
人工智能·后端·程序员
aidesignplus6 分钟前
Anthropic 最新发布《2026 Agentic Coding Trends Report》粗浅解析
人工智能
能源革命8 分钟前
AI日报 2026-08-15
人工智能
Uncommon.18 分钟前
使用Pytorch自动计算梯度
人工智能·pytorch·python
安逸sgr29 分钟前
循环神经网络 RNN、LSTM、GRU 是什么?为什么能处理序列?
人工智能·ai·大模型·agent·智能体
猿小猴子39 分钟前
主流 AGENT 实战教程「OpenCodex」与「ChatGPT Work」与「Codex Record & Replay」介绍
人工智能·ai·chatgpt·codex·minimax·deepseek·opencodex
shdwak....sad1 小时前
版本管理&迁移kali-vmware&知识库
人工智能·网络安全
程序员cxuan1 小时前
OpenAI Linux 版来了!
人工智能·后端·程序员
用户5619035069331 小时前
OpenAI兼容API报401/404/429怎么解决?API Key、Base URL、模型名排查
人工智能
lucky_syq1 小时前
专栏目录与学习路线 | 48 篇正文导览
人工智能·学习