3926

Opencv TypeError: points is not a numpy array, neither a scalar

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.

Answer1:

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!

Answer2:

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)

Recommend

  • How do I program frames per second
  • Squaring introduces a lot of noise when doing Sobel operator using filter2D (OpenCV)
  • Detect angle and rotate an image in Python [closed]
  • OpenCV Python Error: error: (-215) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psr
  • Import a VB6 structure into C#
  • Can't get VideoCapture property as the property identifier are not defined
  • par(xpd=NA) does not work when axes=F in R plot
  • OpenCV's video capture not returning an image
  • Error while trying to upload file using kairos
  • custom_annotation with npc coordinates in ggplot2
  • Can't open HTTP stream: Error opening file (../cap_ffmpeg_impl.hpp:529)
  • What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and rep
  • Find 5 consecutive numbers in numpy array by row, ignore duplicates
  • Put value at centre of bins for histogram
  • vectorized indexing/slicing in numpy/scipy?
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • Python ImageIO Gif Set Delay Between Frames
  • Enabling DTD support in Sql Server
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • Center align outputs in ipython notebook
  • python script hangs on input method when running spark
  • Make new pandas columns based on pipe-delimited column with possible repeats
  • How to generate and display a QR Code in ionic 2
  • pyodbc doesn't report sql server error
  • Python urlparse: small issue
  • How reduce the height of an mschart by breaking up the y-axis
  • Check if a string to interpolate provides expected placeholders
  • Matplotlib draw Spline from multiple points
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • Traverse Array and Display in markup
  • Python: how to group similar lists together in a list of lists?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • Busy indicator not showing up in wpf window [duplicate]