gpt4 book ai didi

python-3.x - OpenCV合并无法合并图像 channel

转载 作者:行者123 更新时间:2023-12-02 17:04:03 24 4
gpt4 key购买 nike

我正在尝试将高斯噪声添加到图像的单个 channel 中。

import cv2 as cv
import numpy as np

img1 = cv.imread('input/foo.png')
img1_blue, img1_green, img1_red = cv.split(img1)
img1_h, img1_w, _ = img1.shape

s = 5
noise = np.random.normal(0, s, (img1_h, img1_w))
img1_gn = img1_green + noise

print(img1_green.shape) # (512, 384)
print(img1_gn.shape) # (512, 384)
print(img1_blue.shape) # (512, 384)

img1_g_noise = cv.merge((img1_blue, img1_gn, img1_red))

这会导致以下错误:
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-34-049cf9e65133> in <module>
13
---> 14 img1_g_noise = cv.merge((img1_blue, img1_gn, img1_red))
15

error: OpenCV(3.4.5) /io/opencv/modules/core/src/merge.cpp:293: error: (-215:Assertion failed) mv[i].size == mv[0].size && mv[i].depth() == depth in function 'merge'

我不确定这是如何或为什么会发生的。生成的嘈杂绿色 channel 与其他两个 channel 具有相同的尺寸和类型。重新组合原来的绿色 channel 就可以了。任何转向方向表示赞赏,并提前感谢您。

最佳答案

这是因为噪声和 channel 数据类型不匹配。 numpy 矩阵的默认数据类型为 numpy.float64。并且您必须通过添加 .astype(img1_blue.dtype) 来定义 rach channel 类型中的噪声噪声防御。

编辑代码:

import cv2 as cv
import numpy as np

img1 = cv.imread('list.JPG')
img1_blue, img1_green, img1_red = cv.split(img1)
img1_h, img1_w, _ = img1.shape

s = 5
noise = np.random.normal(0, s, (img1_h, img1_w)).astype(img1_blue.dtype)
img1_gn = img1_green + noise

print(img1_green.shape) # (512, 384)
print(img1_gn.shape) # (512, 384)
print(img1_blue.shape) # (512, 384)

img1_g_noise = cv.merge((img1_blue, img1_gn, img1_red))
cv.imshow("img1_g_noise",img1_g_noise)
cv2.waitKey()

关于python-3.x - OpenCV合并无法合并图像 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57839149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com