本文介绍了无法在 opencv python 中读取彩色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在 windows (PyCharm IDE) 中使用 python 使用 Opencv.我试图阅读彩色图像.但它以灰度显示.所以我尝试将其转换如下:

I have just started to use Opencv using python in windows(PyCharm IDE).I tried to read a color image. But it got displayed in Grayscale. So I tried to convert it as below:

import cv2
img = cv2.imread('C:\Ai.jpg', 0)
b,g,r = cv2.split(img)
rgb = cv2.merge([r,g,b])
cv2.imshow('image', img)
cv2.imshow('rgb image',rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()

但我收到一个错误:

"b, g, r = cv2.split(img) ValueError: 需要 1 个以上的值解压"

你们能帮帮我吗?提前致谢.

Can you guys please help me out?Thanks in advance.

推荐答案

你的代码第二行有问题 img = cv2.imread('C:\Ai.jpg', 0),根据 文档0值对应cv2.IMREAD_GRAYSCALE,这就是你得到灰度图的原因.如果您想在 RGB 颜色空间中加载它,您可能需要将其更改为 1 或如果您想包含任何其他通道(如 alpha-1code> 与图像一起编码的通道.

There is a problem in the second line of your code img = cv2.imread('C:\Ai.jpg', 0), as per the documentation, 0 value corresponds to cv2.IMREAD_GRAYSCALE, This is the reason why you are getting a grayscale image. You may want to change it to 1 if you want to load it in RGB color space or -1 if you want to include any other channel like alpha channel which is encoded along with the image.

并且 b,g,r = cv2.split(img) 引发错误,因为在那个时间点 img 是只有一个通道的灰度图像,并且不可能将 1 通道图像拆分为 3 个各自的通道.

And b,g,r = cv2.split(img) was raising an error because img at that point of time is a grayscale image which had only one channel, and it is impossible to split a 1 channel image to 3 respective channels.

您的最终片段可能如下所示:

Your final snippet may look like this:

import cv2
# Reading the image in RGB mode
img = cv2.imread('C:\Ai.jpg', 1)

# No need of following lines:
# b,g,r = cv2.split(img)
# rgb = cv2.merge([r,g,b])
# cv2.imshow('rgb image',rgb)

# Displaying the image
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

这篇关于无法在 opencv python 中读取彩色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:41
查看更多