# Untitled

unknown
python
2 years ago
4.4 kB
2
Indexable
Never
```'''
Created on 5 gru 2022

@author: SzymonPolak
'''

import cv2
import numpy as np
import math
import itertools
import random

class InitiatorEstimator:
def __init__(self):
self.idealPoint = Point("Ideal point", 0 ,0)
self.chosenPoint = Point("Chosen point", 0 ,0)
def findMinAvgPoint(self, pointDistances):
minAverage = pointDistances[0].averageDistance()
bestPoint = pointDistances[0].testPoint
for pd in pointDistances:
if pd.averageDistance() < minAverage:
minAverage = pd.averageDistance()
bestPoint = pd.testPoint
bestPoint.name = bestPoint.name+" best initiator"
bestPoint.color = (0,0,255)
print("Min avg point is:", bestPoint.name)
return bestPoint

class PointDistance:
def __init__(self, testPoint, points):
self.testPoint = testPoint
self.points = points
self.distances = []
self.avgDistance = 0

def averageDistance(self):
distanceSum = 0
for d in self.distances:
distanceSum = distanceSum+d
self.avgDistance = distanceSum/len(self.distances)
print(self.testPoint.name, "avg distance:", self.avgDistance)
return self.avgDistance

def calcDistances(self):
for p in self.points:
self.distances.append(self.calcDistance(self.testPoint, p))
return

def listDistances(self):
for (point, distance) in zip(self.points, self.distances):
print(self.testPoint.name, "->", point.name, ":", distance)

def calcDistance(self, pointA, pointB):
xAxisDiff = abs(pointA.x - pointB.x)
yAxisDiff = abs(pointA.y - pointB.y)
powered = math.pow(xAxisDiff,2)+math.pow(yAxisDiff,2)
#print("id:",pointA.name, "x:", pointA.x, "y:", pointA.y, "id:",pointB.name, pointB.x, pointB.y, "xdiff:",xAxisDiff, "ydiff:", yAxisDiff, "distance:", math.sqrt(powered))
return math.sqrt(powered)

class Point():
def __init__(self, name, x, y):
self.x=x
self.y=y
self.name = name
self.color = (255,0,0)

def setCoords(self, x, y):
self.x = x
self.y = y

def drawPoint(self, image):
radius = 5
thickness = 2
coords = (self.x, self.y)
cv2.circle(image, coords, radius, self.color, thickness)
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 0.50
coords = (self.x-15, (self.y)+20)
cv2.putText(image, self.name, coords, font, fontScale, self.color, 1, cv2.LINE_AA)

@staticmethod
def drawPoints( image, points):
for point in points:
point.drawPoint(image)

def createImage(height, width):
image = np.zeros((width,height,3), np.uint8)
image[0:height,0:width] = (255,255,255)  # white background
return image

def createListOfPointDistances(anchorPoints):
PointDistances = []
for p in anchorPoints:
PointDistances.append(PointDistance(p, anchorPoints))
return PointDistances

if __name__ == '__main__':
image = createImage(900, 900)
initiatorestimator = InitiatorEstimator()
#anchors = [ Point("0000", 200, 200), Point("1111", 400, 200), Point("2222", 600, 200), Point("3333", 200, 400), Point("4444", 400, 400), Point("5555", 600, 400), Point("6666", 200, 600), Point("7777", 400, 600), Point("8888", 600, 600)]
anchorsTest = [ Point("A", random.randint(100, 400), random.randint(100, 400)), Point("B", random.randint(100, 400), random.randint(100, 400)), Point("C", random.randint(100, random.randint(100, 400)), 200), Point("D", random.randint(100, 400), random.randint(100, 400)),  Point("F", random.randint(100, 400), random.randint(100, 400)), Point("G", random.randint(100, 400), random.randint(100, 400)),Point("H", random.randint(100, 400), random.randint(100, 400)) ,Point("I", random.randint(100, 400), random.randint(100, 400))   ]
pointDistances = createListOfPointDistances(anchorsTest)
for pd in pointDistances:
pd.calcDistances()
Point.drawPoints(image, anchorsTest)
initiatorestimator.findMinAvgPoint(pointDistances).drawPoint(image)
cv2.imshow('frame', image)
cv2.waitKey(0)
```