python opencv findContours() error cpp 197

Question:

Trying to use findContours() but keep getting a cpp:197 error (-210:Unsupported format or combination of formats)

I have used the same format in other files and it works fine. Not sure why it doesn’t work here.

full error:

Traceback (most recent call last):
  File "C:/Users/FreddyMac/PycharmProjects/TestProj/ballTrackingAbsDiff.py", line 33, in <module>
    cnts = cv2.findContours(th1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.error: OpenCV(4.4.0) C:UsersappveyorAppDataLocalTemp1pip-req-build-cff9bdsmopencvmodulesimgprocsrccontours.cpp:197: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl'

I checked the type of my image and is the correct ‘uint8’ type.

see code below.

import cv2
import imutils

vs = cv2.VideoCapture('ballsFlying.MP4')
while True:
    # read frame1, resize and convert to grayscale
    ret, frame1 = vs.read()
    if frame1 is None:
        break
    frame1 = imutils.resize(frame1, width=600)
    gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)

    # read frame2, resize and convert to grayscale
    ret2, frame2 = vs.read()
    if frame2 is None:
        break
    frame2 = imutils.resize(frame2, width=600)
    gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)

    # compute the difference between frames
    dist = cv2.absdiff(frame1, frame2)
    # blur image
    blurred = cv2.GaussianBlur(dist, (9, 9), 0)

    # global thresholding
    ret3, th1 = cv2.threshold(blurred, 85, 255, cv2.THRESH_BINARY)
    print(th1.dtype)

    cnts = cv2.findContours(th1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # other way to find contours = same error
    # hierarchy, contours = cv2.findContours(th1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    cv2.imshow('dist', frame1)
    cv2.imshow('thresh', th1)
    cv2.imshow('blurred', blurred)

    # show the frame to our screen
    key = cv2.waitKey(100) & 0xFF
    # if the 'q' key is pressed, stop the loop
    if key == ord("q"):
        break

# otherwise, release the camera
vs.release()
# close all windows
cv2.destroyAllWindows()

Asked By: fredo124

||

Answers:

Well, the error says the answer:

FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function

Since you are not using CV_RETR_FLOODFILL, your image should be CV_32SC1 means a single-channel image. findContours works with a single channel image.

Use gray images and the problem will be solved.

dist = cv2.absdiff(gray1, gray2)

Results:

th


enter image description here

Blur


enter image description here

Answered By: Ahx

This can also happen when the input image passed to the function is not uint8. Doing

array = np.array(array, np.uint8)

also might help.

Answered By: Ananda

Make sure th1 here should be binary array. You can check by printing th1 value or save in the form image using cv2.imwrite to check whether it is binary or not.

cnts = cv2.findContours(th1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Answered By: Muhammad Faizan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.