如何在opencv中查找集群索引

时间:2016-09-30 14:47:40

标签: opencv

我想使用OpenCV使用描述包,所以,最后,我想找到SampleImage的每个描述符的集群索引,但我不知道如何找到它,BOWImgDescriptorExtractor.compute()只返回一个大小为dictronery的数组,但我希望它是一个大小为SIFT提取的描述符的数组

sift = cv2.SIFT(100, contrastThreshold=0.005, edgeThreshold=4, sigma=1.4)

dictionarySize = 20

BOW = cv2.BOWKMeansTrainer(dictionarySize)

imagepath = 'sampleimage/bow/1.jpg';
imagepath2 = 'sampleimage/bow/2.jpg';
imagepath3 = 'sampleimage/bow/3.jpg';
imagepath4 = 'sampleimage/bow/4.jpg';
imagepath5 = 'sampleimage/bow/5.jpg';

# compute the images
image = cv2.imread(imagepath)
gray = cv2.cvtColor(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
kp, dsc = sift.detectAndCompute(gray, None)
BOW.add(dsc)


image = cv2.imread(imagepath2)
gray = cv2.cvtColor(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
kp, dsc = sift.detectAndCompute(gray, None)
BOW.add(dsc)



image = cv2.imread(imagepath3)
gray = cv2.cvtColor(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
kp, dsc = sift.detectAndCompute(gray, None)
BOW.add(dsc)


image = cv2.imread(imagepath4)
gray = cv2.cvtColor(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
kp, dsc = sift.detectAndCompute(gray, None)
BOW.add(dsc)


image = cv2.imread(imagepath5)
gray = cv2.cvtColor(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
kp, dsc = sift.detectAndCompute(gray, None)
BOW.add(dsc)


#dictionary created
dictionary = BOW.cluster()



FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)   # or pass empty dictionary
flann = cv2.FlannBasedMatcher(index_params, search_params)
sift2 = cv2.DescriptorExtractor_create("SIFT")
bowDiction = cv2.BOWImgDescriptorExtractor(sift2, cv2.BFMatcher(cv2.NORM_L2))
bowDiction.setVocabulary(dictionary)
print "bow dictionary", np.shape(dictionary)

#returns descriptor of image at pth
def feature_extract(pth):
    im = cv2.imread(pth, 1)
    gray = cv2.cvtColor(im, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    return bowDiction.compute(gray, sift.detect(gray))




desc1 = feature_extract(imagepath)
for k in desc1:
    print k;

print "Done"

结果是

[ 0.          0.05        0.02        0.          0.09        0.          0.02
  0.          0.09999999  0.22999999  0.19999999  0.          0.02        0.01
  0.08        0.02        0.06        0.09999999  0.          0.        ]

结果的大小是20(字典长度),但我希望它是100(SIFT大小)

0 个答案:

没有答案