python - 为什么当我在单个轮廓上调用cv2.boundingRect()时,我的代码是否会引发 “TypeError: points is not a numpy array, neither a scalar”?

标签 python opencv anaconda

我试图写一个相当简单的机器人视觉系统功能的演示。我正在编写的程序应该在阈值图像中找到最大轮廓,然后跟踪过去100帧中最大轮廓的边界矩形的中心的路径。但是,当我调用cv2.boundingRect(bigCont)时,我看到了TypeError: points is not a numpy array, neither a scalar。我在Win7 SP1 64位上使用Python 2.7.9,Anaconda 2.2.0(64位)和OpenCV 2.4.9.1。我已经看过this threadthis one;但是,当函数实际上返回两个值时,这两者似乎都涉及将cv2.findContours设置为单个变量。我的代码已经将cv2.findContours设置为等于两个单独的变量,所以我不认为这是问题所在。涉及隔离最大轮廓中心的代码部分为:

    # find the largest contour in the thresholded image
    conts, _ = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    bigCont = []
    bigContSize = 0
    for cont in conts:
        contSize = cv2.contourArea(cont)
        if contSize > bigContSize:
            bigContSize = contSize
            bigCont = cont
    # find the center of the largest contour's bounding rectangle
    x,y,w,h = cv2.boundingRect(bigCont)
    centerX = x + (w / 2)
    centerY = y + (h / 2)
    contCenter = (centerX, centerY)

错误的完整回溯是:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)
  File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "D:/newVisionDemo.py", line 73, in <module>
    showHist(orig)
  File "D:/newVisionDemo.py", line 51, in showHist
    x,y,w,h = cv2.boundingRect(bigCont)
TypeError: points is not a numpy array, neither a scalar

作为第二个问题,这是基于我们之前为实际机器人代码编写的文件编写的。该API似乎表明cv2.boundingRect现在仅返回一个值,但并未完全解释该值代表什么。如果有人可以解释如何使用cv2.boundingRect的当前实现,将不胜感激。

另外,如果您需要查看更多原始代码,请随时告诉我。

更新:根据另一个团队成员的建议,我尝试将对cv2.boundingRect的调用更改为cv2.boundingRect(np.array(bigCont))。这引起了不同的追溯,我在下面包括了:
OpenCV Error: Assertion failed (points.checkVector(2) >= 0 && (points.depth() == CV_32F || points.depth() == CV_32S)) in cv::boundingRect, file ..\..\..\modules\imgproc\src\contours.cpp, line 1895
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)
  File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "D:/newVisionDemo.py", line 68, in <module>
    showHist(orig)
  File "D:/newVisionDemo.py", line 48, in showHist
    x,y,w,h = cv2.boundingRect(np.array(bigCont))
cv2.error: ..\..\..\modules\imgproc\src\contours.cpp:1895: error: (-215) points.checkVector(2) >= 0 && (points.depth() == CV_32F || points.depth() == CV_32S) in function cv::boundingRect

任何帮助将非常感激。提前致谢。

最佳答案

在尝试了阈值之后,我确定是因为没有轮廓而引发了错误-二进制图像是完全黑色的。通过仅尝试找到中心if bigContSize > 0并将contCenter设置为(0, 0)来解决此问题(因为稍后将使用它)。这个问题解决了,我现在转向困扰这个代码的下一个问题...

关于python - 为什么当我在单个轮廓上调用cv2.boundingRect()时,我的代码是否会引发 “TypeError: points is not a numpy array, neither a scalar”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30249719/

相关文章:

python - scikit-learn SelectPercentile TFIDF 数据特征缩减

c - 实现本地化 Otsu 二值化

python - TypeError:cv2_imshow()接受1个位置参数,但给出了2个:google colab cv相关问题

python - 使用 conda 将 python 更新到特定版本

Python 或 R! - 为指定的光栅文件绘制直方图

Python 将过滤级别记录到流中

python - a[0],a[1] = a[1],a[0]的过程中发生了什么?

c++ - 使用 Opencv 从 Macbook Pro iSight 捕获

python - Conda 虚拟环境在 Windows 下没有改变

python - Anaconda python ver5.3 在更新时永远挂起