Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 比较两个图像,并使输出打印语句标签反映正在比较的样本_Python_Macos_Label_Output_Jupyter - Fatal编程技术网

Python 比较两个图像,并使输出打印语句标签反映正在比较的样本

Python 比较两个图像,并使输出打印语句标签反映正在比较的样本,python,macos,label,output,jupyter,Python,Macos,Label,Output,Jupyter,我不确定要在网上找到解决方案,正确的术语应该是什么 我有两个文件夹文件1和文件2。在本例的每个文件夹中,我只在每个文件夹中使用了六个图像。然而,一旦我修复了代码,我想比较几百张图片 两个文件夹中的文件完全相同 我的代码可以完美地比较每个文件并显示结果,但标签与输出结果不匹配 compare_image_files(imgA[0], imgA[0]) compare_image_files(imgA[0], imgB[0]) compare_image_files(imgA

我不确定要在网上找到解决方案,正确的术语应该是什么

我有两个文件夹文件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,在你的代码中你不使用列表来保存带有名字的图像。您读取图像并始终将其指定给同一变量,以便删除以前的图像。最后,在
imgA
imgB
中,您只有一个图像。而
source\u head
refe 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] )
现在有两个图像列表,它们的名称是-
所有源图像
所有参考图像

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

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

您的问题是您没有将名称作为参数发送-
比较图像文件(imgA、imgB、filename1、filenam2):
。您必须在列表中保留带有图像的名称-
(图像,文件名)
。使用
source\u head,tail=…
您只能保留循环中的最后一个值。其他问题可能是您没有在列表中保留图像,而
imgA
imgB
只能保留最后一个图像,使用
imgA[0]
,您可以比较文件中的两行,而不是两行。谢谢。非常感谢。非常感谢。我已经主演这部电影两天了。它工作得很好。我唯一的问题是,是否可以不在标签的末尾添加ext.png文件?除此之外。非常感谢。非常感谢。谢谢。只需在
所有源图像中使用
source\u head
而不是
源文件
。追加([imgA,source\u file])
。与其他列表相同。我将
source\u head
而不是
source\u file
放在回答中的当前代码中。其他列表也一样。输出看起来很漂亮。非常感谢。我不能停止看它。太高兴了。