[英]How can I fix my problem with merging channels in OpenCv in Python?
I simply imported an image.我只是导入了一张图片。 I want to merge each BGR channel with zero arrays and then show them.
我想将每个 BGR 通道与零 arrays 合并,然后显示它们。 I've written my code as follows:
我编写了如下代码:
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()
After running this code I get the following error.运行此代码后,我收到以下错误。
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'
It seems correct to me.这对我来说似乎是正确的。 what is the problem and how can I Fix this?
有什么问题,我该如何解决?
here's the error:这是错误:
zeros = np.zeros((image.shape[:2]), dtype = 'int8')
dtype should be the same as the image so uint8
. dtype 应该与图像相同,因此
uint8
。
BTW I think you want to just show the image and you don't care about merging?顺便说一句,我认为您只想显示图像而不关心合并?
Then you can just do the convert from GRAY to BGR with a single channel:然后,您可以使用单个通道进行从 GREY 到 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))
In this line:在这一行:
zeros = np.zeros((image.shape[:2]), dtype = 'int8')
change the dtype
to 'uint8'
将
dtype
更改为'uint8'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.