Peppe
Peppe

Reputation: 21

Comparison between two images in Python

I want to compare two images using Python, but I'm not familiar with this language.

I have two images of the same size. I have to create an array containing the pixel-by-pixel difference of the two images. Finally, I have to calculate the average of the sum of all the values ​​of the array, as a float.

I am able to do it using Processing, but I can't do it using Python.

If the two images are the same the result will obviously be 0.

I would like to translate this code into Python (the most important thing is the value of the final average).

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;
}

EDIT::::

Thank you very much!

The comparison function that I posted previously is not really correct

it should actually appear an image (after it has been transformed to grayscale) with another image (which canny algorithm has been applied)

how could i modify the comparison function posted by elgordorafiki?

the canny algorithm to use is this:

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

Upvotes: 2

Views: 1411

Answers (1)

freerafiki
freerafiki

Reputation: 559

As @martineau suggests, the Python Imaging Library is a good way to go. I personally also think you can use numpy and matplotlib as an alternative. The good thing about python is that you can then use the array and do operation on the whole image instead of using for loops, that would look better and be faster.

As an example I quickly ported your code to python (not sure about what filter is doing there and what value your threshold has, but rest should be pretty much the same)

I have also some doubts (you set to 255 the value after binarizing, that meaning the final average will be somehow high, maybe using values between 1 and 0 would be easier to be interpreted after, but that's up to you).

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

Hope this helps!

Upvotes: 1

Related Questions