KNN Ref:
http://www.hudong.com/wiki/KNN
http://en.wikipedia.org/wiki/K-nearest_neighbor_algorithm
Example Ref: (C Language)
http://www.opencv.org.cn/index.php/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C
from ml import *
from highgui import *
from cv import *
if __name__ == '__main__':
color = CV_RGB(180, 120, 0)
K = 10
trainSampleCount = 300
rngState = cvRNG()
trainData = cvCreateMat(trainSampleCount, 2, CV_32FC1)
trainClasses = cvCreateMat(trainSampleCount, 1, CV_32FC1)
img = cvCreateImage(cvSize(500, 500), 8, 3)
sample = cvCreateMat(1, 2, CV_32FC1)
cvZero(img)
# form the training samples
trainData1 = cvGetRows(trainData, 0, 100)
colData1x = cvGetCol(trainData1, 0)
colData1y = cvGetCol(trainData1, 1)
cvRandArr(rngState, colData1x, CV_RAND_NORMAL, cvScalar(200), cvScalar(50))
cvRandArr(rngState, colData1y, CV_RAND_NORMAL, cvScalar(200), cvScalar(50))
trainData2 = cvGetRows(trainData, 100, 200)
colData2x = cvGetCol(trainData2, 0)
colData2y = cvGetCol(trainData2, 1)
cvRandArr(rngState, colData2x, CV_RAND_NORMAL, cvScalar(300), cvScalar(50))
cvRandArr(rngState, colData2y, CV_RAND_NORMAL, cvScalar(300), cvScalar(50))
trainData3 = cvGetRows(trainData, 200, 300)
colData3x = cvGetCol(trainData3, 0)
colData3y = cvGetCol(trainData3, 1)
cvRandArr(rngState, colData3x, CV_RAND_NORMAL, cvScalar(100), cvScalar(30))
cvRandArr(rngState, colData3y, CV_RAND_NORMAL, cvScalar(400), cvScalar(30))
trainClasses1 = cvGetRows(trainClasses, 0, 100)
cvSet(trainClasses1, cvScalar(1))
trainClasses2 = cvGetRows(trainClasses, 100, 200)
cvSet(trainClasses2, cvScalar(2))
trainClasses3 = cvGetRows(trainClasses, 200, 300)
cvSet(trainClasses3, cvScalar(3))
knn = CvKNearest(trainData, trainClasses, None, False, K)
nearests = cvCreateMat(1, K, CV_32FC1)
for i in range(0, img.height):
for j in range(0, img.width):
sample[0, 0] = float(j)
sample[0, 1] = float(i)
#response = knn.find_nearest(sample, K, None, 0.0, nearests, None)
response = knn.find_nearest(sample, K, None)
accuracy = 0
for k in range(0, K):
if nearests[0, k] == response:
accuracy += 1
color = CV_RGB(180, 120, 0)
if response == 1:
if accuracy > 5:
color = CV_RGB(180, 0, 0)
else:
color = CV_RGB(180, 100, 100)
elif response == 2:
if accuracy > 5:
color = CV_RGB(0, 180, 0)
else:
color = CV_RGB(100, 180, 100)
elif response == 3:
if accuracy > 5:
color = CV_RGB(0, 0, 180)
else:
color = CV_RGB(100, 100, 180)
cvSet2D(img, i, j, color)
try:
for i in range(0, 100):
pt = CvPoint()
pt.x = cvRound(trainData1[0, i * 2])
pt.y = cvRound(trainData1[0, i * 2 + 1])
cvCircle(img, pt, 2, CV_RGB(255, 0, 0), CV_FILLED)
pt.x = cvRound(trainData2[0, i * 2])
pt.y = cvRound(trainData2[0, i * 2 + 1])
cvCircle(img, pt, 2, CV_RGB(0, 255, 0), CV_FILLED)
pt.x = cvRound(trainData3[0, i * 2])
pt.y = cvRound(trainData3[0, i * 2 + 1])
cvCircle(img, pt, 2, CV_RGB(0, 0, 255), CV_FILLED)
except Exception, e:
print e
cvNamedWindow('result', 1)
cvShowImage('result', img)
cvWaitKey(0)
cvReleaseMat(trainClasses)
cvReleaseMat(trainData)
Issue:
1. knn.find_nearest(sample, K, None, 0.0, nearests, None)
Will cause Exception:
NotImplementedError: Wrong number of arguments for overloaded function
'CvKNearest_find_nearest'.
Waiting for solution:
http://tech.groups.yahoo.com/group/OpenCV/message/73337
2. 'for i in range(0, 100)' will cause out of bound error. Maybe relates with Issue 1.
- 大小: 22 KB
分享到:
相关推荐
Python版本的OpenCV库,也就是`opencv-python`,为Python程序员提供了方便的接口来利用这个功能丰富的库。在本案例中,我们讨论的是OpenCV的Python接口的4.10.0.84版本,它是一个经过编译的二进制包,文件名为`...
Python版本的OpenCV(即`opencv-python`)提供了丰富的接口,使得Python开发者能够轻松地利用这个库进行图像和视频操作。`opencv-python-4.5.5.64.tar.gz`是一个包含了OpenCV Python接口的特定版本4.5.5.64的源码或...
这个压缩包“opencv-python-4.10.0.82.tar.gz”包含了Python接口的OpenCV库版本4.10.0.82,是开发人员在Python环境中进行图像和视频处理的重要资源。 OpenCV-Python是OpenCV库的一个Python绑定,它使得Python程序员...
在给定的压缩包"opencv-python-4.5.5-cp311-cp311-win-amd64.zip"中,包含的是OpenCV的Python绑定版本4.5.5,专为Python 3.11编译,并适用于Windows操作系统上的AMD64架构。这个安装包使得开发者能够在Python环境中...
在给定的压缩包"opencv-python-3.1.0-cp34-cp34m-win-amd64.zip"中,包含的是OpenCV Python接口的3.1.0版本,专为32位或64位的Windows系统以及Python 3.4编译。`cp34m`表示这是针对Python 3.4的 ABI(应用程序二进制...
由于官网没有提供opencv_python‑3.4.7+contrib‑cp37‑cp37m‑win_amd64.whl的下载了,可以通过先下载opencv_python-3.4.7-cp37-cp37m-win_amd64....pip install --user opencv-contrib-python==3.4.7.28 两步实现
opencv_python-4.2.0.32-cp38-cp38-win32.whl 在线cmd命令pip3 install opencv_python 网速过慢也导致出错的问题。 操作方法:将文件下载后拷贝至Anaconda或者python安装目录下的\libs\site-packages文件夹下(没有...
《Python库OpenCV:探索计算机视觉的神器》 在当今数据驱动的世界中,计算机视觉作为一门重要的技术领域,正日益成为科研与开发的热点。OpenCV(Open Source Computer Vision Library)是一个开源的强大计算机视觉...
《OpenCV-Python 4.5.3.56:深度学习与计算机视觉的利器》 OpenCV(开源计算机视觉库)是计算机视觉和机器学习领域的重要工具,它提供了丰富的功能,用于处理图像和视频数据。在Python环境中,OpenCV通过`opencv-...
opencv_python_headless-4.6.0.66-cp36-abi3-win_amd64.whl 下载官网:https://pypi.org/project/opencv-python-headless/4.6.0.66/#files
opencv_python-3.4.0-cp36-cp36m-win_amd64.whl,适用于python3.6,window 64位。上传它是因为原下载链接打不开了。【opencv_python-3.4.0-cp36-cp36m-win_amd64.whl】,用于python安装opencv模块。
opencv-python-4.9.0.80-cp37-abi3-win-amd64.whl
该资源为opencv_python-4.10.0.84-cp37-abi3-win32.whl,欢迎下载使用哦!
opencv-python-4.8.0.76-cp37-abi3-win-amd64.whl
opencv_python-4.5.5-cp39-cp39-win_amd64.whl
在给定的压缩包文件"opencv-python-4.5.5.62.tar.gz"中,包含的是OpenCV的Python绑定版本4.5.5.62,这是一个重要的软件更新,提供了许多新功能、优化和错误修复。 OpenCV-Python是OpenCV库与Python语言的接口,使得...
opencv_python-4.5.5-cp37-cp37m-win_amd64.whl 适用于64位系统
该资源为opencv_python-4.10.0.82-cp37-abi3-win_amd64.whl,欢迎下载使用哦!
该资源为opencv_python-4.10.0.84-cp37-abi3-win_amd64.whl,欢迎下载使用哦!
opencv-python-4.10.0.84-cp37-abi3-win-amd64.whl