¿Cómo usar `cv2.findContours` en diferentes versiones de OpenCV?

Estoy tratando de usar OpenCV con Python para detectar cuadrados en un video en vivo desde una cámara Raspberry Pi. Sin embargo, las funciones cv2.GaussianBlur y cv2.Canny en el código a continuación están causando el siguiente error: “El error TypeError: numpy.ndarray ‘no se puede llamar” .

Parece que no puedo resolver el error. Cualquier ayuda es apreciada.

Código tomado de https://www.pyimagesearch.com/2015/05/04/target-acquired-finding-targets-in-drone-and-quadcopter-video-streams-using-python-and-opencv/#comment- 446639

 import cv2 # load the video camera = cv2.VideoCapture(0) # keep looping while True: # grab the current frame and initialize the status text (grabbed, frame) = camera.read() status = "No Targets" # check to see if we have reached the end of the # video if not grabbed: break # convert the frame to grayscale, blur it, and detect edges gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (7, 7), 0) edged = cv2.Canny(blurred, 50, 150) # find contours in the edge map (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # loop over the contours for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.01 * peri, True) # ensure that the approximated contour is "roughly" rectangular if len(approx) >= 4 and len(approx)  25 and h > 25 keepSolidity = solidity > 0.9 keepAspectRatio = aspectRatio >= 0.8 and aspectRatio <= 1.2 # ensure that the contour passes all our tests if keepDims and keepSolidity and keepAspectRatio: # draw an outline around the target and update the status # text cv2.drawContours(frame, [approx], -1, (0, 0, 255), 4) status = "Target(s) Acquired" # draw the status text on the frame cv2.putText(frame, status, (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # show the frame and record if a key is pressed cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the 'q' key is pressed, stop the loop if key == ord("q"): break # cleanup the camera and close any open windows camera.release() cv2.destroyAllWindows() 

Una alternativa para trabajar con 2.x 、 3.x 、 4.x es:

 cnts, hiers = cv2.findContours(...)[-2:] 

Darse cuenta:

cv2.findContours ha cambiado desde OpenCV 3.x , pero en OpenCV 4.0 vuelve a cambiar !!!

En OpenCV 3.4:

 findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy 

introduzca la descripción de la imagen aquí

En OpenCV 4.0:

 findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy 

introduzca la descripción de la imagen aquí