您好,我正在关注下一个文档“ https://pysource.com/2018/09/25/simple-shape-detection-opencv-with-python-3/”,但遇到下一个错误:
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
这是我的代码:
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
答案 0 :(得分:0)
template<int size>
constexpr auto createObj(){
// implicitly constexpr ---v
constexpr auto enumArray = []{
Enums enum_array[size] = {};
for(int i = 0; i < size ; i++){
//fill array with e1 and e2
}
return enum_array;
}();
return []<std::size_t... S>(std::index_sequence<S...>) -> decltype(auto) {
return Example<double, enumArray[S]...>();
}(std::make_index_sequence<size>());
}
这样,您的for循环...
template<int size>
constexpr auto createObj(){
// implicitly constexpr ---v
constexpr auto enumArray = []{
Enums enum_array[size] = {};
for(int i = 0; i < size ; i++){
//fill array with e1 and e2
}
return enum_array;
}();
constexpr auto&& [...allEnums] = enumArray;
return Example<double, allEnums...>();
}
...与...相同
contours == None
for cnt in contours:
pass
不是容器。 for cnt in None:
pass
没有2或3或100个可以循环的元素。以下是None
None
您将其称为:
cv.findContours
我认为您可能已经交换了contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]] )
和_, contours = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
的顺序
无论如何,看起来_, contours
的返回值为contours, _