【问题标题】:c = max(cnts, key=cv2.contourArea) error in raspberry pi树莓派中的 c = max(cnts, key=cv2.contourArea) 错误
【发布时间】:2019-03-20 08:54:41
【问题描述】:

嗨,我想在树莓派中使用 opencv

但我有一个错误

Traceback(最近一次调用最后一次): 文件“testface.py”,第 76 行,在

c = max(cnts, key=cv2.contourArea)

cv2.error: OpenCV(4.0.0) /home/pi/opencv-4.0.0/modules/imgproc/src/shapeescr.cpp:272: error: (-215:Assertion failed) npoints >= 0 && (深度 == CV_32F || 深度 == CV_32S)在函数“contourArea”中

请看我的代码:

# import the necessary packages
from __future__ import print_function
from imutils.video import VideoStream
import argparse
import imutils
import time
import cv2
import RPi.GPIO as GPIO

# initialize GPIO
redLed = 21
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(redLed, GPIO.OUT)

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--picamera", type=int, default=-1,
	help="whether or not the Raspberry Pi camera should be used")
args = vars(ap.parse_args())

# initialize the video stream and allow the camera sensor to warmup
print("[INFO] waiting for camera to warmup...")
vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
time.sleep(2.0)

# define the lower and upper boundaries of the object
# to be detected in the HSV color space
colorLower = (24, 100, 100) 
colorUpper = (44, 255, 255) 

# Start with LED off
print("\n Starting..... ==> Press 'q' to quit Program \n")
GPIO.output(redLed, GPIO.LOW)
ledOn = False

# loop over the frames from the video stream
while True:
	# grab the next frame from the video stream, Invert 180o, resize the
	# frame, and convert it to the HSV color space
	frame = vs.read()
	frame = imutils.resize(frame, width=500)
	frame = imutils.rotate(frame, angle=180)
	hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

	# construct a mask for the obect color, then perform
	# a series of dilations and erosions to remove any small
	# blobs left in the mask
	mask = cv2.inRange(hsv, colorLower, colorUpper)
	mask = cv2.erode(mask, None, iterations=2)
	mask = cv2.dilate(mask, None, iterations=2)

	# find contours in the mask and initialize the current
	# (x, y) center of the object
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
		cv2.CHAIN_APPROX_SIMPLE)
	cnts = cnts[0] if imutils.is_cv2() else cnts[1]
	center = None
	#print("hey")

	if cnts is None:
		cnts = [0]

	# only proceed if at least one contour was found
	if len(cnts) > 0:
		# find the largest contour in the mask, then use
		# it to compute the minimum enclosing circle and
		# centroid
		c = max(cnts, key=cv2.contourArea)
		((x, y), radius) = cv2.minEnclosingCircle(c)
		M = cv2.moments(c)
		center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

		# only proceed if the radius meets a minimum size
		if radius > 10:
			# draw the circle and centroid on the frame,
			# then update the list of tracked points
			cv2.circle(frame, (int(x), int(y)), int(radius),
				(0, 255, 255), 2)
			cv2.circle(frame, center, 5, (0, 0, 255), -1)

			# if the led is not already on, turn the LED on
			if not ledOn:
				GPIO.output(redLed, GPIO.HIGH)
				ledOn = True
				print("fined")

	# if the object is not detected, turn the LED off
	elif ledOn:
		GPIO.output(redLed, GPIO.LOW)
		ledOn = False

	# show the frame to our screen
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF

	# if the 'q' key is pressed, stop the loop
	if key == ord("q"):
		break

# do a bit of cleanup
print("\n Exiting Program and cleanup stuff \n")
GPIO.cleanup()
cv2.destroyAllWindows()
vs.stop()

【问题讨论】:

    标签: python python-3.x opencv raspberry-pi3 raspbian


    【解决方案1】:

    看这里:http://answers.opencv.org/question/102465/error-calling-contourarea/

    也许你需要更换:

    cnts = [0]
    

    与:

    cnts = []
    

    所以你会得到:

    if cnts == None:
        cnts = []
    

    您的代码将是:

    # import the necessary packages
    from __future__ import print_function
    from imutils.video import VideoStream
    import argparse
    import imutils
    import time
    import cv2
    import RPi.GPIO as GPIO
    
    # initialize GPIO
    redLed = 21
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(redLed, GPIO.OUT)
    
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-p", "--picamera", type=int, default=-1,
    	help="whether or not the Raspberry Pi camera should be used")
    args = vars(ap.parse_args())
    
    # initialize the video stream and allow the camera sensor to warmup
    print("[INFO] waiting for camera to warmup...")
    vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
    time.sleep(2.0)
    
    # define the lower and upper boundaries of the object
    # to be detected in the HSV color space
    colorLower = (24, 100, 100) 
    colorUpper = (44, 255, 255) 
    
    # Start with LED off
    print("\n Starting..... ==> Press 'q' to quit Program \n")
    GPIO.output(redLed, GPIO.LOW)
    ledOn = False
    
    # loop over the frames from the video stream
    while True:
    	# grab the next frame from the video stream, Invert 180o, resize the
    	# frame, and convert it to the HSV color space
    	frame = vs.read()
    	frame = imutils.resize(frame, width=500)
    	frame = imutils.rotate(frame, angle=180)
    	hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    
    	# construct a mask for the obect color, then perform
    	# a series of dilations and erosions to remove any small
    	# blobs left in the mask
    	mask = cv2.inRange(hsv, colorLower, colorUpper)
    	mask = cv2.erode(mask, None, iterations=2)
    	mask = cv2.dilate(mask, None, iterations=2)
    
    	# find contours in the mask and initialize the current
    	# (x, y) center of the object
    	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
    		cv2.CHAIN_APPROX_SIMPLE)
    	cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    	center = None
    	#print("hey")
    
    	if cnts is None:
    		cnts = []
    
    	# only proceed if at least one contour was found
    	if len(cnts) > 0:
    		# find the largest contour in the mask, then use
    		# it to compute the minimum enclosing circle and
    		# centroid
    		c = max(cnts, key=cv2.contourArea)
    		((x, y), radius) = cv2.minEnclosingCircle(c)
    		M = cv2.moments(c)
    		center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
    
    		# only proceed if the radius meets a minimum size
    		if radius > 10:
    			# draw the circle and centroid on the frame,
    			# then update the list of tracked points
    			cv2.circle(frame, (int(x), int(y)), int(radius),
    				(0, 255, 255), 2)
    			cv2.circle(frame, center, 5, (0, 0, 255), -1)
    
    			# if the led is not already on, turn the LED on
    			if not ledOn:
    				GPIO.output(redLed, GPIO.HIGH)
    				ledOn = True
    				print("fined")
    
    	# if the object is not detected, turn the LED off
    	elif ledOn:
    		GPIO.output(redLed, GPIO.LOW)
    		ledOn = False
    
    	# show the frame to our screen
    	cv2.imshow("Frame", frame)
    	key = cv2.waitKey(1) & 0xFF
    
    	# if the 'q' key is pressed, stop the loop
    	if key == ord("q"):
    		break
    
    # do a bit of cleanup
    print("\n Exiting Program and cleanup stuff \n")
    GPIO.cleanup()
    cv2.destroyAllWindows()
    vs.stop()

    我觉得可以的

    【讨论】:

    • 因为您将需要该列表,但您给了他们一个“空”列表(其中只有一个 0)
    • 嗨,我将 : cnts = [0] 替换为 cnts = [] 。但得到同样的错误
    • 当 cnts 得到 1 并运行 if len(cnts) > 0: 我得到同样的错误: c = max(cnts, key=cv2.contourArea) cv2.error: OpenCV(4.0.0) / home/pi/opencv-4.0.0/modules/imgproc/src/shapeescr.cpp:272: 错误: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function '轮廓区域'
    【解决方案2】:

    改变

    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    

    cnts = cnts[1] if imutils.is_cv2() else cnts[0]
    

    Duplicate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 2022-06-20
      • 1970-01-01
      • 2015-06-17
      • 2018-07-08
      相关资源
      最近更新 更多