寻找相似图像的算法

91

我需要一种算法,可以确定两个图像是否“相似”,并识别颜色、亮度、形状等方面的相似模式。 我可能需要一些指针,了解人脑用于“分类”图像的参数。

我已经看过基于哈斯多夫匹配的方法,但似乎主要用于匹配变换后的对象和形状的模式。


这个相关的问题中有一些不错的答案:https://dev59.com/EnVD5IYBdhLWcg3wTJvF - blak
2
很多“可能”和“也许”的说法。有人尝试过所有这些建议,并知道哪个是最好的吗? - john k
已经过去了13年,但我想在这里更新一下,基于机器学习的图像特征向量可以具有鲁棒性,并且可以通过余弦相似度进行轻松比较。您可以搜索img2vec项目或类似latentvector.space的内容以获得更简单的API集成(免责声明:我运营该服务)。 - Christian Safka
16个回答

63

我曾采用过一种类似的方法,通过使用小波变换将图像分解为特征。

我的方法是从每个转换后的信道中选择最重要的n个系数,并记录它们的位置。这是通过根据abs(power)对(power,location)元组列表进行排序来完成的。相似的图像将在相同的位置具有显著的系数。

我发现最好将图像转换为YUV格式,这有效地允许您在形状(Y通道)和颜色(UV通道)上加权相似性。

您可以在mactorii中找到上述实现,不幸的是,我没有像应该做的那样多地工作在这上面 :-)。

另一种方法是,我的一些朋友用了一个令人惊讶的好结果,就是将图像简单地调整大小到4x4像素,然后将其存储为您的特征。可以通过计算两个图像之间的曼哈顿距离来评估它们的相似程度,使用相应像素。我没有它们执行缩放的详细信息,所以您可能需要尝试使用各种可用于此任务的算法来找到适合您的算法。


7
调整大小到4x4的方法是一个很棒的想法(不是你的方法不好),但第一个方法更简单。 - Alix Axel
@freespace,您能否解释一下“使用相应的像素计算两个图像之间的曼哈顿距离”的含义? - Ambika
1
@Ambika:将每个像素的颜色视为长度为3的向量,并计算要比较的图像中对应像素之间的曼哈顿距离。这将给出4个曼哈顿距离。如何从中得出单一的度量标准取决于您。最明显的方法是将它们相加。 - freespace

48

pHash可能会引起您的兴趣。

感知哈希(perceptual hash) n. 音频、视频或图像文件的指纹,基于其包含的音频或视觉内容的数学计算。与依赖于输入微小变化引起输出剧烈变化的密码哈希函数不同,如果输入在视觉上或听觉上相似,则感知哈希是“接近”的。


11
刚才浏览了pHash的网站。他们目前在网站上提供了一个功能,可以上传两张图片,并告诉你它们是否相似。我尝试了大约10张相似的图片和10张不相似的图片。不幸的是,成功率并不太令人满意。 - rodrigo-silveira
3
pHash实际上相当严格,您可能希望使用“ahash”或平均哈希,这些哈希更不严格。您可以在此处找到Python实现:https://github.com/JohannesBuchner/imagehash/blob/master/find_similar_images.py - Rohit

13

我曾经使用SIFT在不同的图像中重新检测相同的对象。它非常强大但也相当复杂,可能有些过度。如果这些图像应该是相似的,则基于两个图像之间差异的一些简单参数可以告诉你很多信息。以下是一些提示:

  • 归一化图像即通过计算两个图像的平均亮度并根据比率将最亮处降低(以避免在最高级别进行裁剪)使两幅图像的平均亮度相同,特别是如果您更关心形状而不是颜色。
  • 对于每个通道,在规范化的图像上计算颜色差异总和。
  • 查找图像中的边缘并测量两个图像中边缘像素之间的距离。(用于形状)
  • 将图像分割成一组离散区域,并比较每个区域的平均颜色。
  • 在一个或多个水平阈值处对图像进行阈值处理,并计算结果黑白图像之间不同的像素数。

你能指出使用SIFT类特征计算图像相似度的代码吗? - mrgloom
很抱歉,我相信有公开可用的代码,但我不知道。这个网站上有一些例子。例如在这里:https://dev59.com/fW035IYBdhLWcg3wYO11 - jilles de wit
Accord Framework for .Net(http://accord-framework.net)拥有一些出色的类,可使用各种内核和聚类算法进行SURF、BagOfVisualWords、Harris Corner Detection等操作。 - dynamichael

6
我的实验室也需要解决这个问题,我们使用了Tensorflow。以下是一个可视化图像相似性的完整应用程序实现:https://github.com/YaleDHLab/pix-plot
如果想要了解有关将图像向量化以进行相似性计算的教程,请查看此页面。以下是Python代码(请参见帖子以获取完整工作流程):
from __future__ import absolute_import, division, print_function

"""

This is a modification of the classify_images.py
script in Tensorflow. The original script produces
string labels for input images (e.g. you input a picture
of a cat and the script returns the string "cat"); this
modification reads in a directory of images and 
generates a vector representation of the image using
the penultimate layer of neural network weights.

Usage: python classify_images.py "../image_dir/*.jpg"

"""

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Simple image classification with Inception.

Run image classification with Inception trained on ImageNet 2012 Challenge data
set.

This program creates a graph from a saved GraphDef protocol buffer,
and runs inference on an input JPEG image. It outputs human readable
strings of the top 5 predictions along with their probabilities.

Change the --image_file argument to any jpg image to compute a
classification of that image.

Please see the tutorial and website for a detailed description of how
to use this script to perform image recognition.

https://tensorflow.org/tutorials/image_recognition/
"""

import os.path
import re
import sys
import tarfile
import glob
import json
import psutil
from collections import defaultdict
import numpy as np
from six.moves import urllib
import tensorflow as tf

FLAGS = tf.app.flags.FLAGS

# classify_image_graph_def.pb:
#   Binary representation of the GraphDef protocol buffer.
# imagenet_synset_to_human_label_map.txt:
#   Map from synset ID to a human readable string.
# imagenet_2012_challenge_label_map_proto.pbtxt:
#   Text representation of a protocol buffer mapping a label to synset ID.
tf.app.flags.DEFINE_string(
    'model_dir', '/tmp/imagenet',
    """Path to classify_image_graph_def.pb, """
    """imagenet_synset_to_human_label_map.txt, and """
    """imagenet_2012_challenge_label_map_proto.pbtxt.""")
tf.app.flags.DEFINE_string('image_file', '',
                           """Absolute path to image file.""")
tf.app.flags.DEFINE_integer('num_top_predictions', 5,
                            """Display this many predictions.""")

# pylint: disable=line-too-long
DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
# pylint: enable=line-too-long


class NodeLookup(object):
  """Converts integer node ID's to human readable labels."""

  def __init__(self,
               label_lookup_path=None,
               uid_lookup_path=None):
    if not label_lookup_path:
      label_lookup_path = os.path.join(
          FLAGS.model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt')
    if not uid_lookup_path:
      uid_lookup_path = os.path.join(
          FLAGS.model_dir, 'imagenet_synset_to_human_label_map.txt')
    self.node_lookup = self.load(label_lookup_path, uid_lookup_path)

  def load(self, label_lookup_path, uid_lookup_path):
    """Loads a human readable English name for each softmax node.

    Args:
      label_lookup_path: string UID to integer node ID.
      uid_lookup_path: string UID to human-readable string.

    Returns:
      dict from integer node ID to human-readable string.
    """
    if not tf.gfile.Exists(uid_lookup_path):
      tf.logging.fatal('File does not exist %s', uid_lookup_path)
    if not tf.gfile.Exists(label_lookup_path):
      tf.logging.fatal('File does not exist %s', label_lookup_path)

    # Loads mapping from string UID to human-readable string
    proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()
    uid_to_human = {}
    p = re.compile(r'[n\d]*[ \S,]*')
    for line in proto_as_ascii_lines:
      parsed_items = p.findall(line)
      uid = parsed_items[0]
      human_string = parsed_items[2]
      uid_to_human[uid] = human_string

    # Loads mapping from string UID to integer node ID.
    node_id_to_uid = {}
    proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
    for line in proto_as_ascii:
      if line.startswith('  target_class:'):
        target_class = int(line.split(': ')[1])
      if line.startswith('  target_class_string:'):
        target_class_string = line.split(': ')[1]
        node_id_to_uid[target_class] = target_class_string[1:-2]

    # Loads the final mapping of integer node ID to human-readable string
    node_id_to_name = {}
    for key, val in node_id_to_uid.items():
      if val not in uid_to_human:
        tf.logging.fatal('Failed to locate: %s', val)
      name = uid_to_human[val]
      node_id_to_name[key] = name

    return node_id_to_name

  def id_to_string(self, node_id):
    if node_id not in self.node_lookup:
      return ''
    return self.node_lookup[node_id]


def create_graph():
  """Creates a graph from saved GraphDef file and returns a saver."""
  # Creates graph from saved graph_def.pb.
  with tf.gfile.FastGFile(os.path.join(
      FLAGS.model_dir, 'classify_image_graph_def.pb'), 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')


def run_inference_on_images(image_list, output_dir):
  """Runs inference on an image list.

  Args:
    image_list: a list of images.
    output_dir: the directory in which image vectors will be saved

  Returns:
    image_to_labels: a dictionary with image file keys and predicted
      text label values
  """
  image_to_labels = defaultdict(list)

  create_graph()

  with tf.Session() as sess:
    # Some useful tensors:
    # 'softmax:0': A tensor containing the normalized prediction across
    #   1000 labels.
    # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
    #   float description of the image.
    # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
    #   encoding of the image.
    # Runs the softmax tensor by feeding the image_data as input to the graph.
    softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')

    for image_index, image in enumerate(image_list):
      try:
        print("parsing", image_index, image, "\n")
        if not tf.gfile.Exists(image):
          tf.logging.fatal('File does not exist %s', image)

        with tf.gfile.FastGFile(image, 'rb') as f:
          image_data =  f.read()

          predictions = sess.run(softmax_tensor,
                          {'DecodeJpeg/contents:0': image_data})

          predictions = np.squeeze(predictions)

          ###
          # Get penultimate layer weights
          ###

          feature_tensor = sess.graph.get_tensor_by_name('pool_3:0')
          feature_set = sess.run(feature_tensor,
                          {'DecodeJpeg/contents:0': image_data})
          feature_vector = np.squeeze(feature_set)        
          outfile_name = os.path.basename(image) + ".npz"
          out_path = os.path.join(output_dir, outfile_name)
          np.savetxt(out_path, feature_vector, delimiter=',')

          # Creates node ID --> English string lookup.
          node_lookup = NodeLookup()

          top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
          for node_id in top_k:
            human_string = node_lookup.id_to_string(node_id)
            score = predictions[node_id]
            print("results for", image)
            print('%s (score = %.5f)' % (human_string, score))
            print("\n")

            image_to_labels[image].append(
              {
                "labels": human_string,
                "score": str(score)
              }
            )

        # close the open file handlers
        proc = psutil.Process()
        open_files = proc.open_files()

        for open_file in open_files:
          file_handler = getattr(open_file, "fd")
          os.close(file_handler)
      except:
        print('could not process image index',image_index,'image', image)

  return image_to_labels


def maybe_download_and_extract():
  """Download and extract model tar file."""
  dest_directory = FLAGS.model_dir
  if not os.path.exists(dest_directory):
    os.makedirs(dest_directory)
  filename = DATA_URL.split('/')[-1]
  filepath = os.path.join(dest_directory, filename)
  if not os.path.exists(filepath):
    def _progress(count, block_size, total_size):
      sys.stdout.write('\r>> Downloading %s %.1f%%' % (
          filename, float(count * block_size) / float(total_size) * 100.0))
      sys.stdout.flush()
    filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
    print()
    statinfo = os.stat(filepath)
    print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
  tarfile.open(filepath, 'r:gz').extractall(dest_directory)


def main(_):
  maybe_download_and_extract()
  if len(sys.argv) < 2:
    print("please provide a glob path to one or more images, e.g.")
    print("python classify_image_modified.py '../cats/*.jpg'")
    sys.exit()

  else:
    output_dir = "image_vectors"
    if not os.path.exists(output_dir):
      os.makedirs(output_dir)

    images = glob.glob(sys.argv[1])
    image_to_labels = run_inference_on_images(images, output_dir)

    with open("image_to_labels.json", "w") as img_to_labels_out:
      json.dump(image_to_labels, img_to_labels_out)

    print("all done")
if __name__ == '__main__':
  tf.app.run()

有没有一种方法可以改进这个技术?当我添加新文件时,我必须重新做所有工作才能找到相似之处。有没有一种加快添加新文件过程的方法?@duhaime - mostafa8026
感谢您提供一些关键词或阅读资源,以加快对新文件进行分类的过程。 - mostafa8026
@mostafa8026 随时欢迎!如果您想尝试向pixplot存储库发送拉取请求,我们肯定会感激不尽! - duhaime

5
你可以使用感知图像差异
这是一个命令行实用程序,使用感知度量比较两个图像。也就是说,它使用人类视觉系统的计算模型来确定两个图像是否在视觉上不同,因此忽略像素中的微小变化。此外,它大大减少了由于随机数生成、操作系统或机器架构差异而导致的误报数量。

4
这是一个棘手的问题!这取决于您需要多么准确,以及您正在处理哪种类型的图像。您可以使用直方图来比较颜色,但这显然不考虑图像中这些颜色的空间分布(即形状)。边缘检测后跟某种分割(即挑选出形状)可以提供用于与另一幅图像匹配的模式。您可以使用共生矩阵来比较纹理,通过将图像视为像素值矩阵,并比较这些矩阵。有一些关于图像匹配和机器视觉的好书 - 在亚马逊上搜索会找到一些。
希望这能帮到您!

3

3

1
neurondotnet.freehostia.com的链接已经失效。 - Monica Heddneck

3

有相关研究使用Kohonen神经网络/自组织映射。

无论是更加学术的系统(例如Google的PicSOM),还是较少学术性的演示文稿(例如http://www.generation5.org/content/2004/aiSomPic.asp,可能不适用于所有工作环境),都存在。


3

计算缩小版本(例如:6x6像素)像素颜色值差的平方和非常有效。相同的图像产生0,相似的图像产生小数字,不同的图像产生大数字。

其他人提出的先分解成YUV的想法听起来很有趣 - 虽然我的想法很好用,但我希望我的图像被计算为“不同”,以便得到正确的结果 - 即使从色盲观察者的角度也是如此。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接