numpy 中的 RGB 到 HSV [英] RGB to HSV in numpy

查看:88
本文介绍了numpy 中的 RGB 到 HSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 这里:

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

def rgb2hsv_np(img_rgb):
    assert img_rgb.dtype == np.float32
    
    height, width, c = img_rgb.shape
    r, g, b = img_rgb[:,:,0], img_rgb[:,:,1], img_rgb[:,:,2]  
    
    t = np.min(img_rgb, axis=-1)
    v = np.max(img_rgb, axis=-1)
    
    s = (v - t) / (v + 1e-6)
    s[v==0] = 0
    
    # v==r
    hr = 60 * (g - b) / (v - t + 1e-6)
    # v==g
    hg = 120 + 60 * (b - r) / (v - t + 1e-6)
    # v==b
    hb = 240 + 60 * (r - g) / (v - t + 1e-6)

    h = np.zeros((height, width), np.float32)
    
    h = h.flatten()
    hr = hr.flatten()
    hg = hg.flatten()
    hb = hb.flatten()
    
    h[(v==r).flatten()] = hr[(v==r).flatten()]
    h[(v==g).flatten()] = hg[(v==g).flatten()]
    h[(v==b).flatten()] = hb[(v==b).flatten()]
    
    h[h<0] += 360
    
    h = h.reshape((height, width))
    
    img_hsv = np.stack([h, s, v], axis=-1)
    
    return img_hsv


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

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

img_rgb = img_rgb / 255.0
img_rgb = img_rgb.astype(np.float32)

img_hsv1 = rgb2hsv_np(img_rgb)
img_hsv2 = rgb2hsv_opencv(img_rgb)

print('max diff:', np.max(np.fabs(img_hsv1 - img_hsv2)))
print('min diff:', np.min(np.fabs(img_hsv1 - img_hsv2)))
print('mean diff:', np.mean(np.fabs(img_hsv1 - img_hsv2)))

但我得到了很大的不同:

But I get big diff:

max diff: 240.0
min diff: 0.0
mean diff: 0.18085355

我是否遗漏了什么?

还有可能编写更高效的 numpy 代码,例如没有 flatten 吗?

Also maybe it's possible to write numpy code more efficient, for example without flatten?

我也很难找到 cvtColor 函数的原始 C++ 代码,据我所知,它实际上应该是 C 代码中的 cvCvtColor 函数,但我找不到实际的带公式的源代码.

Also I have hard time finding original C++ code for cvtColor function, as I understand it should be actually function cvCvtColor from C code, but I can't find actual source code with formula.

推荐答案

从最大差异正好是 240 的事实来看,我很确定发生的情况是 v==r, v==gv==b 同时为真,最后执行.

From the fact that the max difference is exactly 240, I'm pretty sure that what's happening is in the case when both or either of v==r, v==g are simultaneously true alongside v==b, which gets executed last.

如果您更改订单:

h[(v==r).flatten()] = hr[(v==r).flatten()]
h[(v==g).flatten()] = hg[(v==g).flatten()]
h[(v==b).flatten()] = hb[(v==b).flatten()]

致:

h[(v==r).flatten()] = hr[(v==r).flatten()]
h[(v==b).flatten()] = hb[(v==b).flatten()]
h[(v==g).flatten()] = hg[(v==g).flatten()]

最大差异可能开始显示为 120,因为在该等式中添加了 120.因此,理想情况下,您希望按照 b->g->r 的顺序执行这三行.那么差异应该可以忽略不计(仍然注意到最大差异为 0.01~,将其归为某个四舍五入的地方).

The max difference may start showing up as 120, because of that added 120 in that equation. So ideally, you would want to execute these three lines in the order b->g->r. The difference should be negligible then (still noticing a max difference of 0.01~, chalking it up to some round off somewhere).

h[(v==b).flatten()] = hb[(v==b).flatten()]
h[(v==g).flatten()] = hg[(v==g).flatten()]
h[(v==r).flatten()] = hr[(v==r).flatten()]

这篇关于numpy 中的 RGB 到 HSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆