简体   繁体   English

rgb_to_hsv并使用python和numpy向后

[英]rgb_to_hsv and backwards using python and 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. 我似乎无法摆脱零值除法。

I tried to copy this code from caman Js for transforming from rgb to hsv but I get the same thing. 我试图从caman Js复制此代码,以便从rgb转换为hsv,但是我得到了同样的东西。

RuntimeWarning invalide value encountered in divide

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

The exception I get it in both whereever I divide with maxc or with dif (because they have zero values). 我在用maxc或dif进行除法的任何地方都会得到它的例外(因为它们的值为零)。

I encounter the same problem on the original code by @unutbu, runtimewarning. 我在@unutbu,runtimewarning的原始代码上遇到了相同的问题。 Caman seems to do this in every pixel seperately that is for every r,g,b combinations. Caman似乎分别针对每个r,g,b组合在每个像素中执行此操作。

I also get a ValueError of shape missmatch: Objexts cannot be broadcast to a single shape when the select function is executed. 我还得到了形状不匹配的ValueError:执行select函数时,对象无法广播到单个形状。 But i double checked all the shapes of the choices and they are all (256,256) 但是我仔细检查了所有选择的形状,它们都是(256,256)

Edit: I corrected the function using this wikipedia article , and updated the code...now i get only the runimeWarning 编辑:我更正了使用此Wikipedia文章的功能,并更新了代码...现在我只获得了runimeWarning

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. 该错误来自以下事实: numpy.where (和numpy.select )计算其所有参数,即使未在输出中使用它们。 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. 因此,在您线hsv[...,1] = np.where(maxc==0, 0, dif/maxc) dif / maxc即使计算了元素,其中maxc == 0 ,但随后只的那些,其中maxc != 0被使用。 This means that your output is fine, but you still get the RuntimeWarning. 这意味着您的输出很好,但是您仍然可以获得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]

You'll also have to change the numpy.select statement, because it also evaluates all its arguments. 您还必须更改numpy.select语句,因为它还会评估其所有参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM