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

2024-03-28 09:30:54 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我被要求使用每个像素的加权平均值将BGR图像转换为灰度。你知道吗

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)

它应该是单通道图像,对吗? 结果显示的图像是灰度的,但它仍然是一个3通道的图像,有人能帮我吗?你知道吗


Tags: in图像imgforrange像素cv2灰度
1条回答
网友
1楼 · 发布于 2024-03-28 09:30:54

原因很简单,这是因为您在开头复制了整个img,它有三个通道。您只需复制一个频道,如下所示:

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

相关问题 更多 >