@

一、实现原理

首先从图像数据库中提取特征并存储它。然后我们计算与查询图像相关的特征。最后,我们检索具有最近特征的图像

二、 基于内容的图像检索的特征提取

在这篇研究论文中(https://arxiv.org/pdf/1404.1777.pdf),作者证明了为分类目的而训练的卷积神经网络(CNN) 可用于提取图像的“神经代码”。这

些神经代码是用于描述图像的特征。研究表明这种方法在许多数据集.上的表现与最先进的方法一样。这种方法的问题是我们首先需要标记数据来训练神经网络。标签任务可能是昂贵且耗时的。为我们的图像检索任务生成这些“神经代码”的另一种方法是使用无监督的深度学习算法。这是去噪

自动编码器的来源。相关代码可以参见:https://blog.csdn.net/qq_34213260/article/details/106333947.

三、代码实现

import numpy as np

from keras.models import Model

from keras.datasets import mnist

import cv2

from keras.models import load_model

from sklearn.metrics import label_ranking_average_precision_score

import time

print('Loading mnist dataset')

t0 = time.time()

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.astype('float32') / 255.

x_test = x_test.astype('float32') / 255.

x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format

x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format

noise_factor = 0.5

x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)

x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)

x_train_noisy = np.clip(x_train_noisy, 0., 1.)

x_test_noisy = np.clip(x_test_noisy, 0., 1.)

t1 = time.time()

print('mnist dataset loaded in: ', t1-t0)

print('Loading model :')

t0 = time.time()

autoencoder = load_model('autoencoder.h5')

encoder = Model(inputs=autoencoder.input, outputs=autoencoder.get_layer('encoder').output)

t1 = time.time()

print('Model loaded in: ', t1-t0)

def retrieve_closest_images(test_element, test_label, n_samples=10):

learned_codes = encoder.predict(x_train) # 提取数据库图像的特征向量

# 转换成一维向量

learned_codes = learned_codes.reshape(learned_codes.shape[0],

learned_codes.shape[1] * learned_codes.shape[2] * learned_codes.shape[3])

test_code = encoder.predict(np.array([test_element]))

test_code = test_code.reshape(test_code.shape[1] * test_code.shape[2] * test_code.shape[3])

distances = []

# 计算输入图像和数据库所有图像的距离

for code in learned_codes:

distance = np.linalg.norm(code - test_code)

distances.append(distance)

# 排序取出距离最小的图像

nb_elements = learned_codes.shape[0]

distances = np.array(distances)

learned_code_index = np.arange(nb_elements)

labels = np.copy(y_train).astype('float32')

labels[labels != test_label] = -1

labels[labels == test_label] = 1

labels[labels == -1] = 0

distance_with_labels = np.stack((distances, labels, learned_code_index), axis=-1)

sorted_distance_with_labels = distance_with_labels[distance_with_labels[:, 0].argsort()]

sorted_distances = 28 - sorted_distance_with_labels[:, 0]

sorted_labels = sorted_distance_with_labels[:, 1]

sorted_indexes = sorted_distance_with_labels[:, 2]

kept_indexes = sorted_indexes[:n_samples]

score = label_ranking_average_precision_score(np.array([sorted_labels[:n_samples]]), np.array([sorted_distances[:n_samples]]))

print("Average precision ranking score for tested element is {}".format(score))

original_image = x_test[0]

cv2.imshow('original_image', original_image)

retrieved_images = x_train[int(kept_indexes[0]), :]

for i in range(1, n_samples):

retrieved_images = np.hstack((retrieved_images, x_train[int(kept_indexes[i]), :]))

cv2.imshow('Results', retrieved_images)

cv2.waitKey(0)

cv2.imwrite('test_results/original_image.jpg', 255 * cv2.resize(original_image, (0,0), fx=3, fy=3))

cv2.imwrite('test_results/retrieved_results.jpg', 255 * cv2.resize(retrieved_images, (0,0), fx=2, fy=2))

# To retrieve closest image

retrieve_closest_images(x_test[0], y_test[0])

打赏

如果对您有帮助,就打赏一下吧O(∩_∩)O

以图搜图 图像匹配_基于内容的图像检索(CBIR) ——以图搜图相关推荐

  1. 基于内容的图像检索CBIR部分数据库和源代码资料

    CBIR资源列表 图像检索常用数据库:image-retrieval-database 图像检索开源代码:awesome-image-retrieval 图像标注技术:image-annotation ...

  2. 基于内容的图像检索技术

    转:https://blog.csdn.net/u013087984/article/details/52038980 图像检索:基于内容的图像检索技术 2016年06月05日  图像检索  图像检索 ...

  3. vc++实现基于内容的图像检索系统(一)

    vc++实现基于内容的图像检索系统(一) 系统设计 下载数据集 检索用的图像数据,下载网址: http://cecas.clemson.edu/~stb/research/headtracker/se ...

  4. JAVA实现的基于内容的图像检索系统设计与实现

    1 图像检索系统工作流程 基于内容的图像检索技术是对输入的图像进行分析并分类统一建模,提取其颜色.形状.纹理.轮廓和空间位置等特征,建立特征索引, 存储于特征数据库中.检索时,用户提交查询的源图像,通 ...

  5. 基于内容的图像检索软件库LIRE的特征提取方法综述

    LIRE(Lucene Image Retrieval ) 是利用Apache Lucene 建立索引进行图像检索的开源软件库.该软件项目的网址是 http://lire-project.net.LI ...

  6. 以图搜图 图像匹配_图像匹配,基于深度学习DenseNet实现以图搜图功能

    原标题:图像匹配,基于深度学习DenseNet实现以图搜图功能 度学习的发展使得在此之前以机器学习为主流算法的相关实现变得简单,而且准确率更高,效果更好,在图像检索这一块儿,目前有谷歌的以图搜图,百度 ...

  7. 基于内容的图像检索引擎(以图搜图)

    基于内容的图像检索引擎(以图搜图) 本文介绍一些基于内容的图像检索技术(Content-Based Image Retrieval,CBIR)的搜索引擎(即以图搜图),这类搜索引擎基本上代表了图像检索 ...

  8. 基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!

    百度云提取源码以及数据包,直接下载压缩包解压就可以使用,数据就在压缩包文件dataset中. 简化版:只有-颜色信息–纹理信息–形状信息–PHASH–SHFT特征点的综合检测 [百度云链接,提取码:6 ...

  9. 基于内容的图像检索概述

    摘要:我们现在处于信息爆炸的时代,各种海量信息充斥在我们周围,如何能在海量的数据中搜索到我们想要的图像是个很有挑战性的研究课题.本文简要分析了目前基于内容的图像检索(CBIR)的几种主要方法,如颜色, ...

  10. 基于内容的图像检索技(CBIR)术相术介绍

    基于内容的图像检索技(CBIR)术相术介绍 kezunhai@gmail.com http://blog.csdn.net/kezunhai 近20年来,计算机与信号处理领域如火如荼地发展着,随着普通 ...

最新文章

  1. Java开发环境的搭建以及使用eclipse从头一步步创建java项目
  2. Controller类的方法上的RequestMapping一定要写在Controller类里吗?
  3. 轨迹生成--三次样条插值
  4. Oracle 9i默认表空间
  5. ValueError: Shape mismatch: The shape of labels (received (768,)) should equal the shape of logits e
  6. 20175204 张湲祯 2018-2019-2《Java程序设计》 第一周学习总结
  7. 蜗牛导航网站模板+随机壁纸+天气插件
  8. MySQL8的8大新SQL特性
  9. 第十课 これは古い庭園です。
  10. Iridient Developer的设置和预设文件
  11. Delphi、Lazarus保留字、关键字详解
  12. 模拟电路全系列复习题 恶补知识点
  13. Matlab 神经网络工具箱应用
  14. amend用法 git 信息_详解git commit --amend 用法
  15. 2019年定义区块链领域的7个法律问题(下篇)
  16. iapp上传图片到云函数
  17. P1914 小书童——凯撒密码
  18. 2022年双11预告:李佳琦双十一美妆产品清单抢先看
  19. Rust——crates.io 更换成清华镜像源
  20. 在MySQL中,MUL、PRI和UNI有什么区别?

热门文章

  1. 微信小程序 谈谈在大学初次写项目的体验
  2. 三层交换机和vrrp协议
  3. android 中文转首字母,Android开发 - 汉字转拼音首字母
  4. OSS对象存储是什么?
  5. Learning deep representations by mutual information estimation and maximization
  6. 给Matlab添加工具箱Toolbox的方法(有截图详细讲解)(R2019b)
  7. ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.9/main: DNS lookup error
  8. html日历页面节假日_基于jquery实现可查询节假日万年历代码
  9. Windows2012 系统从MBR转GPT免重装的经验
  10. 【清澄】A1013. 进制转换4