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

问题描述 投票:1回答:1

因此,有人要求我使用每个像素的加权平均值将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通道图像,有人可以帮我吗?

python opencv rgb
1个回答
0
投票

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

gray = img[:, :, 0].copy()
© www.soinside.com 2019 - 2024. All rights reserved.