Python中两个图像的比较

2022-03-11 00:00:00 python java processing

我想使用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发布的比较函数?

要使用的Canny算法如下:

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

1

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 ()

Python

正如@martineau建议的那样,推荐答案图像库是一个不错的选择。我个人也认为可以使用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))

希望这能有所帮助!

相关文章