蠡測 2022-07-11 16:32 采纳率: 57.1%
浏览 58
已结题

没有添加路径 不知道问题在哪 也没有报错 可否讲下代码

img


没有报错 不知道哪里有问题 程序是github上找的
import cv2
import numpy as np
import pickle as pk
import random
import os
import matplotlib.pyplot as plt
from imageio import imread

def extract_features(image_path, vector_size=32):
image = imread(image_path, mode="RGB")
try:
alg = cv2.SIFT_create()
kps = alg.detect(image)
kps = sorted(kps, key=lambda x: -x.response)[:vector_size]
kps, dsc = alg.compute(image, kps)
dsc = dsc.flatten()
needed_size = (vector_size * 64)
if dsc.size < needed_size:
dsc = np.concatenate([dsc, np.zeros(needed_size - dsc.size)])
except cv2.error as e:
print('Error: ', e)
return None

return dsc

def batch_extractor(images_path, pickled_db_path):
files = [os.path.join(images_path, p) for p in sorted(os.listdir(images_path))]

result = {}
for f in files:
    print('Extracting features from image %s' % f)
    name = f.split('/')[-1].lower()
    result[name] = extract_features(f)

# saving all our feature vectors in pickled file
with open(pickled_db_path, 'w') as fp:
    pickle.dump(result, fp)

class Matcher(object):

def __init__(self, pickled_db_path="features.pck"):
    with open(pickled_db_path) as fp:
        self.data = pickle.load(fp)
    self.names = []
    self.matrix = []
    for k, v in self.data.iteritems():
        self.names.append(k)
        self.matrix.append(v)
    self.matrix = np.array(self.matrix)
    self.names = np.array(self.names)

def cos_cdist(self, vector):
    # getting cosine distance between search image and images database
    v = vector.reshape(1, -1)
    return scipy.spatial.distance.cdist(self.matrix, v, 'cosine').reshape(-1)

def match(self, image_path, topn=5):
    features = extract_features(image_path)
    img_distances = self.cos_cdist(features)
    # getting top 5 records
    nearest_ids = np.argsort(img_distances)[:topn].tolist()
    nearest_img_paths = self.names[nearest_ids].tolis
  • 写回答

1条回答 默认 最新

  • 爱晚乏客游 2022-07-11 18:13
    关注

    extract_features在哪里调用呗,路径就是在调用的时候输入的,例如Matcher.match(image_path,5),这里的imgpath就是图像的路径。
    或者你可以像我这样在调用里面print一下路径,就知道每次调用的图像在哪里

    def extract_features(image_path, vector_size=32):
        image = imread(image_path, mode="RGB")
        print(image_path)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 7月22日
  • 已采纳回答 7月14日
  • 赞助了问题酬金5元 7月11日
  • 创建了问题 7月11日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)