python版本:3.6.5

opencv版本:3.2.0

使用的是jupyter notebook

源代码如下:

import cv2
import numpy as np
import matplotlib.pyplot as plt %matplotlib inline # Feature set containing (x,y) values of 25 known/training data
trainData = np.random.randint(0,100,(25,2)).astype(np.float32) # Labels each one either Red or Blue with numbers 0 and 1
responses = np.random.randint(0,2,(25,1)).astype(np.float32) # Take Red families and plot them
red = trainData[responses.ravel()==0]
plt.scatter(red[:,0],red[:,1],80,'r','^') # Take Blue families and plot them
blue = trainData[responses.ravel()==1]
plt.scatter(blue[:,0],blue[:,1],80,'b','s') # plt.show() newcomer = np.random.randint(0,100,(1,2)).astype(np.float32)
plt.scatter(newcomer[:,0],newcomer[:,1],80,'g','o') knn = cv2.KNearest_create() knn.train(trainData,cv2.ml.ROW_SAMPLE,responses)
ret, results, neighbours ,dist = knn.findNearest(newcomer, 3) print ("result: ", results,"\n")
print ("neighbours: ", neighbours,"\n")
print ("distance: ", dist) plt.show()

出现的错误:

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-18-35bac89cb177> in <module>()
18 plt.scatter(newcomer[:,0],newcomer[:,1],80,'g','o')
19
---> 20 knn = cv2.KNearest_create()
21
22 knn.train(trainData,cv2.ml.ROW_SAMPLE,responses) AttributeError: module 'cv2' has no attribute 'KNearest_create'

解决方法:

opencv官方教程中的是opencv2的版本

在3中需要使用

cv2.ml.KNearest_create()

参考链接:https://stackoverflow.com/questions/34247502/cv2-ml-knearest-object-has-no-attribute-find-nearest

module 'cv2' has no attribute 'KNearest_create'的更多相关文章

  1. AttributeError: module 'cv2' has no attribute 'SIFT'解决总结

    AttributeError: module 'cv2' has no attribute 'SIFT' 遇到该问题时,网友多是建议补个包,即pip install opencv-contrib-py ...

  2. OpenCV 学习笔记 05 人脸检测和识别 AttributeError: module 'cv2' has no attribute 'face'

    1 环境设置: win10 python 3.6.8 opencv 4.0.1 2 尝试的方法 在学习人脸识别中,遇到了没有 cv2 中没有 face 属性.在网上找了几个方法,均没有成功解决掉该问题 ...

  3. AttributeError: 'module' object has no attribute 'face'

    报错 raceback (most recent call last): File "D:/work/python/face_ai/predict.py", line 41, in ...

  4. python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法?

    python中引入包的时候报错:import unittestimport smtplibimport timeimport osimport sysimp.reload(sys)sys.setdef ...

  5. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  6. attributeError:'module' object has no attribute ** 解决办法

    写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...

  7. AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier'

    /*************************************************************************** * AttributeError: 'modu ...

  8. 【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe' - IronPython 2.7

    最近在开发微信公众号,有一个自定义消息回复的需求 比如用户:麻烦帮我查询一下北京的天气? 系统回复:北京天气,晴,-℃... 这时候需要根据关键字[北京][天气],分词匹配需要执行的操作,然后去调用天 ...

  9. AttributeError: 'module' object has no attribute 'Thread'

    $ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last):  File "th ...

随机推荐

  1. Android中jsoup的混淆规则【转】

    Android中jsoup的混淆规则版权声明:转载必须注明本文转自严振杰的博客:http://blog.yanzhenjie.com 说实话这篇文章的标题和内容我觉得很水,所以读者们要是也觉得这篇文章 ...

  2. ionic生命周期函数

    Ionic4中的生命周期函数和angualr7基本是一样的,下面我们看看Ionic4中的生命周期函数,以及生命周期函数的用法. Ionic4中内置的生命周期函数: ionViewWillEnter — ...

  3. Windows下安装RabbitMQ3.6.5

    1.安装erlang 网址:http://www.erlang.org/ 下载exe文件安装即可 2.安装RabbitmQ 下载地址:http://www.rabbitmq.com/download. ...

  4. CentOS7下搭建Redis主从复制

    (1).实验环境 youxi1 192.168.1.6 Master服务器 youxi2 192.168.1.7 Slave服务器 (2).实验 1)两台服务器上yum安装Redis,启动并设置开机自 ...

  5. Python手册 3.7

    Python手册 3.7 下载地址:https://pan.baidu.com/s/1dPzwwP3ehnyLUNWTsB2QJg 关注微信公众号获取提取码: 输入:py99   获取提取码

  6. Ideal设置编码格式

    file-------settings-------file Encodings

  7. mybatis typeHandler类型转换器

    typeHandler类型转换器 在JDBC中,需要在PreparedStatement对象中设置那些已经预编译过的SQL语句的参数.执行SQL后,会通过ResultSet对象获取得到数据库的数据,而 ...

  8. 改进初学者的PID-介绍

    最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...

  9. Bmp格式图片与16进制的互相转换简解 Python

    BMP TO HEX 首先介绍Github上一个简单的Bmp转成16进制的py: https://github.com/robertgallup/bmp2hex 网上这种例子很多.思路也简单:将bmp ...

  10. nodejs应用转换png,jpg,gif为webp图片格式

    本博客列表缩略图在支持webp格式的浏览器下,使用的是webp格式图片,不支持webp图片下使用的是原图片(如png,gif,jpg等) webp使用指南,请参考 https://www.imqian ...