-3

I loaded some images (with cv2.imread_color) into a list and I'm trying to get hsv (hue, saturnation, value) values from it with function rgb_to_hsv.

I got red, green and blue values but when i'm trying to get hsv values i get an error about parameters:

Traceback (most recent call last):
  File "C:\Users\Downloads\preprocessing\program.py", line 69, in <module>
    hue_images[i], saturnation_images[i], value_images[i] = rgb_to_hsv(hsv_red[i], hsv_green[i], hsv_blue[i])

File "C:\Users\Downloads\preprocessing\program.py", line 24, in rgb_to_hsv
    mx = max(r, g, b)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

This is the code I'm using:

hue_images = list(range(len(image)))
saturnation_images = list(range(len(image)))
value_images = list(range(len(image)))

hsv_red = list(range(len(image)))
hsv_green = list(range(len(image)))
hsv_blue = list(range(len(image)))

def rgb_to_hsv(r, g, b):
    r, g, b = r/255.0, g/255.0, b/255.0
    mx = max(r, g, b)
    mn = min(r, g, b)
    df = mx-mn
    if mx == mn:
        h = 0
    elif mx == r:
        h = (60 * ((g-b)/df) + 360) % 360
    elif mx == g:
        h = (60 * ((b-r)/df) + 120) % 360
    elif mx == b:
        h = (60 * ((r-g)/df) + 240) % 360
    if mx == 0:
        s = 0
    else:
        s = (df/mx)*100
    v = mx*100
    return h, s, v

def hsv_get_red(img):
    red = img.copy()
    red[:, :, 0] = 0
    red[:, :, 1] = 0

    return red[:, :, 2]

def hsv_get_green(img):
    green = img.copy()
    green[:, :, 0] = 0
    green[:, :, 2] = 0

    return green[:, :, 1]

def hsv_get_blue(img):
    blue = img.copy()
    blue[:, :, 2] = 0
    blue[:, :, 1] = 0

    return blue[:, :, 0]

for i in range(len(image)):
    hsv_red[i] = hsv_get_red(image[i])
    hsv_green[i] = hsv_get_green(image[i])
    hsv_blue[i] = hsv_get_blue(image[i])

for i in range(len(image)):
    hue_images[i], saturnation_images[i], value_images[i] = rgb_to_hsv(hsv_red[i], hsv_green[i], hsv_blue[i])

My question is: How can I use list values as parameters in the function (it works with int numbers) to get results (another list values)?

mx0
  • 6,445
  • 12
  • 49
  • 54
John Smith
  • 75
  • 1
  • 11
  • Possible duplicate of [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – BoarGules Apr 27 '19 at 10:03

1 Answers1

0

the r g b parameters you are passing are Mat "arrays" of opencv library

the in-built min and max functions do not know how to handle them - you have to use the min and max functions of the numpy library

import numpy as np
np.max([r,g,b])

or loop over all the r,g,b values manually to find the min and the max if you dont want the numpy dependency i.e. something like

import itertools
maximum = max( x for x in itertools.chain(r.reshape(r.size),g.reshape(g.size),b.reshape(b.size)))

unfortunately cv2.min does not work like numpy min and it is not useful here

Derte Trdelnik
  • 2,656
  • 1
  • 22
  • 26