Python中两个图像的比较

2024-03-29 10:15:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我可以使用处理来完成它,但是我不能使用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

你知道吗

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

Tags: 图像importimgforthresholdpltgreenarray
1条回答
网友
1楼 · 发布于 2024-03-29 10:15:46

正如@martineau所建议的,Python图像库是一个很好的方法。我个人也认为可以使用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))

希望这有帮助!你知道吗

相关问题 更多 >