无法在opencv python中将图像转换为灰色

时间:2017-11-12 20:54:09

标签: python-3.x opencv3.0

在执行其他需要灰度图像的操作之前,以下代码是预备。问题是我转换为灰度似乎不起作用,当进行imshow(测试)时,它是紫色和蓝色的。

%matplotlib inline
import matplotlib.image as mpimg
from matplotlib import pyplot as plt

red_image = mpimg.imread('/vagrant/notebooks/red_thing.jpg')

import cv2
import numpy as np
hsv = cv2.cvtColor(red_image, cv2.COLOR_BGR2HSV)
lower_red = np.array([30,150,50])
upper_red = np.array([255,255,180])

mask = cv2.inRange(hsv, lower_red, upper_red)
res = cv2.bitwise_and(red_image,red_image, mask= mask)

# this should be grayscale but when I display it, it contains color:
test = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

cv2.cvtColor(res,cv2.COLOR_HSV2RGB)