官术网_书友最值得收藏!

Implementing k-NN in OpenCV

Using OpenCV, we can easily create a k-NN model via the cv2.ml.KNearest_create() function. Building the model then involves the following steps:

  1. Generate some training data.
  2. Create a k-NN object for a given number k.
  3. Find the k nearest neighbors of a new data point that we want to classify.
  4. Assign the class label of the new data point by majority vote.
  5. Plot the result.

We first import all the necessary modules: OpenCV for the k-NN algorithm, NumPy for data munging, and Matplotlib for plotting. If you are working in a Jupyter Notebook, don't forget to call the %matplotlib inline magic:

In [1]: import numpy as np
... import cv2
... import matplotlib.pyplot as plt
... %matplotlib inline
In [2]: plt.style.use('ggplot')
主站蜘蛛池模板: 永新县| 揭阳市| 汨罗市| 清原| 长白| 巴林左旗| 天门市| 宣城市| 昌吉市| 天长市| 肇州县| 乐昌市| 瑞丽市| 大连市| 双牌县| 大荔县| 深泽县| 淳安县| 新干县| 阿巴嘎旗| 济源市| 荥阳市| 蕲春县| 云梦县| 奉化市| 阿巴嘎旗| 拜城县| 桑日县| 富宁县| 道孚县| 通山县| 青浦区| 左云县| 金平| 永和县| 鹰潭市| 航空| 色达县| 扎兰屯市| 桂平市| 衢州市|