如何解决 Python 中 OpenCv 合并通道的问题?
How can I fix my problem with merging channels in OpenCv in Python?
我只是导入了一张图片。
我想将每个 BGR 通道与零数组合并,然后显示它们。
我的代码如下:
import cv2
import numpy as np
image = cv2.imread('./images/input.jpg')
B, G, R = cv2.split(image)
zeros = np.zeros((image.shape[:2]), dtype = 'int8')
cv2.imshow("Red", cv2.merge([zeros1, zeros1, R]))
cv2.imshow("Green", cv2.merge([zeros, G, zeros]))
cv2.imshow("Blue", cv2.merge([B, zeros, zeros]))
cv2.waitKey(500)
cv2.destroyAllWindows()
在 运行 这段代码之后,我得到以下错误。
error Traceback (most recent call last)
<ipython-input-13-9252d63ed763> in <module>
7 zeros = np.zeros((image.shape[:2]), dtype = 'int8')
8
----> 9 cv2.imshow("Red", cv2.merge([zeros1, zeros1, R]))
10 cv2.imshow("Green", cv2.merge([zeros, G, zeros]))
11 cv2.imshow("Blue", cv2.merge([B, zeros, zeros]))
error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\core\src\merge.dispatch.cpp:129: error: (-215:Assertion failed) mv[i].size == mv[0].size && mv[i].depth() == depth in function 'cv::merge'
这对我来说似乎是正确的。有什么问题,我该如何解决?
这是错误:
zeros = np.zeros((image.shape[:2]), dtype = 'int8')
dtype 应该与图像相同所以 uint8
.
顺便说一句,我想你只想显示图像而不关心合并?
然后你就可以用一个通道从 GRAY 转换成 BGR:
cv2.imshow("Red", cv2.cvtColor(R, cv2.COLOR_GRAY2BGR))
cv2.imshow("Green", cv2.cvtColor(G, cv2.COLOR_GRAY2BGR))
cv2.imshow("Blue", cv2.cvtColor(B, cv2.COLOR_GRAY2BGR))
这一行:
zeros = np.zeros((image.shape[:2]), dtype = 'int8')
将 dtype
更改为 'uint8'
我只是导入了一张图片。 我想将每个 BGR 通道与零数组合并,然后显示它们。 我的代码如下:
import cv2
import numpy as np
image = cv2.imread('./images/input.jpg')
B, G, R = cv2.split(image)
zeros = np.zeros((image.shape[:2]), dtype = 'int8')
cv2.imshow("Red", cv2.merge([zeros1, zeros1, R]))
cv2.imshow("Green", cv2.merge([zeros, G, zeros]))
cv2.imshow("Blue", cv2.merge([B, zeros, zeros]))
cv2.waitKey(500)
cv2.destroyAllWindows()
在 运行 这段代码之后,我得到以下错误。
error Traceback (most recent call last)
<ipython-input-13-9252d63ed763> in <module>
7 zeros = np.zeros((image.shape[:2]), dtype = 'int8')
8
----> 9 cv2.imshow("Red", cv2.merge([zeros1, zeros1, R]))
10 cv2.imshow("Green", cv2.merge([zeros, G, zeros]))
11 cv2.imshow("Blue", cv2.merge([B, zeros, zeros]))
error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\core\src\merge.dispatch.cpp:129: error: (-215:Assertion failed) mv[i].size == mv[0].size && mv[i].depth() == depth in function 'cv::merge'
这对我来说似乎是正确的。有什么问题,我该如何解决?
这是错误:
zeros = np.zeros((image.shape[:2]), dtype = 'int8')
dtype 应该与图像相同所以 uint8
.
顺便说一句,我想你只想显示图像而不关心合并?
然后你就可以用一个通道从 GRAY 转换成 BGR:
cv2.imshow("Red", cv2.cvtColor(R, cv2.COLOR_GRAY2BGR))
cv2.imshow("Green", cv2.cvtColor(G, cv2.COLOR_GRAY2BGR))
cv2.imshow("Blue", cv2.cvtColor(B, cv2.COLOR_GRAY2BGR))
这一行:
zeros = np.zeros((image.shape[:2]), dtype = 'int8')
将 dtype
更改为 'uint8'