2

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.

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).

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.

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)

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

Community
  • 1
  • 1
Apostolos
  • 7,763
  • 17
  • 80
  • 150
  • Can you please link some input data so we can reproduce the problem. – Daniel Oct 31 '13 at 17:17
  • http://iconbug.com/data/45/256/ee71e5424fa6f61009240ab62c956d5e.png i used this image..it gives me all these errors https://www.google.com/mapmaker/mapfiles/small_blue_ball.png This one produces the new images but i still get an error for RuntimeWarning but no shape mismatch – Apostolos Oct 31 '13 at 18:15
  • Still no luck?I can't get to pinpoint the problems...I think my code looks right – Apostolos Nov 01 '13 at 07:04
  • Were you aware of Python's [colorsys module](http://docs.python.org/2/library/colorsys.html)? – Mark Ransom Dec 02 '13 at 23:32
  • yes but as stated in the link of the stackoverlow question in my original post, it says that the guy achieved 5 times speed using numpy – Apostolos Dec 03 '13 at 11:23

1 Answers1

0

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]

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

hunse
  • 3,175
  • 20
  • 25