KNN is one of the most widely used classification algorithms that is used in machine learning. To know more about the KNN algorithm read here KNN algorithm
Today we are going to see how we can implement this algorithm in OpenCV and how we can visualize the results in 2D plane showing different features of classes we have in our training data.
Let’s consider two classes for our code. We generate 20 random data points belonging to the 2 classes using a random generator. The training points will be either of the ‘magenta’ class or ‘yellow’ class. The magenta is drawn as square and the label for magenta is 1 similarly yellow is drawn as a circle and is labelled as 0.
Now consider an unknown new data point, our KNN classifier will label that data point either 0 or 1 depending on its features and the number of neighbours that are defined by us.
Label of the unknown data - [[1.]] Nearest neighbors - [[1. 1. 0. 1. 1.]] Distance of each neighbor - [[ 1. 65. 130. 173. 245.]]
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Introduction to OpenCV
- OpenCV - Overview
- OpenCV | Displaying an Image
- Background subtraction - OpenCV
- OpenCV | Loading Video
- OpenCV | Saving an Image
- Filter Color with OpenCV
- Gun Detection using Python-OpenCV
- Set up Opencv with anaconda environment
- OCR of Handwritten digits | OpenCV
- Image Inpainting using OpenCV
- Histograms Equalization in OpenCV
- Python OpenCV: Meanshift
- OpenCV: Segmentation using Thresholding
- OpenCV Python Tutorial
- Saving a Video using OpenCV
- OCR of English alphabets in Python OpenCV
- Detect an object with OpenCV-Python
- Python OpenCV | cv2.putText() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.