Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 类型错误:';非类型';对象在opencv中不可编辑_Python 3.x_Opencv - Fatal编程技术网

Python 3.x 类型错误:';非类型';对象在opencv中不可编辑

Python 3.x 类型错误:';非类型';对象在opencv中不可编辑,python-3.x,opencv,Python 3.x,Opencv,我正在遵循下一个文档“”,我有下一个错误: Traceback (most recent call last): File "e:/Proyectos/Detecciondeobjetos/Untitled-1.py", line 9, in <module> for cnt in contours: TypeError: 'NoneType' object is not iterable 回溯(最近一次呼叫最后一次): 文件“e:/Proyectos/deteccio

我正在遵循下一个文档“”,我有下一个错误:

Traceback (most recent call last):
  File "e:/Proyectos/Detecciondeobjetos/Untitled-1.py", line 9, in <module>
    for cnt in contours:
TypeError: 'NoneType' object is not iterable
回溯(最近一次呼叫最后一次):
文件“e:/Proyectos/detecciondobjetos/Untitled-1.py”,第9行,在
对于轮廓中的cnt:
TypeError:“非类型”对象不可编辑
这是我的代码:

import cv2
import numpy as np
font = cv2.FONT_HERSHEY_COMPLEX

img = cv2.imread("123.png", cv2.IMREAD_GRAYSCALE)
_, threshold = cv2.threshold(img, 240, 255, cv2.THRESH_BINARY)
_, contours = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
    cv2.drawContours(img, [approx], 0, (0), 5)
    x = approx.ravel()[0]
    y = approx.ravel()[1]
    if len(approx) == 3:
        cv2.putText(img, "Triangle", (x, y), font, 1, (0))
    elif len(approx) == 4:
        cv2.putText(img, "Rectangle", (x, y), font, 1, (0))
    elif len(approx) == 5:
        cv2.putText(img, "Pentagon", (x, y), font, 1, (0))
    elif 6 < len(approx) < 15:
        cv2.putText(img, "Ellipse", (x, y), font, 1, (0))
    else:
        cv2.putText(img, "Circle", (x, y), font, 1, (0))

cv2.imshow("shapes", img)

cv2.imshow("Threshold", threshold)

cv2.waitKey(0)

cv2.destroyAllWindows()

this is the image i am using
导入cv2
将numpy作为np导入
font=cv2.font\U好时综合酒店
img=cv2.imread(“123.png”,cv2.imread\u灰度)
_,threshold=cv2.threshold(img,240255,cv2.THRESH_二进制)
_,等高线=cv2.findContours(阈值、cv2.RETR_树、cv2.CHAIN_近似值、简单值)
对于轮廓中的cnt:
近似=cv2.近似聚合度(cnt,0.01*cv2.弧长(cnt,真),真)
cv2.等高线图(img,[近似值]、0、(0)、5)
x=大约拉威尔()[0]
y=大约拉威尔()[1]
如果len(近似值)=3:
cv2.putText(img,“三角形”(x,y),字体,1,(0))
elif len(近似值)=4:
cv2.putText(img,“矩形”(x,y),字体,1,(0))
elif len(近似值)=5:
cv2.putText(img,“五角大楼”(x,y),字体,1,(0))
elif 6

等高线==None

因此,您的for循环

for cnt in contours:
     pass
。。。和…一样

for cnt in None:
    pass
None
不是容器<代码>无
没有可以循环的2、3或100个元素。以下是
cv.findContours

contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]] )
你称之为:

_, contours = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
我想你可能已经交换了
,等高线
等高线


无论如何,它看起来像是
层次结构
返回值是
None

轮廓==None

因此,您的for循环

for cnt in contours:
     pass
。。。和…一样

for cnt in None:
    pass
None
不是容器<代码>无没有可以循环的2、3或100个元素。以下是
cv.findContours

contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]] )
你称之为:

_, contours = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
我想你可能已经交换了
,等高线
等高线


无论如何,它看起来像是
层次结构
返回值是

好的,我尝试只进行打印(),但在控制台显示“无”,为什么会发生这种情况?好的,我尝试只进行打印(),但在控制台显示“无”,为什么会发生这种情况?