numpy 使用opencv计算RMS强度图像

laximzn5  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(121)

我想计算I(x,y)/Io(x,y)的强度。
首先,我使用rawpy读取图像,因为我有.nef文件(Nikon raw)。然后我使用opencv将图像转换为灰度,并计算I(x,y)/Io(x,y)。其中I(x,y)是“brut”,Io(x,y)是“init”。
但是在划分两个图像(cv2.divide)之后,我使用cv2.meanStdDev(test),我有一个“Nan”值。
当我使用matplotlib绘制“test”时,我得到了这个:

当我从cv 2中使用imshow时,我得到了我想要的:

我不明白为什么我从cv2.meanStdDev(test)中得到nan,以及两个图之间的差异?

import numpy as np
import cv2
import rawpy
import rawpy.enhance
import matplotlib.pyplot as plt

####################
# Reading a Nikon RAW (NEF) image
init="/media/alexandre/Transcend/Expérience/Ombroscopie/eau/initialisation/2023-09-19_19-02-33.473.nef"
brut="/media/alexandre/Transcend/Expérience/Ombroscopie/eau/DT0.2/2023-09-20_10-34-27.646.nef"
bruit="/media/alexandre/Transcend/Expérience/Ombroscopie/eau/bruit-electronique/2023-09-18_18-59-34.994.nef"

####################
# This uses rawpy library
print("reading init file using rawpy.")
raw_init = rawpy.imread(init)
image_init = raw_init.postprocess(use_camera_wb=True, output_bps=16)
print("Size of init image read:" + str(image_init.shape))

print("reading brut file using rawpy.")
raw_brut = rawpy.imread(brut)
image_brut = raw_brut.postprocess(use_camera_wb=True, output_bps=16)
print("Size of brut image read:" + str(image_brut.shape))

print("reading bruit file using rawpy.")
raw_bruit = rawpy.imread(bruit)
image_bruit = raw_bruit.postprocess(use_camera_wb=True, output_bps=16)
print("Size of bruit image read:" + str(image_bruit.shape))

####################
# (grayscale) OpenCV
print(image_init.dtype)
init_grayscale = cv2.cvtColor(image_init, cv2.COLOR_RGB2GRAY).astype(float)
brut_grayscale = cv2.cvtColor(image_brut, cv2.COLOR_RGB2GRAY).astype(float)
bruit_grayscale = cv2.cvtColor(image_bruit, cv2.COLOR_RGB2GRAY).astype(float)

print(np.max(brut_grayscale))
print(init_grayscale.dtype)

"test = (brut_grayscale)/(init_grayscale)"
init = init_grayscale-bruit_grayscale
test = cv2.divide((brut_grayscale),(init_grayscale))

print(test.shape)
print(test.dtype)
print(type(test))

print(test.max())
print(test.min())

####################
# Irms
mean, std_dev = cv2.meanStdDev(test)
intensite_rms = std_dev[0][0]
print("Intensité RMS de l'image :", intensite_rms)

####################
# Matplotlib
import matplotlib.pyplot as plt
plt.imshow(test, cmap='gray')
plt.show()

# Show using OpenCV
import imutils
image_rawpy = imutils.resize(test, width=1080)
cv2.imshow("image_rawpy read file: ", image_rawpy)
cv2.waitKey(0)
cv2.destroyAllWindows("image_rawpy read file: " , image_rawpy)

产出:

reading init file using rawpy.
Size of init image read:(5520, 8288, 3)
reading brut file using rawpy.
Size of brut image read:(5520, 8288, 3)
reading bruit file using rawpy.
Size of bruit image read:(5520, 8288, 3)
uint16
37977.0
float64
(5520, 8288)
float64
<class 'numpy.ndarray'>
nan
nan
Intensité RMS de l'image : nan
^CTraceback (most recent call last):
  File "ombro.py", line 62, in <module>
    plt.show()
  File "/home/alexandre/.local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 368, in show
    return _backend_mod.show(*args, **kwargs)
  File "/home/alexandre/.local/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 3544, in show
    cls.mainloop()
  File "/home/alexandre/.local/lib/python3.8/site-packages/matplotlib/backends/backend_qt.py", line 1023, in mainloop
    qt_compat._exec(qApp)
  File "/usr/lib/python3.8/contextlib.py", line 120, in __exit__
    next(self.gen)
  File "/home/alexandre/.local/lib/python3.8/site-packages/matplotlib/backends/qt_compat.py", line 262, in _maybe_allow_interrupt
    old_sigint_handler(*handler_args)
56lgkhnf

56lgkhnf1#

我不明白为什么我从cv2.meanStdDev(test)得到nan
如果没有原始文件,很难复制您的代码。但是看起来你在init_grayscale中有一些像素nan,而test.max()返回nannp.max(brut_grayscale)返回非nan值。
我不知道为什么会发生这种情况,看起来像rawpy数组可能包含无效的uint16值。请尝试使用dtype=np.uint16image_init转换为np.array
两个地块之间的差异
Matplotlibs plt.imshow规范化输入值(详情请参见documentation),传递给cv2.imshow的值将乘以255(详情请参见post
如果你将plt.imshow调用为:

plt.imshow((test * 255).astype(np.uint8), cmap="gray")

相关问题