【问题标题】:Comparison between two images in PythonPython中两个图像之间的比较
【发布时间】:2019-11-01 01:06:08
【问题描述】:

我想使用 Python 比较两个图像,但我不熟悉这种语言。

我有两张相同大小的图片。我必须创建一个包含两个图像的逐像素差异的数组。最后,我要计算数组所有值之和的平均值,作为浮点数。

我可以使用 Processing 来做到这一点,但我不能使用 Python 来做到这一点。

如果两个图像相同,则结果显然是 0。

我想把这段代码翻译成Python(最重要的是最终平均值的值)。

PImage img,img2;
int threshold = 64;

void setup(){
    //size(600,400);
    img = loadImage(args[0]);
    img2 = loadImage(args[1]);
    println(comparison(img,img2));
    exit();
}

PImage binarization(PImage img,int threshold){
    for(int i = 0; i < img.pixels.length; i++){
        if(green(img1.pixels[i]) > threshold) img.pixels[i] = color(255);
        else img.pixels[i] = color(0);
    }
    return img;
}

float comparison(PImage img, PImage img2){

    img.filter(GRAY);
    img2.filter(GRAY);

    img = binarazation(img,threshold);
    img2 = binarization(img2,threshold); 

    int array[] = new int[img.pixels.length];

    for(int i = 0; i < img.pixels.length; i++){
        array[i] = int( abs(green(img.pixels[i]) - green(img2.pixels[i])));
    }

    float average = 0;

        for(int i = 0; i < img.pixels.length; i++){
            average+= array[i];
    }
    average = average/img.pixels.length;

    return average;
}

编辑::::

非常感谢!

我之前贴的比较函数不太对

它实际上应该出现一个图像(在它被转换为灰度之后)和另一个图像(已经应用了 canny 算法)

如何修改 elgordorafiki 发布的比较功能?

使用的精明算法是这样的:

import cv2
import numpy as np
from matplotlib import pyplot as plt

 

img = cv2.imread ('img', 0)
edges = cv2.Canny (img, 100,110)

plt.subplot (2,1,1), plt.imshow (img, cmap = 'gray')
plt.title ('Original Image'), plt.xticks ([]), plt.yticks ([])
plt.subplot (2,1,2), plt.imshow (edges, cmap = 'gray')
plt.title ('Canny Edge Detection'), plt.xticks ([]), plt.yticks ([])

plt.show ()

【问题讨论】:

  • 那是什么语言?
  • If the two images are equal I will obviously get 1 as a value 你不应该得到 0 吗?每个像素的差为0,所以总和和它的平均值也应该是0。
  • 我贴的代码是在处理过程中写的我应该在python中做同样的事情
  • 是的,我已经纠正了错误
  • 您可以使用 Python 图像库 (PIL) 的 Pillow 端口在 Python 3.x 中执行类似的图像处理任务。这是它的online documentation。它将允许您编写 Python 代码来执行此操作。如果您在使其正常工作时遇到问题,请发布另一个问题。

标签: java python processing


【解决方案1】:

正如@martineau 所建议的,Python Imaging Library 是一个不错的选择。我个人也认为您可以使用 numpy 和 matplotlib 作为替代方案。 python的好处是你可以使用数组对整个图像进行操作,而不是使用for循环,这样看起来会更好更快。

作为示例,我快速将您的代码移植到 python(不确定在那里执行什么过滤器以及您的阈值具有什么值,但其余部分应该几乎相同)

我也有一些疑问(您将二值化后的值设置为 255,这意味着最终平均值会有点高,也许使用 1 和 0 之间的值会更容易解释,但这取决于您)。

import numpy as np
import matplotlib.pyplot as plt
import sys

def binarize(img, threshold):

    # create an image with True or False (that can be seen as 1 or 0) and multiply by 255
    binaryimg = (img > threshold).astype(int) * 255
    return binaryimg

def comparison(img1, img2):

    # convert to gray. What's the filter doing?
    gray1 = rgb2gray(img1)
    gray2 = rgb2gray(img2)

    # select a threhsold value and binarize the image
    threshold = 0.5
    gray1_bin = binarize(gray1, threshold)
    gray2_bin = binarize(gray2, threshold)

    # in python you can compute a difference image.
    # so diff will contain in each pixel the difference between the two images
    diff = gray1_bin - gray2_bin

    # the np.mean gives you already sum / number of pixels
    average = np.mean(diff)

    return average

def rgb2gray(col_img):

    # converts images to gray
    weights = np.array([0.3, 0.59, 0.11])
    gray = np.sum([col_img[:, :, i].astype(np.float64) * weights[i] for i in range(3)], axis=0)

    return gray

# the "main" method
if __name__ == "__main__":

    # read the images
    img = plt.imread(sys.argv[1])
    img2 = plt.imread(sys.argv[2])

    result = comparison(img, img2)

    print("The difference between the images is {}".format(result))

希望这会有所帮助!

【讨论】:

  • 非常感谢!!我想问你最后一件事......
  • 好的,现在问题很不一样了!但我不明白是什么问题,您发布的代码现在可以正常工作了! (我测试过)。两个阈值(您输入 100 和 110)控制您将检测到的“边缘”的数量(请参阅here 以获得更详细的解释),如果您需要更好的结果,请使用这些值,否则代码正在工作很好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-24
  • 1970-01-01
  • 2018-12-31
  • 2013-01-04
  • 1970-01-01
相关资源
最近更新 更多