我是靠谱客的博主 成就宝马,最近开发中收集的这篇文章主要介绍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:projectsopencv-pythonopencvmodulesimgcodecssrcutils.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()

最后

以上就是成就宝马为你收集整理的python opencv颜色通道_如何使OpenCv结合两个颜色通道显示图像?的全部内容,希望文章能够帮你解决python opencv颜色通道_如何使OpenCv结合两个颜色通道显示图像?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部