# Compare two images and find all pixel coordinates that differ

## Issue

I have designed a program that compares two images and gives you the coordinates of the pixels that are different in both images and plots the using pygame. I do not mind having to use another library or to remake my whole code but it should ideally take less that 0.6s to process and it should not reduce file size, all I need it to do is to return the coordinates relative to the image

My code:

``````import cv2
import pygame
from pygame.locals import *
import time

lib = 'Map1.png'
lib2 = 'Map2.png'
lib3 = ()
coordenatesx = ()
coordenatesy = ()

counter = 0

pygame.init()

flags = DOUBLEBUF
screen = pygame.display.set_mode((500,500), flags, 4)

start = time.process_time()#To tell me how long it takes
difference = (all)-(all2)
if difference > 10 or difference < -10: #To see if the pixel's difference is in the boundary if not it is different and it gets plotted
counter+=1
pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(x, y, 1, 1))

pygame.display.update()
print(time. process_time() - start)

if counter >= (y * x) * 0.75:
print('They are similar images')
print('They are different by only :', str((counter / (y * x)) * 100), '%')
else:
print('They are different')
print('They are different by:', str((counter / (y * x)) * 100), '%')

pygame.display.update()
``````
image1 image2

## Solution

You do not need to use `for` loop to do the same.

`Numpy` makes things simple:

• it’s easy to understand
• speed up operations

``````img1 = cv2.imread(r'C:\Users\524316\Desktop\Stack\m1.png', 0)
``````

Subtracting them. `cv2.subtract()` takes care of normalization such that it doesn’t return negative values. Coordinates having no change remain black (pixel intensity = 0)

``````sub = cv2.subtract(img1, img2)
``````

Using `numpy` find the coordinates where changes are more than 0

``````coords = np.argwhere(sub > 0)

# return first 10 elements of the array coords
coords[:10]

array([[ 0, 23],
[ 0, 24],
[ 0, 25],
[ 0, 26],
[ 0, 27],
[ 0, 28],
[ 0, 29],
[ 0, 30],
[ 0, 31],
[ 0, 32]], dtype=int64)
``````

`coords` returns an array, which can be converted to a list:

``````coords_list = coords.tolist()
# return first 10 elements of the list:
>>> coords_list[:10]
[[0, 23], [0, 24], [0, 25], [0, 26], [0, 27], [0, 28], [0, 29], [0, 30], [0, 31], [0, 32]]
``````

Update:

Based on the comment made by fmw42, if you are only looking for coordinates where difference between pixel intensities is less than or greater than a certain value (say 10); you could do the following:

``````sub = cv2.absDiff(img1, img2)
np.argwhere(sub > 10)
``````