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