python 并行计算 opencv_opencv-python计算影像

本文介绍了使用Python进行并行计算和OpenCV库处理图像的方法,包括图像去噪、图片修补和高动态范围成像。通过cv.fastNlMeansDenoising系列函数实现图像去噪,cv.inpaint进行图片修补,以及使用exposure fusion技术处理HDR图像。
摘要由CSDN通过智能技术生成

图片去燥

主要函数

cv.fastNlMeansDenoising()对灰度图像去燥

cv.fastNlMeansDenoisingColored() 对彩色图像去燥

cv.fastNlMeansDenoisingMulti()对灰度图片序列去燥

cv.fastNlMeansDenoisingColoredMulti()对彩色图片序列去燥

例子cv.fastNlMeansDenoisingColored()

import numpy as np

import cv2 as cv

from matplotlib import pyplot as plt

img = cv.imread('die.png')

dst = cv.fastNlMeansDenoisingColored(img,None,10,10,7,21)

plt.subplot(121),plt.imshow(img)

plt.subplot(122),plt.imshow(dst)

plt.show()

a562f72e089a1135d79d344a350316fc.png

例子 cv.fastNlMeansDenoisingMulti()

import numpy as np

import cv2 as cv

from matplotlib import pyplot as plt

cap = cv.VideoCapture('vtest.avi')

# create a list of first 5 frames

img = [cap.read()[1] for i in xrange(5)]

# convert all to grayscale

gray = [cv.cvtColor(i, cv.COLOR_BGR2GRAY) for i in img]

# convert all to float64

gray = [np.float64(i) for i in gray]

# create a noise of variance 25

noise = np.random.randn(*gray[1].shape)*10

# Add this noise to images

noisy = [i+noise for i in gray]

# Convert back to uint8

noisy = [np.uint8(np.clip(i,0,255)) for i in noisy]

# Denoise 3rd frame considering all the 5 frames

dst = cv.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 4, 7, 35)

plt.subplot(131),plt.imshow(gray[2],'gray')

plt.subplot(132),plt.imshow(noisy[2],'gray')

plt.subplot(133),plt.imshow(dst,'gray')

plt.show()

78900ad2055c0bc9abe3fc303b0b9d8b.png

https://docs.opencv.org/3.4/d5/d69/tutorial_py_non_local_means.html

图片修补

首先需要创建一个保护罩

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()

a3c954af999f5e23b663ad2fe549c2a8.png

https://docs.opencv.org/3.4/df/d3d/tutorial_py_inpainting.html

图片高动态范围成像

Goal

Learn how to generate and display HDR image from an exposure sequence.

Use exposure fusion to merge an exposure sequence.

1加载不同曝光的图片到列表中

a5ef5a067062c4674f40268dd9ceec59.png

import cv2 as cv

import numpy as np

# Loading exposure images into a list

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)

2合并图片成HDR

# Merge exposures to HDR image

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())

3对合并的图片调整

# Tonemap HDR image

tonemap1 = cv.createTonemap(gamma=2.2)

res_debevec = tonemap1.process(hdr_debevec.copy())

4使用Mertens fusion合并图片

# Exposure fusion using Mertens

merge_mertens = cv.createMergeMertens()

res_mertens = merge_mertens.process(img_list)

5转换成8位格式的图片并保存

# Convert datatype to 8-bit and save

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)

9deaeb6c632d295fb2eb6c235e51635a.png

对摄像头的曲线评估

# Estimate camera response function (CRF)

cal_debevec = cv.createCalibrateDebevec()

crf_debevec = cal_debevec.process(img_list, times=exposure_times)

hdr_debevec = merge_debevec.process(img_list, times=exposure_times.copy(), response=crf_debevec.copy())

cal_robertson = cv.createCalibrateRobertson()

crf_robertson = cal_robertson.process(img_list, times=exposure_times)

hdr_robertson = merge_robertson.process(img_list, times=exposure_times.copy(), response=crf_robertson.copy())

0a9dce986ae81ce66ba5a52147858811.png

https://docs.opencv.org/3.4/d2/df0/tutorial_py_hdr.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值