Reputation: 13
I'm getting an error in the following code that I don't understand
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])
The Error:
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'
I have no Idea why that happens because I have similar code and it works :
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])
Please explain why this is happening and how I can solve the problem. It would be great if anyone could offer any suggestions of this problem.
Upvotes: 1
Views: 2514
Reputation: 2584
You're passing to cv2_imshow the result of cv2.threshold which is a tuple. You need to modify the call to e.g.
cv2_imshow(threshold_images[threshold][1])
Upvotes: 1