gpt4 book ai didi

python-3.x - 为什么 cv2.COLOR_RGB2GRAY 和 cv2.COLOR_BGR2GRAY 会给出不同的结果?

转载 作者:行者123 更新时间:2023-12-02 16:05:10 27 4
gpt4 key购买 nike

我一直认为将图像从彩色转换为灰度很简单:每个像素的强度将是每个颜色 channel 强度的平均值。但是我注意到 cv2.COLOR_RGB2GRAYcv2.COLOR_BGR2GRAY 给出了不同的结果。当我用它们进行试验时,我还发现它也不同于每个颜色 channel 的强度平均值。
附言当我发现时,我完全困惑了

img_read_as_color[:,:,0]/3+img_read_as_color[:,:,1]/3+img_read_as_color[:,:,2]/3 == (img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2])/3
但是当显示为图像时
(img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2])/3
看起来像
img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2]
有人可以向我解释为什么会这样吗?
我的完整代码:
import matplotlib.pyplot as plt
import cv2

sample = r'G:\Python\knight-mare\screenshots\2020-07-12-02-40-44.jpg'
img_read_as_grayscale = cv2.imread(sample, cv2.IMREAD_GRAYSCALE)
img_read_as_color = cv2.imread(sample, cv2.IMREAD_COLOR)
img_RGB_to_grayscale = cv2.cvtColor(img_read_as_color, cv2.COLOR_RGB2GRAY)
img_BGR_to_grayscale = cv2.cvtColor(img_read_as_color, cv2.COLOR_BGR2GRAY)
plt.imshow(img_read_as_grayscale)
plt.title('img_read_as_grayscale')
plt.show()
plt.imshow(img_read_as_color)
plt.title('img_read_as_color')
plt.show()
plt.imshow(img_RGB_to_grayscale)
plt.title('img_RGB_to_grayscale')
plt.show()
plt.imshow(img_BGR_to_grayscale)
plt.title('img_BGR_to_grayscale')
plt.show()

channel_avg_div_separately = img_read_as_color[:,:,0]/3+img_read_as_color[:,:,1]/3+img_read_as_color[:,:,2]/3
channel_avg_div_together = (img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2])/3
channel_sum = img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2]
plt.imshow(channel_avg_div_separately)
plt.title('channel_avg_div_separately')
plt.show()
plt.imshow(channel_avg_div_together)
plt.title('channel_avg_div_together')
plt.show()
plt.imshow(channel_sum)
plt.title('channel_sum')
plt.show()
image read as grayscale image read as colored image read as colored transformed by RGB2GRAY image read as colored transformed by BGR2GRAY image read as colored, each channel divided by 3 than added together image read as colored, all channels added together, then divided by 3 image read as colored, just adding channels

最佳答案

  • RGB->灰度转换实际上不是平均值——不同 channel 的权重不同。具体:
  • gray_pixel = 0.114 * blue_pixel + 0.299 * red_pixel + 0.587 * green_pixel
    documentation 中也提到了这一点。因此,预计 RGB2GRAY 和 BGR2GRAY 会给出不同的结果。
  • 关于 sum-then-divide 和divide-then-sum 方法之间的差异,即
  • 之间的差异
    img_read_as_color[:,:,0]/3+img_read_as_color[:,:,1]/3+img_read_as_color[:,:,2]/3
    (img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2])/3
    回想一下 cv2.imread 返回一个 uint8 numpy 数组。因此,后一个操作(在除法之前将所有 channel 组合在一起)会导致溢出(实际上,在这种情况下,ipython3 给了我一个运行时警告)。在标记为 channel_avg_div_togetherchannel_sum 的图像中也可以看到类似溢出的伪影。

    关于python-3.x - 为什么 cv2.COLOR_RGB2GRAY 和 cv2.COLOR_BGR2GRAY 会给出不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62855718/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com