OpenCV TypeError:点不是numpy数组,也不是标量 [英] Opencv TypeError: points is not a numpy array, neither a scalar

查看:345
本文介绍了OpenCV TypeError:点不是numpy数组,也不是标量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有这段代码可以检测背景变化并将其装箱.运行代码时,出现此错误:

Basically, I have this code that detects changes in background and boxed them. When I run the code, I get this error:

Traceback (most recent call last):
  File "cam2.py", line 28, in <module>
    vertices = cv2.boundingRect(list(contours))
TypeError: points is not a numpy array, neither a scalar

代码:

import cv2
import numpy as np

c = cv2.VideoCapture(0)
_,f = c.read()

avg1 = np.float32(f)

while(1):
    _,f = c.read()

    cv2.accumulateWeighted(f,avg1,0.1)

    res1 = cv2.convertScaleAbs(avg1)

    absdiff = cv2.absdiff(f,res1)

    graydiff = cv2.cvtColor(absdiff, cv2.COLOR_BGR2GRAY)

    retval, mask = cv2.threshold(graydiff, 50,255,cv2.THRESH_BINARY) 

    mask = cv2.dilate(mask, None, 18)
    mask = cv2.erode(mask, None, 10) 

    contours = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) #not right

    while contours:
        vertices = cv2.boundingRect(list(contours))
        interest = vertices

        point1 = (interest[0], interest[1])

        point2 = (interest[0] + interest[2], interest[1] + interest[3])
        cv2.rectangle(f, point1, point2, cv2.RGB(255,0,0), 1)
        cv2.rectangle(mask, point1, point2, cv2.RGB(255,255,255), 1)

        contours = contours.h_next()

    cv2.imshow('mask',mask)

    cv2.imshow('img',f)
    cv2.imshow('avg1',res1)

    k = cv2.waitKey(20)

    if k == 27:
        break

cv2.destroyAllWindows()
c.release()

我该如何解决?谢谢.

推荐答案

根据

According to the findContour doc it returns two things:

cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) → contours, hierarchy

将行更改为:

contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) 

那可能有用!

这篇关于OpenCV TypeError:点不是numpy数组,也不是标量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆