import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import csv
import numpy as np
from matplotlib.widgets import CheckButtons

img=cv2.imread('cleaned_skeleton_1_robot.pgm',0)
img2=cv2.imread('cleaned_skeleton_2_robot.pgm',0)
img3=cv2.imread('cleaned_skeleton_ROADS_robot.pgm',0)


#pixely do skeletov poli
Skeleton1=[]
Skeleton2=[]
Skeleton_final_ROADS=[]
Skeleton1_spolu_Skeleton2=[]
Skeleton1_spolu_SkeletonFinalRoads=[]


for i in range(len(img)):
    for j in range(len(img[i])):
        if(img[i][j]!=0):
            img[i][j]=1
            Skeleton1.append([i,j])
cv2.imwrite('cleaned_skeleton_1_robot.pgm', img)

for i in range(len(img2)):
    for j in range(len(img2[i])):
        if(img2[i][j]!=0):
            img2[i][j]=1
            Skeleton2.append([i,j])
cv2.imwrite('cleaned_skeleton_2_robot.pgm', img2)

for i in range(len(img3)):
    for j in range(len(img3[i])):
        if(img3[i][j]!=0):
            img3[i][j]=1
            Skeleton_final_ROADS.append([i,j])
cv2.imwrite('cleaned_skeleton_ROADS_robot.pgm', img3)
#pixely do skeletov poli

#porovnanie spolocnych pixelov
for a in Skeleton1:
    for b in Skeleton2:
        if(a==b):
            Skeleton1_spolu_Skeleton2.append(a)

for a in Skeleton1:
    for b in Skeleton_final_ROADS:
        if(a==b):
            Skeleton1_spolu_SkeletonFinalRoads.append(a)
#porovnanie spolocnych pixelov

#zapisanie pixelov do suboru kvoli grafu (1.Skeleton a Final Roads Skeletons)




with open("Skeleton1-SkeletonFinal.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(Skeleton1_spolu_SkeletonFinalRoads)




#zapisanie pixelov do suboru kvoli grafu (1.Skeleton a Final Roads Skeletons)


#Vykreslenie skeletonov
img4=mpimg.imread('map_entry.pgm')
imgplot = plt.imshow(img4,cmap='gray')


Skeleton1=np.asarray(Skeleton1)
Skeleton2=np.asarray(Skeleton2)
Skeleton_final_ROADS=np.asarray(Skeleton_final_ROADS)
Skeleton1_spolu_Skeleton2=np.asarray(Skeleton1_spolu_Skeleton2)
Skeleton1_spolu_SkeletonFinalRoads=np.asarray(Skeleton1_spolu_SkeletonFinalRoads)

p1=plt.scatter(Skeleton1[:,[1]],Skeleton1[:,[0]], color='blue', s=40)
p2=plt.scatter(Skeleton2[:,[1]],Skeleton2[:,[0]], color='red', s=40)
p3=plt.scatter(Skeleton_final_ROADS[:,[1]],Skeleton_final_ROADS[:,[0]], color='green', s=40)
p4=plt.scatter(Skeleton1_spolu_Skeleton2[:,[1]],Skeleton1_spolu_Skeleton2[:,[0]], color='purple', s=40)
p5=plt.scatter(Skeleton1_spolu_SkeletonFinalRoads[:,[1]],Skeleton1_spolu_SkeletonFinalRoads[:,[0]], color='aqua', s=40)

lines=[p1,p2,p3,p4,p5]
# checkbuton widget
labels = ['Skeleton 1', 'Skeleton 2', 'Skeleton final Roads','Skeleton1=Skeleton2','Skeleton1=SkeletonFinalRoads']
activated = [True, True, True, True, True]
axCheckButton = plt.axes([0.03, 0.4, 0.15, 0.15])
chxbox = CheckButtons(axCheckButton, labels, activated)

#Define colours for rectangles and set them
c = ['blue', 'red', 'green','purple','aqua']
[rec.set_facecolor(c[i]) for i, rec in enumerate(chxbox.rectangles)]

def set_visible(label):
	index = labels.index(label)
	lines[index].set_visible(not lines[index].get_visible())
	plt.draw()

chxbox.on_clicked(set_visible)



plt.show()
#Vykreslenie skeletonov
