gpt4 book ai didi

python - 'tuple'对象没有属性 'clip'

转载 作者:行者123 更新时间:2023-12-02 16:31:08 24 4
gpt4 key购买 nike

我在以下无法理解的代码中遇到错误

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])
请解释为什么会发生这种情况以及如何解决该问题。如果有人可以提供有关此问题的任何建议,那就太好了。

最佳答案

您正在将cv2.threshold的结果传递给cv2_imshow,该结果是一个元组。您需要将通话修改为

cv2_imshow(threshold_images[threshold][1])

关于python - 'tuple'对象没有属性 'clip',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64207654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com