Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.kmeans error with parameters -python

I am somewhat new to cv2 and I am having the following error

First, I obtain the ORB descriptors of an image with the following code:

import cv2
img = cv2.imread('messi.jpg',0) 
orb = cv2.ORB_create()
cv2.ocl.setUseOpenCL(False)
kp, des = orb.detectAndCompute(img, None)

The function detected 500 keypoints, each one with a dimentionality of 32. If I do:

des.shape

I get:

(500L,32L) 

Additionally, I want to get the centroids of the descriptors using kmeans. This is my code:

iterations = 10
epsilon = 1.0
k = 64
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, iterations, epsilon)
compactness, labels, centers = cv2.kmeans(des, k ,None ,criteria, iterations, cv2.KMEANS_RANDOM_CENTERS)

The error I get is:

 error: C:\builds\master_PackSlaveAddon-win64-vc12
 static\opencv\modules\core\src\kmeans.cpp:230: error: (-215) data0.dims <= 2
 && type == CV_32F && K > 0 in function cv::kmeans

I have seen threads on this error for C++, and it was usually a problem with the image dimensionality or the image type, but in python I am not sure what it means.

Any help?

like image 578
Alejandro Simkievich Avatar asked Jul 19 '26 05:07

Alejandro Simkievich


1 Answers

the reason for the error is that the first parameter of the cv2.kmeans function must be an array of data type32

Therefore, introducing the following line of code solves the problem:

des = np.float32(des)

A number of examples on how to use the function are available here:

http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_ml/py_kmeans/py_kmeans_opencv/py_kmeans_opencv.html

like image 53
Alejandro Simkievich Avatar answered Jul 20 '26 17:07

Alejandro Simkievich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!