gpt4 book ai didi

python - OpenCV:rbg-> hsv-> rgb是否无损?

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

这是转换rgb-> hsv-> rgb的一些测试:

def rgb2hsv_opencv(img_rgb):
img_hsv = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV)
return img_hsv

def hsv2rgb_opencv(img_hsv):
img_rgb = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
return img_rgb

img_bgr = cv2.imread('00000.png')

img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)

img_hsv = rgb2hsv_opencv(img_rgb)

img_rgb2 = hsv2rgb_opencv(img_hsv)

cv2.imwrite('debug_1.png', img_rgb)
cv2.imwrite('debug_2.png', img_rgb2)

print('diff:', np.max(np.fabs(img_rgb - img_rgb2)))
图片看起来一样,但是由于某种原因 diff不为零,这是预期的吗?

最佳答案

实际上documentation对此主题有一些说明:

If you use cvtColor with 8-bit images, the conversion will have someinformation lost. For many applications, this will not be noticeablebut it is recommended to use 32-bit images in applications that needthe full range of colors or that convert an image before an operationand then convert back.


因此,在使用非线性转换之前添加 img_rgb = img_rgb.astype(np.float32)会有所帮助,它将最大绝对吸收值降低到 diff: 0.00011444092并添加 img_rgb = img_rgb / 255.0可以提供更多帮助,将最高吸收差值减少到 diff: 4.172325e-07

关于python - OpenCV:rbg-> hsv-> rgb是否无损?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63688369/

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