I have a set of twelve shapes and I'm trying to determine the shape of a query image with the my database For convinience I have done all the preprocessing in MATLAB and have stored the shape contours, as binary image separately for training and testing prupose
I have done the Feature extraction and Implementation phase in Python In the training phase, I read the binary image, extract HU MOMETS (7 Vector) as features for all the train images(285 in total) So my samples (train samples) dimension is [285, 7] My responses dimension is [285]
Now I follow a similar strategy for testing as well. Test images consititue 541 in total Read image -> extract hu moments -> feed it to knn.find_nearest Dimension of the test moments is [1, 7]
The problem that I'm facing here is, irrespective of the test image that I give, I'm getting a return value in the knn.find_nearest as 76. It is consistently giving the same value for every image
What have I done to debug
- Checked with code to ensure if the dimens that I'm providing to KNN is correct
- Checked the Hu-moments values in matlab as well, and found nearly similar with opencv
- Also made sure that hu-moments for test image are computing properly
- Tested with changing the k value (when k=1 retval is 76, when k=3 retval is 75)
I don't know what I have done wrong here. please help
import os
import sys
import numpy
import cv2
import cv2.cv as cv
import xlwt
def main():
pth = sys.argv[1]
lsfolds = os.listdir(pth)
files_path = []
all_moments = numpy.empty((0,7))
train_samples = []
responses = []
cnt=0
for di in lsfolds:
img_path = os.path.join(pth,di)
lsfiles = os.listdir(img_path)
for ls in lsfiles:
comp_path = os.path.join(img_path,ls)
files_path.append(comp_path)
if (comp_path[-4:] == 's.db'):
continue
img_bin = cv2.imread(comp_path,-1)
#cv2.imshow('Image', img_bin)
#cv2.waitKey()
#################### Moments as Feature ###############################
moments = cv2.moments(img_bin,1)
hu_moments = cv2.HuMoments(moments)
hu_moments = hu_moments.reshape((1,7))
all_moments = numpy.append(all_moments,hu_moments,0)
train_samples.append(comp_path)
responses.append(int(cnt))
########################################################################
cnt += 1
responses = numpy.float32(responses)
all_moments = numpy.float32(all_moments)
################## KNN #####################################
knn_train_eng = cv2.KNearest()
knn_train_eng.train(all_moments,responses)
#######################################Testing######################################
timg_pth = sys.argv[2]
tfolds = os.listdir(timg_pth)
wb = xlwt.Workbook()
ws = wb.add_sheet('Test Results')
c=0
for tdi in tfolds:
timg_dir = os.path.join(timg_pth,tdi)
tfiles = os.listdir(timg_dir)
for fl in tfiles:
timg_path = os.path.join(timg_dir,fl)
if (timg_path[-4:] == 's.db'):
continue
timg = cv2.imread(timg_path,-1)
timg_bin = timg;
#cv2.imshow('test_bin',timg_bin)
tmoments = cv2.moments(timg_bin)
thu_moments = cv2.HuMoments(tmoments)
thu_moments = thu_moments.reshape((1,7))
thu_moments = numpy.float32(thu_moments)
retval, results, neigh_resp, dists = knn_train_eng.find_nearest(thu_moments,1) # Predict using KNN
res,dist = int(results[0][0]),dists[0][0]
op_answ = str(int((results[0][0])))
print op_answ
print train_samples[int(op_answ)]
op = cv2.imread(train_samples[int(op_answ)])
c+=1
cv2.destroyAllWindows()
##############################################################################
cv2.destroyAllWindows()
if __name__ == '__main__':
main()