cv2.findCountours Failure in Python OpenCV Error: Assertion failed

asked 2015-03-06 17:05:32 -0500

bell_zack gravatar image

Hello I am new to openCV and am having a problem to which I do not understand. I am trying to use:

contours, hierarchy = cv2.findContours(edges_canny,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

Every time I run it the following error is thrown:

OpenCV Error: Assertion failed (step[dims-1] == (size_t)CV_ELEM_SIZE(flags)) in create, file /home/zack/opencv-2.4.10/modules/core/src/matrix.cpp, line 236 Traceback (most recent call last): File "/home/zack/tutorials_opencv/object_tracking_4.py", line 181, in <module> contours, hierarchy = cv2.findContours(edges_canny,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cv2.error: /home/zack/opencv-2.4.10/modules/core/src/matrix.cpp:236: error: (-215) step[dims-1] == (size_t)CV_ELEM_SIZE(flags) in function create

Here is my code:

import cv2
import numpy as np

def nothing(x):
    pass

# the command is guvcview
# need to set the gain to make the color output not change so much
cap = cv2.VideoCapture(1)

# initializing color values
# brightness
b = 1
# contrast
c = 33
# saturation
s = 65
# hue
h = 90
# gain
g = 20
# gaussian blur value
blur_val = 7

# initializing threshold values
# upper threshold
upper_hue = 179
upper_sat = 255
upper_val = 255
# lower threshold
lower_hue = 132
lower_sat = 73
lower_val = 101

# ball example
# # upper threshold
# upper_hue = 179
# upper_sat = 255
# upper_val = 255
# # lower threshold
# lower_hue = 132
# lower_sat = 73
# lower_val = 101

# # blue example
# # upper threshold
# upper_hue = 130
# upper_sat = 255
# upper_val = 255
# # lower threshold
# lower_hue = 110
# lower_sat = 50
# lower_val = 50


# calibration image color switch and mask switch
cal_image_color = False
cal_mask = False

# define range of blue color in HSV
upper_thresh = np.array([upper_hue,upper_sat,upper_val])
lower_thresh = np.array([lower_hue,lower_sat,lower_val])


cv2.namedWindow('image')
cv2.namedWindow('mask')

# create trackbars for color changes
cv2.createTrackbar('Brightness','image',b,255,nothing)
cv2.createTrackbar('Contrast','image',c,255,nothing)
cv2.createTrackbar('Saturation','image',s,255,nothing)
cv2.createTrackbar('Hue','image',h,179,nothing)
cv2.createTrackbar('Gain','image',g,63,nothing)
cv2.createTrackbar('Blur','image',blur_val,50,nothing)

# trackbars for the threshold
cv2.createTrackbar('Upper Hue','mask',upper_hue,179,nothing)
cv2.createTrackbar('Upper Saturation','mask',upper_sat,255,nothing)
cv2.createTrackbar('Upper Value','mask',upper_val,255,nothing)
cv2.createTrackbar('Lower Hue','mask',lower_hue,179,nothing)
cv2.createTrackbar('Lower Saturation','mask',lower_sat,255,nothing)
cv2.createTrackbar('Lower Value','mask',lower_val,255,nothing)

# create switches to turn the trackbars on or off
switch = 'OFF : 0\nON : 1'
cv2.createTrackbar(switch,'image',0,1,nothing)
cv2.createTrackbar(switch,'mask',0,1,nothing)

# first time value
first_time = 0.0
# second time value
second_time = 0.0
# time difference
time_difference_milli = 0.0

# kernel for the eroding and dialation routines
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7))

while(1):

    # reading in the image
    is_read, read = cap.read()

    if is_read:

        # if the read was good getting the time difference to get the fps
        # setting the first time to second
        first_time = second_time

        # updating the second time
        second_time = cap.get(0)

        # updating the time difference
        time_difference_milli = second_time - first_time

        # checking if the image switch is at one allowing the image values to be adjusted
        cal_image_color = (cv2.getTrackbarPos(switch,'image') == 1)
        if cal_image_color:
            # get current positions of the trackbars
            b = (1.0 ...
(more)
edit retag flag offensive close merge delete

Comments

I tried to use the contours.py given in the samples/python2 directory and it is returning the same error. I think something may not be installed correctly but I am not sure.

bell_zack gravatar imagebell_zack ( 2015-03-07 13:54:44 -0500 )edit

I re-installed ubuntu 14.04.2 and installed opencv 2.4.11 and the findContours now works. I think that my first install was bad.

bell_zack gravatar imagebell_zack ( 2015-03-08 15:29:56 -0500 )edit