Search code examples
pythonopencvconvex-hull

Convex hull in python for given set of points?


I am trying to find convex hull in order to my get hands on opencv library and the code is:

import cv2
import numpy as np
a = [[0, 0], [1, 0], [0, 1], [1, 1], [0.5, 0.5]]
cv2.convexHull(np.array(a))

I am getting an error which is -

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    cv2.convexHull(np.array(a))
error: C:\projects\opencv-python\opencv\modules\imgproc\src\convhull.cpp:137: error: (-215) total >= 0 && (depth == 5 || depth == 4) in function cv::convexHull

How to fix it?


Solution

  • The catch is that the C++ code is expecting a "two-channel or three-channel floating-point array, where each element is a 2D/3D vector", which translates in Python/NumPy to a 3-dimensional array.

    import cv2
    import numpy as np
    a = [[0, 0], [1, 0], [0, 1], [1, 1], [0.5, 0.5]]
    cv2.convexHull(np.array(a,dtype='float32'))
    

    Source:- http://answers.opencv.org/question/252/cv2perspectivetransform-with-python/