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. MiniDao & Freemarker & include

    minidao include - 国内版 Binghttps://cn.bing.com/search?q=minidao+include&qs=n&form=QBRE&sp ...

  2. iptables保存规则(ubuntu和centos)

    1.Ubuntu 首先,保存现有的规则: iptables-save > /etc/iptables.rules 然后新建一个bash脚本,并保存到/etc/network/if-pre-up. ...

  3. Vue-cli项目结构讲解

    |-- build // 项目构建(webpack)相关代码 | |-- build.js // 生产环境构建代码 | |-- check-version.js // 检查node.npm等版本 | ...

  4. ubuntu14.0.4安装kafka

    1. 下载 zookeeper-3.4.12 zookeeper download 2 配置Zookeeper 进入 zookeeper 的 conf 目录下,找到 zoo_sample.cfg 文件 ...

  5. Win10下载安装PostgreSQL 11.1

    下载地址:https://get.enterprisedb.com/postgresql/postgresql-11.1-1-windows-x64.exe Installation Director ...

  6. IIS+PHP本地开发环境配置

    打开Win7系统自带IIS.如图只要点击两下,CGI一定要勾选上!完成后打开浏览器输入127.0.0.1测试一下,如果能打开页面说明iis开启成功. 安装PHP.不同版本的PHP会有所不同,这里使用的 ...

  7. C#中Request.servervariables参数

    整理一下,我在asp.net下遍历的Request.servervariables这上集合,得出的所有参数如下: : Request.ServerVariables["ALL_HTTP&qu ...

  8. 【Leetcode_easy】997. Find the Town Judge

    problem 997. Find the Town Judge solution: class Solution { public: int findJudge(int N, vector<v ...

  9. 查看Mysql是否开启binlog

    show variables like 'log_bin';

  10. vue之项目打包部署生产环境前需要注意的问题!

    我们在本地写好代码,然后打包上线前需要修改相关的配置文件,否则项目无法正常运行,主要是路径不对,找不到相关文件. 1.修改config > index.js(如下图) 2.在build > ...