提取sift特征

import cv2
from imageio import imread
import json
import numpy as np

# Feature extractor
# 特征提取器
def extract_features(image_path, vector_size=2048):
    image = imread(image_path)
    #image = cv2.resize(image, (128,128), interpolation=cv2.INTER_AREA)
    #image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    try:
        # Using KAZE, cause SIFT, ORB and other was moved to additional module
        # which is adding addtional pain during install
        # 此处为了简化安装步骤,使用KAZE,因为SIFT/ORB以及其他特征算子需要安
        # 装额外的模块
        alg = cv2.xfeatures2d.SIFT_create()
        # Finding image keypoints
        # 寻找图像关键点
        kps = alg.detect(image)

        print('kps: ' + str(len(kps)))
        # Getting first 32 of them.
        # 计算前32个
        # Number of keypoints is varies depend on image size and color pallet
        # 关键点的数量取决于图像大小以及彩色调色板
        # Sorting them based on keypoint response value(bigger is better)
        # 根据关键点的返回值进行排序(越大越好)
        kps = sorted(kps, key=lambda x: -x.response)[:vector_size]

        print('kpssorted: ' + str(len(kps)))

        print(kps)
        # computing descriptors vector
        # 计算描述符向量
        kps, dsc = alg.compute(image, kps)
        # Flatten all of them in one big vector - our feature vector
        # 将其放在一个大的向量中,作为我们的特征向量
        #dsc = dsc.flatten()
        dsclist=[]

        for keydsc in dsc:
            score=0
            #print(keydsc)
            for dscitem in keydsc:
                score = score + dscitem
            dsclist.append(score/5120)


        print('dsclist: ' + str(len(dsclist)))
        print(dsclist)
        # Making descriptor of same size
        # 使描述符的大小一致
        # Descriptor vector size is 64
        # 描述符向量的大小为64

        with open("/Users/zdwh/Documents/bak/feature/img", 'w') as fp:
            fp.write(str(kps))

        needed_size = (vector_size * 64)
        # if dsc.size < needed_size:
        #     # if we have less the 32 descriptors then just adding zeros
        #     # at the end of our feature vector
        #     # 如果少于32个描述符,则在特征向量后面补零
        #     dsc = np.concatenate([dsc, np.zeros(needed_size - dsc.size)])

        # img = cv2.drawKeypoints(image=image,
        #                         outImage=image,
        #                         keypoints=kps,
        #                         color=(51, 163, 236),
        #                         flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

        #显示图像
        # cv2.imshow('sift_keypoints', img)
        # while (True):
        #     if cv2.waitKey(int(1000 / 12)) & 0xFF == ord('q'):
        #         break
        # cv2.destroyAllWindows()


        dist1 = 1 - np.dot(dsclist, dsclist) / (np.linalg.norm(dsclist) * np.linalg.norm(dsclist))
        print('dist1: ')
        print(dist1)

    except cv2.error as e:
        print
        'Error: ', e
        return None

    return dsclist


def batch_extractor(pickled_db_path="/Users/zdwh/Documents/bak/feature/siftim"):
    for index in range(1, 4):
        f='/Users/zdwh/Documents/bak/img/img'+str(index)+'.jpg'
        name = '1img'
        dic = {}
        extract_sift= extract_features(f)

        dic['feature'] = extract_sift#.tolist()
        dic['hash'] = index
        dic['itemId'] = index
        dic['topimages'] = index
        dicJson = json.dumps(dic)
        #result[name] = extract_features(f)

        # saving all our feature vectors in pickled file
        # 将特征向量存于pickled 文件
        with open(pickled_db_path+str(index)+".txt", 'w') as fp:
            fp.write(dicJson)


images_path = ''
batch_extractor()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值