Ask Your Question
1

numpy.array setting an array element with a sequence

asked 2013-05-26 22:52:37 -0600

I.Zaytsev gravatar image

updated 2017-09-11 10:37:48 -0600

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-05-27 02:47:26 -0600

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

edit flag offensive delete link more

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 ( 2013-05-27 03:31:24 -0600 )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 ( 2013-05-27 19:45:54 -0600 )edit

ok! thanks :)

I.Zaytsev gravatar imageI.Zaytsev ( 2013-05-28 00:50:01 -0600 )edit

Question Tools

Stats

Asked: 2013-05-26 22:52:37 -0600

Seen: 3,569 times

Last updated: May 27 '13