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

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')
主站蜘蛛池模板: 页游| 双峰县| 察哈| 津南区| 青神县| 鄂伦春自治旗| 大兴区| 红河县| 松江区| 那坡县| 永济市| 耿马| 益阳市| 顺义区| 东丰县| 泗水县| 海盐县| 万山特区| 吉首市| 罗平县| 隆化县| 威海市| 红安县| 井研县| 敦煌市| 盘锦市| 九江县| 宜丰县| 津市市| 汕头市| 全州县| 扶余县| 响水县| 延吉市| 原平市| 深州市| 无极县| 利辛县| 德兴市| 高尔夫| 德庆县|