rgb_to_hsv和向后使用python和numpy [英] rgb_to_hsv and backwards using python and numpy

查看:136
本文介绍了rgb_to_hsv和向后使用python和numpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按照此答案中的说明此处执行此代码。我似乎无法摆脱零值。

I tried to execute this code here as described in this answer. Bu I can't seem to get away from dividing with zero value.

我试图从caman Js复制此代码,以便从rgb转换为hsv,但我得到同样的东西。

I tried to copy this code from caman Js for transforming from rgb to hsv but I get the same thing.

RuntimeWarning invalide value encountered in divide

caman code is

caman code is

Convert.rgbToHSV = function(r, g, b) {
  var d, h, max, min, s, v;
  r /= 255;
  g /= 255;
  b /= 255;
  max = Math.max(r, g, b);
  min = Math.min(r, g, b);
  v = max;
  d = max - min;
  s = max === 0 ? 0 : d / max;
  if (max === min) {
    h = 0;
  } else {
    h = (function() {
      switch (max) {
        case r:
          return (g - b) / d + (g < b ? 6 : 0);
        case g:
          return (b - r) / d + 2;
        case b:
          return (r - g) / d + 4;
      }
    })();
    h /= 6;
  }
  return {
    h: h,
    s: s,
    v: v
  };
};

我的代码基于这里的答案

my code based on the answer from here

import Image
import numpy as np

def rgb_to_hsv(rgb):
    hsv = np.empty_like(rgb)
    hsv[...,3] = rgb[...,3]
    r,g,b = rgb[...,0], rgb[...,1], rgb[...,2]
    maxc = np.amax(rgb[...,:3], axis=-1)
    print maxc
    minc = np.amin(rgb[...,:3], axis=-1)
    print minc
    hsv[...,2] = maxc
    dif = (maxc - minc)
    hsv[...,1] = np.where(maxc==0, 0, dif/maxc)
    #rc = (maxc-r)/ (maxc-minc)
    #gc = (maxc-g)/(maxc-minc)
    #bc = (maxc-b)/(maxc-minc)

    hsv[...,0] = np.select([dif==0, r==maxc, g==maxc, b==maxc], [np.zeros(maxc.shape), (g-b) / dif + np.where(g<b, 6, 0), (b-r)/dif + 2, (r - g)/dif + 4])

    hsv[...,0] = (hsv[...,0]/6.0) % 1.0

    idx = (minc == maxc)
    hsv[...,0][idx] = 0.0
    hsv[...,1][idx] = 0.0
    return hsv

除非我用maxc或dif除(因为它们的值为零),我得到它的例外。

The exception I get it in both whereever I divide with maxc or with dif (because they have zero values).

我遇到同样的问题在@unutbu的原始代码上,运行警告。 Caman似乎在每个像素中分别为每个r,g,b组合执行此操作。

I encounter the same problem on the original code by @unutbu, runtimewarning. Caman seems to do this in every pixel seperately that is for every r,g,b combinations.

我还得到形状不匹配的ValueError:Objexts无法广播到执行选择功能时的单一形状。但我仔细检查了所有选择的形状,它们都是(256,256)

I also get a ValueError of shape missmatch: Objexts cannot be broadcast to a single shape when the select function is executed. But i double checked all the shapes of the choices and they are all (256,256)

编辑:
我使用这个维基百科文章,并更新了代码...现在我只获得了runimeWarning

I corrected the function using this wikipedia article, and updated the code...now i get only the runimeWarning

推荐答案

错误来自 numpy.where (以及 numpy。 select )计算其所有参数,即使它们未在输出中使用。所以你的行 hsv [...,1] = np.where(maxc == 0,0,dif / maxc) dif /即使对于 maxc == 0 的元素,也会计算maxc ,但是只有那些 maxc!= 0的元素。这意味着您的输出正常,但您仍然可以获得RuntimeWarning。

The error comes from the fact that numpy.where (and numpy.select) computes all its arguments, even if they aren't used in the output. So in your line hsv[...,1] = np.where(maxc==0, 0, dif/maxc), dif / maxc is computed even for elements where maxc == 0, but then only the ones where maxc != 0 are used. This means that your output is fine, but you still get the RuntimeWarning.

如果您想避免警告(并使代码更快一些),请执行类似的操作:

If you want to avoid the warning (and make your code a little faster), do something like:

nz = maxc != 0   # find the nonzero values
hsv[nz, 1] = dif[nz] / maxc[nz]

您还必须更改 numpy .select 语句,因为它还会计算其所有参数。

You'll also have to change the numpy.select statement, because it also evaluates all its arguments.

这篇关于rgb_to_hsv和向后使用python和numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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