hog 特征提取

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


# Feature extractor
# 特征提取器
def extract_features(image_path, vector_size=2048):

    try:
        img = imread(image_path)
        img = cv2.resize(img, (128, 128))
        # cv2.imshow('imshow', img)
        # cv2.waitKey(0)
        #
        # cv2.destroyAllWindows()

        winSize = (32, 32)
        blockSize = (16, 16)
        blockStride = (8, 8)
        cellSize = (8, 8)
        nbins = 9

        hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins)

        winStride = (64, 64)
        padding = (8, 8)
        test_hog = hog.compute(img, winStride, padding)  # .reshape((-1,))
        lists = [np.float(x[0]) for x in test_hog]
        print(lists)
        print(test_hog.shape)

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

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

    return lists


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

        dic['feature'] = extract_sift
        dic['hash'] = index
        dic['itemId'] = index
        dic['topimages'] = index
        dicJson = json.dumps(dic)

        # 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、付费专栏及课程。

余额充值