【问题标题】:Search for similar images with different filenames in two directories在两个目录中搜索具有不同文件名的相似图像
【发布时间】:2012-09-29 04:20:56
【问题描述】:

我有 2 个目录,其中包含大量图像,例如:color/gray/。在color/中图片被命名为:image1.png image2.png等

我知道 gray/ 包含相同的图像,但是是灰度的,并且文件名和文件顺序不同(例如:file_01.png,但这不是相同的图像作为 image1.png)。

是否可以比较两个目录中的图像并将 color/ 文件复制到带有 gray/ 文件的 results/ 目录名字?

示例:

directory        | directory           | directory
   "color/"      |     "gray/"         |      "results/" 
(color images)   | (grayscale images)  | (color images with gray-scale names)   
-----------------+---------------------+----------------------------------------
color/image1.png | gray/file324.png    | results/file324.png  (in color: ==>
                                       | this and image1.png are the same image)

我希望这不是很混乱,但我不知道如何更好地解释它。

我尝试过使用 imagemagick,似乎 -compare 选项可以解决这个问题,但我无法制作 bash 脚本或其他可以很好地完成的东西。

另一种说法:我希望使用正确匹配的gray/*.jpg 名称将所有color/*.jpg 复制到results/*.jpg 文件夹中。

编辑(一些注释): 1. 三张图片的大小和内容相同。唯一的区别是两个是彩色的,一个是灰度的。当然还有文件的名称。 2. 我上传了一个带有当前名称的示例图像的 zip 文件(文件夹 "img1"color 文件夹和文件夹 "img2"grayscale 文件夹)和预期结果("img3"results 文件夹),这里:http://www.mediafire.com/?9ug944v6h7t3ya8

【问题讨论】:

  • 我们可以建议很多不同的算法...但是如果无法访问一些具有代表性的示例图像,我们将无法确定哪一个有机会为您的具体案例工作。您能否提供(链接到)一组 3 个类似图像,1 个颜色,1 个灰度,1 个“带有灰色名称的颜色”?
  • img1/image1.pngimg3/file324.png 图像是否只有相似,或者它们是否相同?
  • 见上面的编辑。这三个图像是相同的。 img1/image1.png 和 img3/file324.png 是同一个文件,但名称不同。具有正确名称的图像位于 img2/ 文件夹中,但该图像是灰度的。我需要它的颜色。我有一千张图像要处理这个问题。我真的很想为此提供一个软件解决方案。
  • 好的,我已经看过你的样品了。我已经考虑过使用一些简单命令的算法,这些命令应该与标准 ImageMagick 一起使用(不需要使用相当复杂的感知散列技术)。我现在没有时间写下来,但也许今晚或明天......敬请期待。
  • 您到底需要比较多少“数千”张图像? (假设你有 3000 张不同颜色的图像要比较,这将导致 450 万次比较。让每个比较只需要 5 秒,我们最终会花费 2250 万秒,大约需要 260 天运行......)-因此,在设计算法时应考虑一些基本的“先验”性能...

标签: image graphics imagemagick


【解决方案1】:

如果我正确理解了要求,我们需要:

  • 查找文件夹 gray/ 中名为 XYZ 的每个 灰度 图像...
  • ...文件夹color/中名为ABC的匹配color图像和...
  • ...将 ABC 复制到文件夹 results/ 下,新名称为 XYZ

所以我建议的基本算法是这样的:

  1. 将文件夹 color/ 中的所有图像转换为灰度并将结果存储在文件夹 gray-reference/ 中。保留原名:

    mkdir gray-reference
    convert  color/img123.jpg  -colorspace gray  gray-reference/img123.jpg
    
  2. 对于 reference/ 中的每个灰度图像,与文件夹 gray/ 中的每个灰度图像进行比较。如果找到匹配项,请将相同名称的相应图像从 color/ 复制到 results/。创建差异的视觉表示的一种可能的比较命令是:

    compare  gray-reference/img123.jpg  gray/imgABC.jpg  -compose src delta.jpg
    

真正的诀窍是比较两个灰度图像(如第 2 步)。 ImageMagick 有一个方便的命令来逐像素比较两个(相似)图像并将结果写入“增量”图像:

compare  reference.png  test.png  -compose src  delta.png

如果比较是针对彩色图像,则在增量图像中...

  • ...每个相等的像素显示为白色,而...
  • ...每个不同的像素都以高亮颜色显示(默认为红色)。

另请参阅我的回答 "ImageMagick: 'Diff' an Image",了解该技术的示例。

如果我们直接逐个像素地比较灰色图像和彩色图像,我们当然会发现几乎每个像素都是不同的(导致全红色的“delta”图片)。因此,我在上面的步骤 1 中建议首先将彩色图像转换为灰度图像。

如果我们比较两张灰度图像,得到的增量图像也是灰度的。因此默认的高亮颜色不能是红色。我们最好将其设置为“黑色”以便更好地查看。

现在,如果我们当前对颜色的灰度转换会导致与现有灰度图像具有“不同”的灰度(我们当前生成的灰度可能只是比现有灰度图像稍亮或稍暗,因为已应用不同的颜色配置文件),可能仍然我们的增量图片是全“红色”,或者更确切地说是全高亮颜色。但是,我用您的示例图像对此进行了测试,结果很好:

 convert  color/image1.jpg  -colorspace gray  image1-gray.jpg  
 compare                  \
    gray/file324.jpg      \
    image1-gray.jpg       \
   -highlight-color black \
   -compose src           \
    delta.jpg

delta.jpg 由 98% 的白色像素组成。我不确定您的数千张灰度图像中的所有其他图像在源自彩色原件时是否使用相同的设置。因此,我们在运行 compare 命令时添加了一个小的 fuzz 因素,这确实允许在比较 2 个像素时出现一些颜色偏差:

compare  -fuzz 3%  reference.png  test.png  -compose src  delta.png

由于该算法要执行数千次(可能是数百万次,考虑到您所说的图像数量),我们应该考虑一些性能因素,我们应该对compare 命令的持续时间进行计时。这尤其令人担忧,因为您的示例图像相当大(3072x2048 像素 - 6 兆像素),并且比较可能需要一段时间。

我在 MacBook Pro 上的计时结果如下:

time (convert  color/image1.jpg  -colorspace gray  image1-gray.jpg ;
      compare                   \
         gray/file324.jpg       \
         image1-gray.jpg        \
        -highlight-color black  \
        -fuzz 3%                \
        -compose src            \
         delta100-fuzz.jpg)

  real  0m6.085s
  user  0m2.616s
  sys   0m0.598s

6 秒时间:1 次将大型彩色图像转换为灰度,外加 1 次比较两个大型灰度图像。

您谈到了“数千张图片”。假设有 3000 张图像,基于这个时间,所有图像的处理将需要(3000*3000)/2 比较(450 万)和(3000*3000*6)/2 秒(2700 万秒)。完成所有比较总共需要 312 天。太长了,如果你问我的话。

我们可以做些什么来提高性能?

嗯,我的第一个想法是减小图像的大小。如果我们比较较小的图像而不是 3072x2048 大小的图像,则比较应该更快地返回结果。 (但是,我们还将花费额外的时间来首次缩小测试图像 - 但希望比我们以后在比较较小的图像时节省的时间要少得多:

time (convert color/image1.jpg  -colorspace gray  -scale 6.25%  image1-gray.jpg  ;
      convert gray/file324.jpg                    -scale 6.25%  file324-gray.jpg ;
      compare                  \
         file324-gray.jpg      \
         image1-gray.jpg       \
        -highlight-color black \
        -fuzz 3%               \
        -compose src           \
         delta6.25-fuzz.jpg)

   real  0m0.670s
   user  0m0.584s
   sys   0m0.074s

这样好多了!我们缩短了近 90% 的处理时间,如果您使用 MacBook Pro,这让您有希望在 35 天内完成工作。

改进是合乎逻辑的:通过将图像尺寸减小到原始图像的 6.25%,生成的图像只有 192x128 像素——从 600 万像素减少到 24500 像素,比例为 256:1。

(注意:-thumbnail-resize 参数的运行速度会比 -scale 快一点。但是,这种速度提高是对质量损失的权衡。质量损失可能会使比较不那么可靠...)

我们可以告诉 ImageMagick 打印一些统计数据,而不是从比较图像中创建视觉可检查的 delta 图像。要获取不同像素的数量,我们可以使用AE 指标。命令及其结果如下:

time (convert color/image1.jpg -colorspace gray -scale 6.25% image1-gray.jpg  ;
     convert gray/file324.jpg                   -scale 6.25% file324-gray.jpg ;
     compare -metric AE  file324-gray.jpg image1-gray.jpg -fuzz 3% null: 2>&1 )
0 

  real  0m0.640s
  user  0m0.574s
  sys   0m0.073s

这意味着我们有 0 不同的像素——我们可以直接在 shell 脚本中使用这个结果!

Shell 脚本的构建块

以下是用于执行自动比较的 shell 脚本的构建块:

  1. 将彩色图像从'color/'目录转换为灰度图像,将它们缩小到6.25%并将结果保存在'reference-color/'目录中:

    # Estimated time required to convert 1000 images of size 3072x2048:
    #   500 seconds
    mkdir reference-color
    for i in color/*.jpg; do
        convert  "${i}"  -colorspace gray  -scale 6.25%  reference-color/$(basename "${i}")
    done
    
  2. 缩小“gray/”目录中的图像并将结果保存在“reference-gray/”目录中:

    # Estimated time required to convert 1000 images of size 3072x2048:
    #    250 seconds
    mkdir reference-gray
    for i in gray/*.jpg; do
        convert  "${i}"  -scale 6.25%  reference-gray/$(basename "${i}")
    done
    
  3. 将“reference-gray/”目录中的每个图像与“reference-color”目录中的图像进行比较,直到找到匹配项:

    # Estimated time required to compare 1 image with 1000 images:
    #    300 seconds
    # If we have 1000 images, we need to conduct a total of 1000*1000/2
    # comparisons to find all matches;
    #    that is, we need about 2 days to accomplish all.
    # If we have 3000 images, we need a total of 3000*3000/2 comparisons
    # to find all matches;
    #    this requires about 20 days.
    #
    for i in reference-gray/*.jpg ; do
    
        for i in reference-color/*.jpg ; do
    
            # compare the two grayscale reference images
            if [ "x0" == "x$(compare  -metric AE  "${i}"  "${j}" -fuzz 3%  null: 2>&1)" ]; then
    
                # if we found a match, then create the copy under the required name
                cp color/$(basename "${j}"  results/$(basename "${i}") ;
    
                # if we found a match, then remove the respective reference image (we do not want to compare again with this one)
                rm -rf "${i}"
    
                # if we found a match, break from within this loop and start the next one
                break ;
    
            fi
    
        done
    
    done
    

警告:不要盲目依赖这些构建块。它们未经测试。我没有可用于测试的多个合适图像的目录,并且我不想自己创建一个只是为了这个练习。谨慎行事!

【讨论】:

  • 非常非常感谢! :D 我没想到这一切!再次感谢您,先生! :D 我会尝试你的建议并与结果保持联系。
  • 这似乎完全可行。现在消耗的时间不是很高,这将一遍又一遍地用于处理非常大的图片集,是的,但是这些集将是大约 1000 1500 张图片,而不是 3000 张,所以我认为这可能会完成在 20 天左右。现在不到一个月对我来说是完美的。再次感谢您!
【解决方案2】:

如果感知散列技术(例如pHash)对您的具体数据产生了一些好的结果,您应该尝试一下。

感知哈希将为您提供可靠的相似性度量,因为底层算法足够强大,可以考虑对比度调整或不同压缩/格式等变化/转换 - 标准加密哈希函数(例如 MD5)并非如此.

此外,您可以通过在您自己的图像上使用其方便的基于网络的demo interface 来验证 pHash 是否有效。

【讨论】:

  • 这是一个非常有趣的建议,我将查看文档。该演示看起来很有希望,它输出了我期望的结果,即这两个图像,无论它们的色彩空间,都是相同的。但是,我不是开发人员,我还不确定如何实施此解决方案。反正我要试试,谢谢。
【解决方案3】:

在对 -fuzz 选项进行一些调整和摆弄之后,Kurt 的解决方案非常有效! :) 最终运行良好的 -fuzz 的最终值为 50%!我尝试了 3、10、19、20、24、25、30 和 40%,但没有成功。可能是因为之前生成的灰度图像是用不同的方法生成的,所以灰度不同。此外,所有图像的大小都不同,其中一些相对较小,因此按百分比缩放方法会产生不好的结果。我用了-resize 200x,所以所有的参考图像都差不多大小,最后这是我用的bash脚本:

    # this bash assumes the existence of two dirs: color/ and gray/ 
    # each one with images to compare

    echo Starting...
    echo Checking directories...
    if [ ! -d color ]; then
        echo Error: the directory color does not exist!
        exit 1;
    fi
    if [ ! -d gray ]; then
        echo Error: the directory gray does not exist!
        exit 1;
    fi

    echo Directories exist. Proceeding...

    mkdir reference-color
    echo creating reference-color...
    for i in color/*.png; do
        convert  "${i}"  -colorspace gray  -resize 200x  reference-color/$(basename "${i}")
    done
    echo reference-color created...

    mkdir reference-gray
    echo creating reference-gray...
    for i in gray/*.png; do
        convert  "${i}"  -resize 200x  reference-gray/$(basename "${i}")
    done
    echo reference-gray created...

    mkdir results
    echo created results directory...

    echo ...ready.

    echo "-------------------------"
    echo "|  starting comparison  |"
    echo "-------------------------"

    for i in reference-gray/*.png; do
        echo comparing image $i 

        for j in reference-color/*.png; do

            # compare the two grayscale reference images

            if [ "x0" == "x$(compare  -metric AE "${i}"  "${j}" -fuzz 50% null: 2>&1)" ]; then

                # if we found a match, then create the copy under the required name
                echo Founded a similar one. Copying and renaming it...
                cp color/$(basename "${j}")  results/$(basename "${i}")

                # if we found a match, then remove the respective reference image (we do not want to compare again with this one)
                echo Deleting references...
                rm -rf "${i}"
                rm -rf "${j}"
                echo "--------------------------------------------------------------"

                # if we found a match, break from within this loop and start the next one
                break ;

            fi

        done

    done
    echo Cleaning...
    rm -rf reference-color
    rm -rf reference-gray
    echo Finished!

时间度量是(对于 180 个图像,在 cygwin 中使用 imagemagick,所以在本机 linux imagemagick 中可能更好,我还不知道):

real    5m29.308s
user    2m25.481s
sys     3m1.573s

如果有人感兴趣,我上传了一个包含脚本和一组测试图像的文件。 http://www.mediafire.com/?1ez0gs6bw3rqbe4(以7z格式压缩)

再次感谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2011-07-13
    • 2016-12-27
    相关资源
    最近更新 更多