【发布时间】:2018-08-30 11:27:33
【问题描述】:
我有以下 Python 代码在标题中引发错误:
# calculate number of all ROI pixels inside defined(hsv -) color range
pts2 = cv2.findNonZero(mask_final)
non_zero_pixel = int(len(pts2))
这里也一样:
# calculate number of all ROI pixels inside defined(bgr -) color range "black"
pts = cv2.findNonZero(mask_black)
black_pixel = int(len(pts))
我从它的 C++ 版本中翻译了这段代码,它确实可以正常工作:
// Calculate number of all ROI pixels inside defined (hsv-)color range
vector<Point> pts2;
findNonZero(mask_final, pts2);
double non_zero_pixel = static_cast<int>(pts2.size());
我不知道为什么 Python 版本会出现问题,而 C++ 版本不会。
有什么想法吗?
编辑:异常详情:
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-119-f76eaba94311> in <module>()
28
29 if __name__ == "__main__":
---> 30 main() # pass the list of arguments from the command line
<ipython-input-119-f76eaba94311> in main()
16
17 # detect screws
---> 18 detectScrews(img)
19
20 for j in range(len(screw_radiuses)):
<ipython-input-118-4c225e9bc794> in detectScrews(img)
80 # calculate number of all ROI pixels inside defined(bgr -) color range "black"
81 pts = cv2.findNonZero(mask_black)
---> 82 black_pixel = int(len(pts))
83
84 # if number of black pixel is lower than threshold
TypeError: object of type 'NoneType' has no len()
【问题讨论】:
-
你检查过文档,看看是什么情况导致
cv2.findNonZero(mask_black)返回无? -
为什么 C++ 版本不这样做?我在同一个图像上运行它。 Python 一个失败,C++ 一个通过。这很奇怪。
-
因为函数在返回的内容和时间方面可能有不同的行为。同样,文档可能会在这里给出答案。
-
我试图在我的回答中回答你的问题。