【问题标题】:Compare two images and have the output print statement labelling reflect the samples being compared比较两个图像并让输出打印语句标签反映正在比较的样本
【发布时间】:2019-12-03 18:26:43
【问题描述】:

我不确定要在线找到解决方案的正确术语是什么。

我有两个文件夹文件 1 和文件 2。在本示例的每个文件夹中,我只在每个文件夹中使用了六个图像。但是,一旦我修复了代码,我想比较几百张图像。

两个文件夹中的文件完全相同。

我的代码可以完美地比较每个文件并显示结果,但标签与输出结果不匹配。

    compare_image_files(imgA[0], imgA[0])
    compare_image_files(imgA[0], imgB[0])
    compare_image_files(imgA[0], imgB[1])
    compare_image_files(imgA[0], imgB[2])
    compare_image_files(imgA[0], imgB[3])
    compare_image_files(imgA[0], imgB[4])
    compare_image_files(imgA[0], imgB[5])

我的代码并不优雅,但除了标签之外它可以工作:

    %matplotlib inline
    import matplotlib.pyplot as plt
    from pathlib import Path
    from IPython.display import Image, display
    from sklearn.metrics import mean_squared_error as mse
    from skimage.measure import compare_ssim as ssim
    import numpy as np
    from skimage import data
    from skimage import exposure
    from skimage.transform import match_histograms
    import os, glob
    import cv2


     path = '/Users/minnymouse/Documents/AAA_TEST_FILES_AUDIO/EXP_1_MULTI_FIBER/'
     #print(os.path.isdir(path))

     from matplotlib import rcParams
     rcParams['axes.titlepad'] = 20 

     source = '/Users/minnymouse/Documents/AAA_TEST_FILES_AUDIO/EXP_1_MULTI_FIBER/file1/'
     #print(os.listdir(reference))
     #print(os.path.isdir(source))
     #print(os.listdir(source_images))

     source_images = glob.iglob(source + "*.png")



     for source_file in source_images:

         source_head, tail = os.path.splitext(source_file)

         im = cv2.imread(source_file)

         imgA = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)


         #fig = plt.figure()

         #source_image = plt.imshow(img)

         #plt.axis("off")

    reference = 
         '/Users/minnymouse/Documents/AAA_TEST_FILES_AUDIO/EXP_1_MULTI_FIBER/file2/'


     reference_images = glob.iglob(reference + "*.png")

    #print(os.listdir(reference_images))

    for ref_file in reference_images:

        ref_head, tail = os.path.splitext(ref_file)

        im = cv2.imread(ref_file)

        imgB = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

        #fig = plt.figure()

        #ref_image = plt.imshow(img)

        #plt.axis("off")

    def compare_image_files(imgA, imgB):

        m = mse(imgA, imgB)

        s = ssim(imgA, imgB)

        print("Source File: " + os.path.basename(source_head), "is compared to " + 
    os.path.basename(ref_head), "= ", "MSE: %.2f, SSIM: %.2f" % (m, s))


        return m, s

我认为问题在于 os.path.basename 或我如何为这两个文件夹设置“for”语句。我真的不知道,这使得这很难解决。 感谢您的帮助。

我是 Mac 10.14.6,使用 Jupyter 和 Python3

【问题讨论】:

  • 您的问题是您没有将名称作为参数发送 - compare_image_files(imgA, imgB, filename1, filenam2):。而且您必须在列表中保留带有图像的名称 - (image, filename)。使用source_head, tail = ...,您只能保留循环中的最后一个值。
  • 其他问题可能是您没有将图像保留在列表中,imgAimgB 只能保留最后一张图像,并使用 imgA[0]imgB[0] 比较文件中的两行,不是两个文件。

标签: python macos label output jupyter


【解决方案1】:

在您的代码中,您不使用列表来保存带有名称的图像。您读取图像并始终分配给相同的变量,以便删除以前的图像。所以最后在imgAimgB 你只有一个图像。而source_headrefe-head 只保留姓氏。而imgA[0]imgB[0] 从两个图像中获取第一行,imgA[1]imgB[1] 从相同的两个图像中获取第二行,等等。

你需要

# ---

all_source_images = []

source_images = glob.iglob(source + "*.png")

for source_file in source_images:
     source_head, tail = os.path.splitext(source_file)
     im = cv2.imread(source_file)
     imgA = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

     all_source_images.append( [imgA, source_head] )

# ---

all_ref_images = []

reference_images = glob.iglob(reference + "*.png")

for ref_file in reference_images:
    ref_head, tail = os.path.splitext(ref_file)
    im = cv2.imread(ref_file)
    imgB = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

    all_ref_images.append( [imgB, ref_head] )

现在您有两个包含图像和名称的列表 - all_source_imagesall_ref_images

现在您必须使用嵌套的for-loops 来处理这些列表。

for image_A, filename_A in all_source_images:
    for image_B, filename_B in all_ref_images:
        compare_image_files(image_A, image_B, filename_A, filename_B)

你也应该发送名字作为参数

def compare_image_files(imgA, imgB, nameA, nameB):
    m = mse(imgA, imgB)
    s = ssim(imgA, imgB)

    print("Source File: %s is compared to %s = MSE: %.2f, SSIM: %.2f" % (os.path.basename(nameA), os.path.basename(nameB), m, s))

    return m, s

【讨论】:

  • 谢谢。谢谢你。谢谢!!!这两天我一直在看这个。它完美地工作。我唯一的问题是,可以在标签末尾没有文件 ext .png 吗?除此之外。谢谢你。谢谢你。谢谢。
  • 只需在all_source_images.append( [imgA, source_file] ) 中使用source_head 代替source_file。与其他列表相同。
  • 我将source_head 而不是source_file 放在当前代码中作为答案。与其他列表相同。
  • 输出看起来很漂亮。谢谢!我不能停止看着它。好开心。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 2019-08-03
  • 1970-01-01
  • 2013-06-17
  • 2016-12-24
相关资源
最近更新 更多