python opencv颜色通道_如何使OpenCv结合两个颜色通道显示图像?

It's not that difficult to use OpenCV to get an output of one colour channel from an image and that can be easily done.

But is it possible that from the three main BGR colours of an image, I want to see the image in just a combination of Green and Red without Blue using a function directly?

So I am able to perform this above operation by setting all the Blue values to 0 and then see the image, the code of which is below:

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()

This above code I had to resort to, because, working with this gr[:, :, 1:3] didn't work. And I don't understand why the cv2 functions work with the whole 3 dimensional values but not with two dimensions?

This is the error, one gets, if they try to show the image using 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'

So my direct and main question is, Is there an inbuilt function to perform this in OpenCV or any other library directly or the set a whole colour value as 0 is the only way to do this?

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):

Is this at all possible?

解决方案

I guess one thing you could do is hold the channels separately and combine what you want for viewing:

#!/usr/local/bin/python3

import numpy as np

import cv2

im = cv2.imread('sd71Z.jpg')

b,g,r = cv2.split(im)

zeros = np.zeros_like(b)

cv2.imshow("Zero blue", cv2.merge([zeros,g,r]))

cv2.imshow("Zero green", cv2.merge([b,zeros,r]))

cv2.imshow('Zero red', cv2.merge([b,g,zeros]))

cv2.waitKey()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值