OpenCV 3.4.1: KNearest function not found

Multi tool use
Multi tool use


OpenCV 3.4.1: KNearest function not found



I can see from the OpenCV 3.4.1 documentation (https://docs.opencv.org/3.4.1/dd/de1/classcv_1_1ml_1_1KNearest.html) that the KNearest function exists. This is exactly my version, as you can see below:


>>> import cv2
>>> cv2.__version__
'3.4.1'



However, when I run my python code it seems that it doesn't exist


knn = cv2.KNearest()
AttributeError: 'module' object has no attribute 'KNearest'



I am quite rookie on this. What have I missed? is this the correct function for such an OpenCV version?




1 Answer
1



Your usage is for the 2.x version


knn = cv2.KNearest() # OpenCv 2.x



Try this instead:


knn = cv2.ml.KNearest_create() # OpenCv 3.x



Read this python example from the documentation






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

JIzt6pcD NsAIy80pcLuK,lG7sBP HlN6yynb,MPXfSa7AadsK6uWE,rOgUpv
U,j5Ebm d i AO,Oq XBqxmSQ7wa6g,e mKeebLc9t N1k7 ufM

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications