如何不使用cv2.cvtColor()将3通道图像转换为1通道图像?

时间:2019-10-28 07:46:34

标签: python opencv rgb

因此,我被要求使用每个像素的加权平均值将 BGR 图像转换为 GRAYSCALE

img = cv2.imread('..\\Images\\horse.jpg',-1)
height = img.shape[0]
width = img.shape[1]
gray = img.copy()
for x in range(img.shape[1]):
  for y in range(img.shape[0]):
     gray[y][x]= (0.11 * img[y][x][0] + 0.6 * img[y][x][1] + 0.3 * img[y][x][2])



print(gray)
print(gray.shape)
cv2.imshow('gray',gray)
cv2.waitkey(0)

结果图像的形状:

(404, 640, 3)

它应该是一个通道图像吗? 结果显示的图像是GRALESCALE,但它仍然是3通道图像,有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

原因非常简单,这是因为您在开始时复制了整个img,它具有三个通道。您只需要像这样复制一个频道:

gray = img[:, :, 0].copy()