简体   繁体   English

功能与opencv中的flann匹配

[英]Feature matching with flann in opencv

I am working on an image search project for which i have defined/extracted the key point features using my own algorithm. 我正在开发一个图像搜索项目,我已经使用自己的算法定义/提取了关键点功能。 Initially i extracted only single feature and tried to match using cv2.FlannBasedMatcher() and it worked fine which i have implemented as below: 最初我只提取了单个功能并尝试使用cv2.FlannBasedMatcher()进行匹配,并且它工作正常,我已经实现如下:

Here vec is 2-d list of float values of shape (10, )
Ex:
[[0.80000000000000004, 0.69999999999999996, 0.59999999999999998, 0.44444444444444448, 0.25, 0.0, 0.5, 2.0, 0, 2.9999999999999996]

[2.25, 2.666666666666667, 3.4999999999999996, 0, 2.5, 1.0, 0.5, 0.37499999999999994, 0.20000000000000001, 0.10000000000000001]

[2.25, 2.666666666666667, 3.4999999999999996, 0, 2.5, 1.0, 0.5, 0.37499999999999994, 0.20000000000000001, 0.10000000000000001]

[2.25, 2.666666666666667, 3.4999999999999996, 0, 2.5, 1.0, 0.5, 0.37499999999999994, 0.20000000000000001, 0.10000000000000001]]

vec1 = extractFeature(img1)
vec2 = extractFeature(img2)

q1 = np.asarray(vec1, dtype=np.float32)
q2 = np.asarray(vec2, dtype=np.float32)

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)
matches = flann.knnMatch(q1,q2,k=2)

But now i have one more feature descriptor for each key point along with previous one but of different length. 但是现在我为每个关键点添加了一个特征描述符以及前一个关键点,但长度不同。 So now my feature descriptor has shape like this: 所以现在我的特征描述符的形状如下:

[[[0.80000000000000004, 0.69999999999999996, 0.59999999999999998, 0.44444444444444448, 0.25, 0.0, 0.5, 2.0, 0, 2.9999999999999996],[2.06471330e-01,   1.59191645e-02,   9.17678759e-05, 1.32570314e-05,   4.58424252e-10,   1.66717250e-06,6.04810165e-11]

[[2.25, 2.666666666666667, 3.4999999999999996, 0, 2.5, 1.0, 0.5, 0.37499999999999994, 0.20000000000000001, 0.10000000000000001],[ 2.06471330e-01,   1.59191645e-02,   9.17678759e-05, 1.32570314e-05,   4.58424252e-10,   1.66717250e-06, 6.04810165e-11],

[[2.25, 2.666666666666667, 3.4999999999999996, 0, 2.5, 1.0, 0.5, 0.37499999999999994, 0.20000000000000001, 0.10000000000000001],[ 2.06471330e-01,   1.59191645e-02,   9.17678759e-05, 1.32570314e-05,   4.58424252e-10,   1.66717250e-06, 6.04810165e-11],

[[2.25, 2.666666666666667, 3.4999999999999996, 0, 2.5, 1.0, 0.5, 0.37499999999999994, 0.20000000000000001, 0.10000000000000001],[ 2.06471330e-01,   1.59191645e-02,   9.17678759e-05, 1.32570314e-05,  4.58424252e-10,   1.66717250e-06, 6.04810165e-11]]

Now since each point's feature descriptor is a list two lists(descriptors) with different length that is (10, 7, ) so in this case i am getting error: 现在,因为每个点的特征描述符是一个列表,两个列表(描述符)具有不同的长度,即(10,7,)所以在这种情况下我得到错误:

setting an array element with a sequence. 使用序列设置数组元素。

while converting feature descriptor to numpy array of float datatype: 将特征描述符转换为float数据类型的numpy数组时:

q1 = np.asarray(vec1, dtype=np.float32)

I understand the reason of this error is different length of lists, so i wonder What would be the right way to implement the same? 我理解这个错误的原因是列表的长度不同,所以我想知道实现相同的正确方法是什么?

You should define a single descriptor of size 10+7=17 . 您应该定义大小为10+7=17的单个描述符。

This way, the space descriptor is now of 17 and you should be able to use cv2.FlannBasedMatcher . 这样,空间描述符现在为17,您应该能够使用cv2.FlannBasedMatcher

Either create a global descriptor of the correct size desc_glob = np.zeros((nb_pts,17)) and fill it manually or find a Python way to do it. 创建一个正确大小的全局描述符desc_glob = np.zeros((nb_pts,17))并手动填充它或找到一种Python方法来完成它。 Maybe np.reshape((nb_pts,17)) ? 也许是np.reshape((nb_pts,17))

Edit: 编辑:

To not favor one descriptor type over the other, you need to weight or normalize the descriptors. 为了不偏袒另一个描述符类型,您需要对描述符进行加权或规范化。 This is the same principle than computing a global descriptor distance from two descriptors: 这与从两个描述符计算全局描述符距离的原理相同:

dist(desc1, desc2) = dist(desc1a, desc2a) + lambda * dist(desc1b, desc2b) dist(desc1,desc2)= dist(desc1a,desc2a)+ lambda * dist(desc1b,desc2b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM