3

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

Code:

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()

How do i solve this? Thanks.

8
  • Did you trace your code!? What was the value for contours list? Commented Apr 16, 2014 at 4:06
  • the value is a very long array heres a snippet: [[ 995, 1018]], [[ 995, 1019]], [[ 995, 1020]], [[ 995, 1021]], Commented Apr 16, 2014 at 15:45
  • It seems you have 4 contours that each of them contains just a point!? Commented Apr 16, 2014 at 17:00
  • So how would get multiple points instead? Commented Apr 16, 2014 at 17:05
  • Can you share your image? Commented Apr 16, 2014 at 17:07

2 Answers 2

4

According to the findContour doc it returns two things:

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

Change the line to:

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

That may works!

Sign up to request clarification or add additional context in comments.

1 Comment

ah ofc. , missed that.
1

contours is actually a double-array. the outer one is the list of contours, the inner ones are the points of a single contour.

so, replace:

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

with:

for cnt in contours:
    vertices = cv2.boundingRect(cnt)

and drop the

contours = contours.h_next()

line (you probably confused cv2 with the old c-api/cv approach, which was using linked lists under the hood)

3 Comments

Hmmm... Still not working (same error). But thank you for informing me with the last comment.
you probably got a different err now.
I wish :( TypeError: points is not a numpy array, neither a scalar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.