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

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')
主站蜘蛛池模板: 高清| 沾化县| 福安市| 汕头市| 台南县| 桦南县| 金湖县| 吉木萨尔县| 泰安市| 龙井市| 正安县| 阜宁县| 会同县| 互助| 黔西县| 迁西县| 岢岚县| 辛集市| 汾阳市| 肇州县| 安泽县| 雅江县| 梁平县| 龙川县| 雷波县| 蒙自县| 米易县| 广东省| 启东市| 博爱县| 黄山市| 女性| 石景山区| 天气| 特克斯县| 图木舒克市| 永春县| 固阳县| 商南县| 海盐县| 新竹县|