Python OpenCV3 KNN Algorithm

本文详细介绍了K-最近邻(KNN)分类算法的基本原理,通过实例解释了如何使用KNN进行分类,并展示了在Python中实现KNN的具体步骤。此外,文章还比较了KNN与BF算法的异同,指出KNN适用于稀有事件分类,尤其适合解决多酚型问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Foreword

Next, i’ll introduce a classification algoriithm—KNN,and of course i’ll show you how to use it in Python.

What’s the KNN?

KNN is the abbreviation of K-Nearest Neighbor .This algorithm is not only a relatively mature in theory ,but also one of the simplest machine learning algorithm.Next,i’ll give you an exanple to help you understang the idea of this algorithm.
KNN
Which kind of green circle in the above figure should be divided into ?Red triangle or blue square?In KNN algorithm, if k = 3, because the proportion of red triangle is two-thirds, the green circle is divided into red triangle.If k=5,because the proportion of blue square is three-fifths,the green circle is divided into blue square.If k=4,the green circle will be divided into red triangle because the shorter the distance, the greater the weight.

Library

pip install opencv-python

Python code

import cv2
# 按照灰度图像的方式读入两幅图片
img1 = cv2.imread("./8.jpg", cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread("./9.jpg", cv2.IMREAD_GRAYSCALE)
# 创建ORB特征检测器和描述符
orb = cv2.ORB_create()
# 对两幅图像检测特征和描述符
keypoint1, descriptor1 = orb.detectAndCompute(img1, None)
keypoint2, descriptor2 = orb.detectAndCompute(img2, None)

# 获得一个暴力匹配器的对象
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
maches = bf.knnMatch(descriptor1, descriptor2, k=1)
# 画出匹配项
img3 = cv2.drawMatchesKnn(img1, keypoint1, img2, keypoint2, maches[0:70], img2, flags=2)

cv2.imshow("KNN", img3)
cv2.waitKey(0)
cv2.destroyAllWindows()

Display

The result of KNN algorithm is almost the same as that of BF algorithm,but KNN runs longer.It’s suitable for rare events classification,especially for polyphenol problems.

在这里插入图片描述

In the next blog, i’ll introduce a faster classification algorithm—FLANN.

我希望我这样一本正经地做出要离开的样子,会引起你的注意。

### Python OpenCV AKAZE 特征匹配 示例 为了在Python中使用OpenCV执行AKAZE特征匹配,需确保已安装`opencv-python-headless`或`opencv-python`库[^1]。 下面是一个完整的AKAZE特征检测与描述以及基于FLANN的快速最近邻搜索来进行特征匹配的例子: ```python import cv2 import matplotlib.pyplot as plt def akaze_match(image_path_1, image_path_2): # 加载图像并转换为灰度模式 img1 = cv2.imread(image_path_1, cv2.IMREAD_GRAYSCALE) img2 = cv2.imread(image_path_2, cv2.IMREAD_GRAYSCALE) # 初始化AKAZE检测器 akaze = cv2.AKAZE_create() # 使用AKAZE找到关键点和描述符 kp1, des1 = akaze.detectAndCompute(img1, None) kp2, des2 = akaze.detectAndCompute(img2, None) # FLANN参数配置 index_params = dict(algorithm=6, table_number=6, # 用于LSH算法中的哈希表数量 key_size=12, # 单个哈希函数使用的位数 multi_probe_level=1) # 多探针级别 search_params = dict(checks=50) # KNN查询时检查次数越多越精确但是速度会变慢 flann = cv2.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(des1, des2, k=2) good_matches = [] for m, n in matches: if m.distance < 0.7 * n.distance: good_matches.append(m) result_img = cv2.drawMatches( img1, kp1, img2, kp2, good_matches[:10], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) plt.figure(figsize=(10, 8)) plt.imshow(result_img), plt.title("AKAZE Matches"), plt.axis(&#39;off&#39;) plt.show() if __name__ == "__main__": akaze_match(&#39;image1.jpg&#39;, &#39;image2.jpg&#39;) # 替换为实际图片路径 ``` 此脚本展示了如何加载两幅图像,利用AKAZE提取它们的关键点和描述子,并通过FLANN方法寻找最佳匹配对。最后绘制前十个最好的匹配结果以便可视化查看效果[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值