Выдает ошибку 'tuple' object has no attribute 'clip', подскажите как решить?

Вот часть кода, которая не воспроизводится, я не понимаю, что не так, потому что есть аналогичный код, только преобразование изображения из BGR в HSV, RGB,XYZ, YUV.

imgColor = cv2.imread(fileName, cv2.IMREAD_COLOR)
imgColor1= cv2.cvtColor(imgColor, cv2.COLOR_BGR2HSV)

ret,thresh1 = cv2.threshold(imgColor1,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(imgColor1,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(imgColor1,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(imgColor1,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(imgColor1,127,255,cv2.THRESH_TOZERO_INV)

threshold_titles = ('BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV')
images = [ thresh1, thresh2, thresh3, thresh4, thresh5]

threshold_images = {threshold : cv2.threshold(imgColor1,127,255, getattr(cv2,'THRESH_'+ threshold )) 
    for threshold in threshold_titles}
for threshold in threshold_images :
   cv2_imshow(threshold_images[threshold])

Программа выдает :

AttributeError                            Traceback (most recent call last)
<ipython-input-31-2702c0b95a98> in <module>()
     13     for threshold in threshold_titles}
     14 for threshold in threshold_images :
---> 15    cv2_imshow(threshold_images[threshold])

/usr/local/lib/python3.6/dist-packages/google/colab/patches/__init__.py in cv2_imshow(a)
     20       image.
     21   """
---> 22   a = a.clip(0, 255).astype('uint8')
     23   # cv2 stores colors as BGR; convert to RGB
     24   if a.ndim == 3:

AttributeError: 'tuple' object has no attribute 'clip'

Вот аналогичный код, который работает :

color_spaces = ('RGB','GRAY','HSV','LAB','XYZ','YUV')
color_images = {color : cv2.cvtColor(imgColor, getattr(cv2,'COLOR_BGR2' + color))
    for color in color_spaces}
for color in color_images:
    cv2_imshow(color_images[color])

Ответы (1 шт):

Автор решения: gil9red

С какой-то версии cv2.threshold возвращает кортеж на два элемент, вам нужно обратиться к второму элементу.

Тут объяснено, что возвращается в результате выполнения функции:

The method returns two outputs. The first is the threshold that was used and the second output is the thresholded image.

Пример:

threshold_images = {
    #                                                                              ↓↓↓↓↓
    threshold: cv2.threshold(imgColor1, 127, 255, getattr(cv2,'THRESH_'+ threshold))[1]
    for threshold in threshold_titles
}
for thresh in threshold_images.values():
    cv2_imshow(thresh)
→ Ссылка