資源簡介
locality-sensitive hashing(局部敏感哈希),實現高位數據搜索平臺

代碼片段和文件信息
#?-*-?coding:utf-8?-*-
import?sys
import?math
import?time
import?string
from?opencv.cv?import?*
from?opencv.highgui?import?*
Hash_1?=?[1?3?7?8]
Hash_2?=?[2?6?9?14]?
Hash_3?=?[3?5?15?21]
Hash_4?=?[12?17?19?23]
#對SIFT特征向量進行歸一化處理
def?Make_Norm(array):
????total?=?0
????normalization?=?[0]?*?len(array)
????for?i?in?range(len(array)):
????????total?+=?array[i]?**?2
????total?=?total?**?0.5
????if?total?!=?0:
????????for?i?in?range(len(array)):
????????????normalization[i]?=?float(array[i])?/?total
????return?normalization
#得到需要的12維顏色直方圖向量
def?Get_vector(image):
????width?=?image.width
????height?=?image.height
????vector?=?[0]?*?12
????index?=?0
????for?m?in?range(2):
????????for?n?in?range(2):
????????????total?=?0
????????????for?i?in?range(m?*?width?/?2?(m+1)?*?width?/?2):
????????????????for?j?in?range(n?*?height?/?2?(n+1)?*?height?/?2):
????????????????????for?k?in?range(3):
????????????????????????vector[index+k]?+=?image[ji][k]
????????????for?k?in?range(3):
????????????????total?+=?vector[index+k]
????????????for?k?in?range(3):
????????????????vector[index+k]?=?vector[index+k]?/?total
????????????index?+=?3
????return?vector
#對12維向量做量化處理
def?Make_character(vector):
????for?i?in?range(len(vector)):
????????if?vector[i]?0.3:
????????????vector[i]?=?0
????????elif?vector[i]?>?0.6:
????????????vector[i]?=?2
????????else:
????????????vector[i]?=?1
????return?vector
#對向量在相應hash函數上求投影
def?Make_projection(I?vector):
????section?=?{}
????projection?=?““?
????I.sort()
????for?i?in?range(len(I)):
????????tmp?=?(I[i]?+?1)?/?2
????????try:
????????????section[tmp].append(I[i])
????????except:
????????????section[tmp]?=?[]
????????????section[tmp].append(I[i])
????for?i?in?range(1?len(vector)+1):
????????num?=?0
????????try:
????????????current?=?section[i]
????????????for?j?in?range(len(current)):
????????????????if?current[j]?-?2?*?(i?-?1)?<=?vector[i-1]:
????????????????????num?+=?1
????????????projection?+=?(num?*?“1“?+?(len(current)?-?num)?*?“0“)
????????except:
????????????pass
????return?projection
#求兩個圖片對應的特征向量的相似度
def?Get_similarity(point1?point2):
????totalSum?=?0
????for?i?in?range(len(point1)):
????????totalSum?+=?point1[i]?*?point2[i]
????return?totalSum
#求每個向量投影后得到的hash值
def?Hash_key(image):
????p?=?Get_vector(image)
????p?=?Make_character(p)
????tmp?=?[]
????tmp.append(Make_projection(Hash_1?p))
????tmp.append(Make_projection(Hash_2?p))
????tmp.append(Make_projection(Hash_3?p))
????tmp.append(Make_projection(Hash_4?p))
????return?tmp
def?main():
????result?=?[]
????image?=?cvLoadImage(“target.jpg“?1)
????object?=?Hash_key(image)
????p?=?Get_vector(image)
????start?=?time.clock()
????for?i?in?range(1?41):
????????image?=?cvLoadImage((“%d.jpg“?%?i)?1)
????????tmp?=?Hash_key(image)
????????if?tmp?==?object:
????????????result.append(i)
????end?=?time.clock()
????print?end?-?start
????start?=?time.c
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????3776??2013-01-11?22:22??LSH.py
- 上一篇:Unity 程序流程圖
- 下一篇:圖像降噪Demo
評論
共有 條評論