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.