使用 OpenCV 从图像中获取一个颜色通道的输出并不困难,而且可以轻松完成。
但是否有可能从图像的三种主要 BGR 颜色中,我想直接使用函数看到仅绿色和红色组合而不是蓝色的图像?
因此,我可以通过将所有蓝色值设置为 0 来执行上述操作,然后查看图像,其代码如下:
import cv2
import numpy as np
gr = cv2.imread('imagetowork2.jpg')
gr[:, :, 0] = 0
cv2.namedWindow("random_image",cv2.WINDOW_NORMAL)
cv2.resizeWindow("random_image",590,332)
cv2.imshow("random_image",gr)
cv2.waitKey(0)
cv2.destroyAllWindows()
我不得不求助于上面的代码,因为,使用这个gr[:, :, 1:3]
没用。我不明白为什么 cv2 函数适用于整个 3 维值而不适用于二维?
如果他们尝试使用以下命令显示图像,就会出现这个错误gr[:, :, 1:3]
:
error: OpenCV(3.4.5) C:\projects\opencv-python\opencv\modules\imgcodecs\src\utils.cpp:622: error: (-15:Bad number of channels) Source image must have 1, 3 or 4 channels in function 'cvConvertImage'
所以我直接和主要的问题是,是否有一个内置函数可以直接在 OpenCV 或任何其他库中执行此操作,或者将整个颜色值设置为 0 是执行此操作的唯一方法?
So this is the image I am working on(can use any kind of image actually):
And this is the output of what I performed(which I want to get using some in-built function and not setting some values to 0):
这是可能吗?