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

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')
主站蜘蛛池模板: 营口市| 宜章县| 清徐县| 祁连县| 东平县| 政和县| 宜都市| 鲁山县| 壤塘县| 林芝县| 鲜城| 巴马| 麻栗坡县| 都江堰市| 安康市| 门源| 中阳县| 洮南市| 龙井市| 将乐县| 贵德县| 京山县| 通州市| 阿拉善盟| 隆子县| 陆丰市| 无为县| 丹凤县| 句容市| 江华| 六枝特区| 建平县| 资阳市| 密山市| 汤阴县| 九寨沟县| 泸西县| 伊通| 偏关县| 明水县| 吴川市|