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

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')
主站蜘蛛池模板: 海林市| 历史| 石嘴山市| 沁水县| 宁武县| 额尔古纳市| 关岭| 南阳市| 会理县| 桐城市| 江达县| 多伦县| 静安区| 项城市| 小金县| 板桥市| 富裕县| 洛阳市| 澎湖县| 五峰| 吉林市| 吴江市| 玉山县| 罗平县| 香格里拉县| 甘德县| 霍城县| 民丰县| 化州市| 汪清县| 伽师县| 蕉岭县| 庆安县| 内丘县| 博白县| 枣强县| 富顺县| 吉首市| 鹤山市| 定兴县| 渑池县|