import cv2
from skimage.metrics import structural_similarity as ssim
import numpy as np
from PIL import Image, ImageChops
import matplotlib.pyplot as plt

################################################################
###########   USING PIXEL COMPARISON   #########################


##############   IMPORTANT TO READ !!  #########################
#1 for maps with real data where Gmapping gives maps with #fefefe instead of #ffffff, and grey with #D8D8D8
#  2 for maps with maching colors, usually simulated data
#  3 for comparing maps with error, or maps where there are many tonalities of white
val = 1

#Path to the map generated by our algorithm
img1= Image.open('C:/Users/jc/Documents/GitHub/saut_ogm/image-comparison/ALL_occ-0.8_free-0.25_cell-0.05_wall-0.05.png')
#Path to the reference map
img2= Image.open('C:/Users/jc/Documents/GitHub/saut_ogm/image-comparison/mape2.png')

#Convert to RGB
img1 = img1.convert("RGB")
img2 = img2.convert("RGB")

#Resize images
width = max(img1.width, img2.width)
height = max(img1.height, img2.height)
img1 = img1.resize((width, height))
img2 = img2.resize((width, height))

# Convert the images to NumPy arrays
pixels1 = np.array(img1)
pixels2 = np.array(img2)

# Find white pixels in img1 and img2
white_pixels_1 = np.all(pixels1 == [255, 255, 255], axis=2)
#Maps that come from Gmapping are not 100% white, they are #fefefe
if val == 1:
    white_pixels_2 = np.all(pixels2 == [254, 254, 254], axis=2)
elif val ==3:
    min_value = [180, 180, 180]
    max_value = [255, 255, 255]
    white_pixels_2 = np.all((pixels2 >= min_value) & (pixels2 <= max_value), axis=2)
else:
    white_pixels_2 = np.all(pixels2 == [255, 255, 255], axis=2)

# Initialize a counter for the different white pixels
count_white1 = np.sum(white_pixels_1)
count_white2 = np.sum(white_pixels_2)
#Interception 
intersecting_pixels = np.sum(white_pixels_1 & white_pixels_2)
total_white_pixels = count_white1 + count_white2 - intersecting_pixels

#total_white_pixels = np.sum(white_pixels_1)
percentage_diff_white = (intersecting_pixels / total_white_pixels) * 100

#Print results
print("Using Only White Comparison:");
print("white 1: " + str(np.sum(white_pixels_1)))
print("white 2: " + str(np.sum(white_pixels_2)))
print("size: " + str(height*width))
print("Number of Intersecting Pixels: " + str(intersecting_pixels))
print("Percentage of equal pixels: " + str(percentage_diff_white))

diff = np.sum(pixels1!=pixels2)
# Create a copy of img1
highlighted_img1 = np.array(img1)
highlighted_img2 = np.array(img2)


diff_pixels = np.any(pixels1 != pixels2, axis=-1)
if val == 1:
    map_pixels = np.logical_or(
        np.all(pixels1 == [255, 255, 255], axis=-1) & np.all(pixels2 == [254, 254, 254], axis=-1),
        np.all(pixels1 == [0, 0, 0], axis=-1) & np.all(pixels2 == [0, 0, 0], axis=-1),
    )
    equal_pixels = np.logical_or(
        np.all(pixels1 == [255, 255, 255], axis=-1) & np.all(pixels2 == [254, 254, 254], axis=-1),
        np.all(pixels1 == [149, 149, 149], axis=-1) & np.all(pixels2 == [216, 216, 216], axis=-1)
        )
    diff_pixels = np.logical_and(diff_pixels, np.logical_not(equal_pixels))
elif val == 3:
    pixels2_range = (pixels2 >= min_value) & (pixels2 <= max_value)
    map_pixels = np.logical_or(
        np.all(pixels1 == [255, 255, 255], axis=-1) & np.all(pixels2_range, axis=-1),
        np.all(pixels1 == [0, 0, 0], axis=-1) & np.all(pixels2 == [0, 0, 0], axis=-1),
        )
    white = np.all(pixels1 == [255, 255, 255], axis=-1) & np.all(pixels2_range, axis=-1)
else:
    map_pixels = np.logical_or(
        np.all(pixels1 == [255, 255, 255], axis=-1) & np.all(pixels2 == [255, 255, 255], axis=-1),
        np.all(pixels1 == [0, 0, 0], axis=-1) & np.all(pixels2 == [0, 0, 0], axis=-1),
    )


diff_img = np.array(img1)
#In red the different pixels
diff_img[diff_pixels] = [255, 0, 0]
#In blue the white pixels of both images
diff_img[white_pixels_1] = [0,0,255]
diff_img[white_pixels_2] = [0, 0, 255]
#In green the map pixels (white and black) that appear simultaneously in both images
diff_img[map_pixels] = [0,255,0]
plt.imshow(diff_img)
plt.title('Differences between Image 1 and Image 2')
plt.show()
