First time here? Check out the FAQ!

Ask Your Question
1

numpy.array setting an array element with a sequence

asked May 27 '13

I.Zaytsev gravatar image

updated Sep 11 '17

HellO! I am russian student and I began to study OpenCV :) I write this code:

import cv2
import numpy

def eye_detection():
    src = cv2.imread("E:\\Ilya\\PythonProjects\\eye.jpg")
    if src == None:
        print 'Do not open image!'
        return -1

    gray = cv2.cvtColor(~src, cv2.cv.CV_BGR2GRAY)
    cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY, gray)

    contours = cv2.findContours(gray.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    #cv2.imwrite(str("E:\\Ilya\\PythonProjects\\") + str("1") + ".jpg", gray)
    #print(contours)
    numpy.array(contours, dtype=numpy.float)
    cv2.drawContours(gray, cv2.cv.Scalar(contours), -1, cv2.cv.RGB(255,255,255), -1)
if __name__ == "__main__":
    eye_detection()

If I compile this script:

line 16, in eye_detection
    numpy.array(contours, dtype=numpy.float)
ValueError: setting an array element with a sequence.

contours contains(print(contours)):

([array([[[344, 254]]]), array([[[194, 243]],

       [[194, 244]],

       [[195, 244]],

       [[196, 244]],

       [[197, 244]],

       [[198, 244]],

       [[199, 244]],

       [[200, 244]],

       [[201, 244]],

       [[202, 244]],

       [[203, 244]],

       [[204, 244]],

       [[205, 244]],

       [[204, 244]],

       [[203, 244]],

       [[202, 244]],

       [[201, 243]],

       [[200, 243]],

       [[199, 243]],

       [[198, 243]],

       [[197, 243]],

       [[196, 243]],

       [[195, 243]]]), array([[[217, 242]], etc.

how to solve this problem?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered May 27 '13

Abid Rahman K gravatar image

Hi,

To draw contours, you need not do like this. Once you got the contours from cv2.findContours() function, you can directly pass that contours to cv2.drawContours() function.

ie the data to be sent into cv2.drawContours() function is a list. cv2.findContours() already returns a list.

You can find more details on contours here : http://opencvpython.blogspot.com/2012/06/hi-this-article-is-tutorial-which-try.html

Preview: (hide)

Comments

thanks! it's work:
contours, hierarchy = cv2.findContours(gray.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(gray, contours, -1, cv2.cv.RGB(255,255,255), -1)

I.Zaytsev gravatar imageI.Zaytsev (May 27 '13)edit
1

You don't need cv2.cv.RGB(255,255,255), those days are gone. Now you can simply pass a tuple(255,255,255)`

Abid Rahman K gravatar imageAbid Rahman K (May 28 '13)edit

ok! thanks :)

I.Zaytsev gravatar imageI.Zaytsev (May 28 '13)edit

Question Tools

Stats

Asked: May 27 '13

Seen: 3,615 times

Last updated: May 27 '13