使用Python将灰度图像转换为其原始颜色格式

时间:2021-10-19 09:00:06

Hi I am currently working on trying to convert a gray scale image to its original color format using Open CV in python.

您好我正在尝试使用python中的Open CV将灰度图像转换为其原始颜色格式。

import cv2

img = cv2.imread('bw.jpg')

img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)


cv2.imwrite('gray_image.png',gray_image)

executing this produces an error:

执行此操作会产生错误:

error: (-215) scn == 1 && (dcn == 3 || dcn == 4) in function cv::cvtColor

Code in Python Imaging Library are also welcome. Any help will be appreciated.

Python Imaging Library中的代码也是受欢迎的。任何帮助将不胜感激。

Thank you

2 个解决方案

#1


0  

I am assuming that you are trying to convert a single channel image to 3 channel grayscale image. You are reading the image as img = cv2.imread('bw.jpg'), by default if you do not pass any param to cv2.imread(), then it reads a 3 channel image, irrespective of the original number of channels in the image. You may simply remove the line cv2.cvtColor(img, cv2.COLOR_GRAY2RGB), as the img is already a 3 channel image with only grayscale information.

我假设您正在尝试将单通道图像转换为3通道灰度图像。您正在将图像读取为img = cv2.imread('bw.jpg'),默认情况下,如果您未将任何参数传递给cv2.imread(),则它会读取3通道图像,而不管原始通道数是多少在图像中。您可以简单地删除行cv2.cvtColor(img,cv2.COLOR_GRAY2RGB),因为img已经是一个只有灰度信息的3通道图像。

However if you are into this delusion that OpenCV has functionality of filling RGB colors to your grayscale image, then you are probably using wrong library. You can checkout other Open Source projects like this, which colorise your image using Deep Learning.

但是,如果您认为OpenCV具有将RGB颜色填充到灰度图像的功能,那么您可能使用了错误的库。您可以查看其他类似的开源项目,这些项目使用Deep Learning为您的图像着色。

#2


0  

See inline comment where mistake was made.

查看错误发生的内联评论。

import cv2

img = cv2.imread('bw.jpg')

x = img.shape

# check for color or gray-scale image type.
if x[3] == 3:

   print 'Got color image'
   # variable "gray_image" linked to result.
   gray_image = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)

   cv2.imwrite('gray_image.png',gray_image) # varname no longer img > gray_image.

else:
    print 'Got black/white, single channel image.'
    url = 'https://github.com//gustavla//autocolorize'
    print "Using ZdaR's posted solution from %s" % (url)

#1


0  

I am assuming that you are trying to convert a single channel image to 3 channel grayscale image. You are reading the image as img = cv2.imread('bw.jpg'), by default if you do not pass any param to cv2.imread(), then it reads a 3 channel image, irrespective of the original number of channels in the image. You may simply remove the line cv2.cvtColor(img, cv2.COLOR_GRAY2RGB), as the img is already a 3 channel image with only grayscale information.

我假设您正在尝试将单通道图像转换为3通道灰度图像。您正在将图像读取为img = cv2.imread('bw.jpg'),默认情况下,如果您未将任何参数传递给cv2.imread(),则它会读取3通道图像,而不管原始通道数是多少在图像中。您可以简单地删除行cv2.cvtColor(img,cv2.COLOR_GRAY2RGB),因为img已经是一个只有灰度信息的3通道图像。

However if you are into this delusion that OpenCV has functionality of filling RGB colors to your grayscale image, then you are probably using wrong library. You can checkout other Open Source projects like this, which colorise your image using Deep Learning.

但是,如果您认为OpenCV具有将RGB颜色填充到灰度图像的功能,那么您可能使用了错误的库。您可以查看其他类似的开源项目,这些项目使用Deep Learning为您的图像着色。

#2


0  

See inline comment where mistake was made.

查看错误发生的内联评论。

import cv2

img = cv2.imread('bw.jpg')

x = img.shape

# check for color or gray-scale image type.
if x[3] == 3:

   print 'Got color image'
   # variable "gray_image" linked to result.
   gray_image = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)

   cv2.imwrite('gray_image.png',gray_image) # varname no longer img > gray_image.

else:
    print 'Got black/white, single channel image.'
    url = 'https://github.com//gustavla//autocolorize'
    print "Using ZdaR's posted solution from %s" % (url)