-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimagePlotter.py
279 lines (201 loc) · 9.99 KB
/
imagePlotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Python script to convert an image into lat lon coordinates and then ADS-B broadcasts
from tkinter.filedialog import askopenfilename # used for file select dialog
import tkinter as tk # needed for gui
import cv2 # needed for finding black pixels
import numpy as np # needed for pixel math
import random # needed for picking random pixels to paint
import math # needed for coord calcs
from sklearn.cluster import DBSCAN # needed for DBSCAN clustering
from sklearn.neighbors import NearestNeighbors # needed for helping find epsilon
from kneed import KneeLocator # needed for helping to find epsilon
from PIL import Image # needed for debugging
import pandas as pd # debugging
import matplotlib.pyplot as plt # needed for visualization
class ImagePlotter:
"""
Class that handles converting image points into lat lon coordinates
"""
def __init__(self, imgFile, lat, lon, width):
"""
Initialization method
Args:
imgFile (file): The image to paint
lat (float): the center lat point in degrees
lon (float): the center lon point in degrees
width (float): the width in km of the image when painted
"""
self.centerLat = math.radians(float(lat))
self.centerLon = math.radians(float(lon))
self.width = float(width)
# makes the list of points to paint
self.pixelList = self.__listBlack(imgFile)
#print(f"Center pixel: {str(self.centerWidth)}, {str(self.centerHeight)}")
def updateLocation(self, lat, lon, width):
"""
Updates the central location and width to be used in coordinate generation
Args:
lat (float): the center lat point in degrees
lon (float): the center lon point in degrees
width (float): the width in km of the image when painted
"""
self.centerLat = math.radians(float(lat))
self.centerLon = math.radians(float(lon))
self.width = float(width)
# TODO: Make this update picture coords
def updateImage(self, imgFile):
"""
updates all of the pixel maps with the new image
Args:
imgFile (file): the new image file to use
"""
# makes the list of points to paint
self.pixelList = self.__listBlack(imgFile)
# TODO: Have this update the rest of the image stuff
def getCoords(self, numTargets, visualize=False):
'''
Pulls a user given number of lat lon coordinates from the loaded image and returns them
'''
clusterPoints = self.__clusterData(self.pixelList)
targetPixels = [] # the eventual list of pixels to paint
# gets the number of targets that will be pulled from each cluster
#targetsPerCluster = round(numTargets / len(clusterPoints))
numOfPoints = 0
for cluster in clusterPoints:
numOfPoints = numOfPoints + len(cluster)
for i in range(len(clusterPoints)):
#targetPixels.extend(random.sample(clusterPoints[i], round(numTargets * (len(clusterPoints[i]) / numOfPoints))))
# calculate the spacing to even distrubute the target pixels within the cluster
gap = math.floor(len(clusterPoints[i]) / round(numTargets * (len(clusterPoints[i]) / numOfPoints)))
val = 0 # variable to move through cluster list
# collect targets from cluster
while val < len(clusterPoints[i]):
targetPixels.append(clusterPoints[i][val])
val = val + gap # increment value to next point to add
#targetPixels = random.sample(self.pixelList, numTargets)
#print(str(len(targetPixels)))
if visualize: # display a picture representing what the transmitted image should look like
# gets max X and Y values
maxX = 0
maxY = 0
for pix in targetPixels:
if pix[1] > maxX:
maxX = pix[1]
if pix[0] > maxY:
maxY = pix[0]
#print(f"{str(maxX)} : {str(maxY)}")
demoImg = Image.new(mode="RGB", size=(maxX+1, maxY+1))
pixelMap = demoImg.load()
for pix in targetPixels:
#print(f"{str(pix[0])} : {str(pix[1])}")
pixelMap[pix[1], pix[0]] = (255, 255, 255)
demoImg.show()
return self.__pixelsToCoords(targetPixels)
def __pixelsToCoords(self, targetPixels):
'''
Takes a list of pixels and returns a list of coordinates
'''
# gets the ratio of kilometers per pixel by dividing the max km width by total width in pixels
kmPerPix = self.width / (self.centerWidth * 2)
#print(f"km per pixel: {str(kmPerPix)}")
# radius of the earth
rEarth = 6371
# empty target list
targetList = []
#print(targetPixels)
for target in targetPixels:
# get angle and distance of each target point from the center
angle = math.atan2(target[0] - self.centerHeight, target[1] - self.centerWidth) * (180 / math.pi)
distPix = math.sqrt(pow(target[1] - self.centerWidth, 2) + pow(target[0] - self.centerHeight, 2))
# get bearing in rads and dist in km
bearing = math.radians(90 + angle)
distKM = distPix * kmPerPix
#print(f"{str(bearing)} {str(distKM)}")
# get the target lat lon
targetLat = math.asin(math.sin(self.centerLat)*math.cos(distKM / rEarth) + math.cos(self.centerLat)*math.sin(distKM / rEarth)*math.cos(bearing))
#print(str(targetLat))
targetLon = self.centerLon + math.atan2(math.sin(bearing)*math.sin(distKM / rEarth)*math.cos(self.centerLat),math.cos(distKM / rEarth)-math.sin(self.centerLat)*math.sin(targetLat))
#print(str(targetLon))
targetLat = math.degrees(targetLat)
targetLon = math.degrees(targetLon)
targetList.append([targetLat, targetLon])
#print(f"{str(targetLon)},{str(targetLat)}")
return targetList
def __clusterData(self, pixelList):
'''
Given a list of pixels, performs a clustering algorithm to look for points
'''
# gets distance from all neighbours
neighbors = NearestNeighbors(n_neighbors=11).fit(np.asarray(pixelList))
distances, indices = neighbors.kneighbors(np.asarray(pixelList))
distances = np.sort(distances[:,len(distances[0])-1], axis=0)
# find knee point
knee = KneeLocator(np.arange(len(distances)), distances, S=1, curve='convex', direction='increasing', interp_method='polynomial')
# use knee point to calculate clusters
dbClusters = DBSCAN(eps=distances[knee.knee], min_samples=8).fit(np.asarray(pixelList))
# Number of Clusters
nClusters=len(set(dbClusters.labels_))-(1 if -1 in dbClusters.labels_ else 0)
#print(str(nClusters))
# creates a list of lists to hold each point in its cluster
clusterPoints = [ [] for _ in range(nClusters) ]
#print(str(clusterPoints))
# put each pixel in the cluster list its a part of
for i in range(len(pixelList)):
if dbClusters.labels_[i] != -1: # if not a un-clustered point, add to list
clusterPoints[dbClusters.labels_[i]].append(pixelList[i])
# print method for debugging
#for i in range(len(clusterPoints)):
#print(str(len(clusterPoints[i])))
return clusterPoints
def __listBlack(self, imgFile):
"""
Returns a list of every black pixel in the provided image.
Also updates centerWidth and centerHeight to the new image.
Args:
imgFile (file): The image file to load and read
Returns:
list: list of pixel coordinates in the format of [Height, Width]
"""
# loads the image and converts it into a grayscale one
image = cv2.imread(imgFile) # grabs transparent pixels
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# grab center
height, width = gray.shape
self.centerHeight = round(height / 2)
self.centerWidth = round(width / 2)
# threshold of what counts as black
threshold = 128
# find coordinates of all pixels below threshold
coords = np.column_stack(np.where(gray < threshold))
#print(coords)
# Sanity check by redrawing the new image
# Create mask of all pixels lower than threshold level
#mask = gray < threshold
# Color the pixels in the mask
#image[mask] = (204, 119, 0)
#cv2.imshow('image', image)
#cv2.waitKey()
return coords.tolist()
if __name__ == '__main__':
root = tk.Tk()
root.withdraw()
print("Welcome to the Image plotter script")
imgFile = askopenfilename(title="select image to use", filetypes=(("PNG files", ".png"), ("JPEG files", ".jpg"), ("all files", ".")))
lat = input("Enter the desired center latitude (decimal format) of the image: ")
lon = input("Enter the desired center longitude (decimal format) of the image: ")
width = input("Enter the desired width in KM of the image: ")
#painter = ImagePlotter(imgFile, 38.6001, -77.1622, 10000)
#painter = ImagePlotter(imgFile, 0, 0, 2000)
painter = ImagePlotter(imgFile, float(lat), float(lon), float(width))
acceptable = False
while not acceptable:
targetNum = input("Enter the number of aircraft to make up the image: ")
coords = painter.getCoords(int(targetNum), True)
choice = input("If output is acceptable, enter 1. Anything else will loop. ")
if choice == '1':
acceptable = True
debug = open('test.txt', 'w')
for target in coords:
# for use with: https://dwtkns.com/pointplotter/
debug.write(f"{str(format(target[1], '.20f'))}, {str(format(target[0], '.20f'))}\n")
debug.close()