Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import cv2 as cv
- coordinates = []
- def calc(xa, ya, xb, yb):
- if ya==yb:
- x=50
- y=(x-xa)*(yb-ya)/(xb-xa)+ya
- x1=-50
- y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
- else:
- y=50
- x=(y-ya)*(xb-xa)/(yb-ya)+xa
- y1=-50
- x1=(y1-ya)*(xb-xa)/(yb-ya)+xa
- if (x<-50 or x>50) and (x1<-50 or x1>50):
- x=50
- y=(x-xa)*(yb-ya)/(xb-xa)+ya
- x1=-50
- y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
- elif (x<-50 or x>50) and (x1>=-50 or x1<=50):
- x=50
- y=(x-xa)*(yb-ya)/(xb-xa)+ya
- if y<-50 or y>50:
- x=-50
- y=(x-xa)*(yb-ya)/(xb-xa)+ya
- elif (x1<-50 or x1>50) and (x>=-50 or x<=50):
- x1=50
- y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
- if y1<-50 or y1>50:
- x1=-50
- y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
- temp = ((x+50, y+50), (x1+50, y1+50))
- coordinates.append(temp)
- # routes = eval(input())
- routes = [((6.1787904345272295, -2.783403534528972), (-39.41911792778795, -44.46664131009641)), ((-18.877526127698374, -37.79337985319449), (-43.16229222458732, -11.702214923983043)), ((18.545029908950298, 30.411030797018157), (28.71378383564337, -48.09601456079844)), ((-44.26912946335746, 19.322383234499057), (37.516483240799374, -11.696154543641171)), ((-30.754994755371634, 45.908258695198256), (43.89609857499805, -4.480480985021586)), ((19.22444870414813, 4.411470653736991), (10.25179389103532, 21.757063915662528))]
- # routes = [((21.337895638059678, -16.442367725281606), (-8.715792454681463, 21.132926054832893)), ((22.75621365883707, 34.97874379715792), (-29.43734143127147, -14.824502111040125)), ((-23.404830443016365, -28.130608388886856), (5.9283479484905754, 14.43924947016319)), ((24.22911467186799, -23.018459318856433), (47.53081104284033, 34.624410165900215)), ((-31.643739551995075, -25.552166581429216), (48.40715662452317, 47.374442610207495)), ((-17.72318329241419, 23.49398037734896), (1.8700077159436574, -49.263732959123075)), ((-29.561045328207914, -30.150361671475977), (10.827431699830804, 3.5598860474482663)), ((-47.875058983329666, 1.2098027190665661), (32.96832290441219, -6.382630291842297)), ((43.81649943236182, -33.5678611952183), (18.262254982578085, 13.781617674118472))]
- # routes = [((18.871756653436506, 27.79571861871969), (-40.113542069413874, 1.7008648089895786)), ((41.93708694624851, -37.67496829048245), (-46.73151632195197, -42.192891918079866)), ((-47.86479693469416, -1.0094512406469782), (-47.641517312849345, 19.629063768788285)), ((28.4459261743296, 25.477440521962336), (-23.704376038672613, 22.4930590438003)), ((-48.69837347204539, -19.093162927334884), (-25.776672293227975, -30.9566554833398)), ((23.30272351696175, 11.555952965204398), (-5.787381319241767, 38.24530139111856)), ((10.422310749552523, 13.96670550524236), (1.7247436433874626, 22.020906526051235)), ((42.46639880530908, 31.425565616836124), (22.47889569733114, -48.090218374651705))]
- # routes = [((-11.537254359032502, 41.24930555285968), (32.86670185359557, -11.819080231272764)), ((17.61595021731175, 48.71375130842715), (7.604915647649001, -10.323499268412853)), ((28.853123163567403, 24.685302689793005), (-38.226472184529044, 19.512735878888947)), ((-1.344337081740889, -47.173161203199896), (3.3636801153457583, -42.426926634089526)), ((-26.371067142408567, 2.207864196354791), (-14.34682667288093, -43.31707089381798)), ((-30.922957465589217, 37.58341863433127), (1.8202214254561042, -5.41852226002996)), ((43.64891153030952, 46.173510118805126), (-9.552485196208458, 24.515163947321412)), ((35.395095766493995, 5.685328887612663), (0.3809976895955529, 42.65858914196967)), ((39.66950224581538, -41.911005856117654), (-21.29889568324044, -33.57813394943881))]
- for i in range(len(routes)):
- calc(routes[i][0][0], routes[i][0][1], routes[i][1][0], routes[i][1][1])
- hw = 1024
- image = np.zeros((hw,hw,3), np.uint8)
- s = 0
- def coord(x, y):
- # global s
- # if x != float(x) or y != float(x):
- # s -= 0.5
- return (int(x*hw/100), int(y*hw/100))
- image = cv.line(image, coord(50+50, -50+50), coord(50+50, 50+50), (255, 255, 255), thickness=2)
- image = cv.line(image, coord(-50+50, -50+50), coord(-50+50, 50+50), (255, 255, 255), thickness=2)
- image = cv.line(image, coord(-50+50, -50+50), coord(50+50, -50+50), (255, 255, 255), thickness=2)
- image = cv.line(image, coord(-50+50, 50+50), coord(50+50, 50+50), (255, 255, 255), thickness=2)
- for i in range(len(coordinates)):
- image = cv.line(image, coord(coordinates[i][0][0], coordinates[i][0][1]), coord(coordinates[i][1][0], coordinates[i][1][1]), (255, 255, 255), thickness=2)
- # cv.imshow("img", image)
- # cv.waitKey()
- # cv.destroyAllWindows()
- data = image
- gray = cv.cvtColor(data, cv.COLOR_BGR2GRAY)
- edged = cv.Canny(gray, 30, 200)
- _, contours, hierarchy = cv.findContours(edged, cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE) #cv.CHAIN_APPROX_TC89_KCOS)#cv.CHAIN_APPROX_SIMPLE)
- contours = sorted(contours, key=cv.contourArea)
- hierarchy = hierarchy.tolist()
- new_contours = []
- c = 0
- for contour in contours:
- area = cv.contourArea(contour)
- # print(area)
- # if area <=2 or area >= 300000:
- # continue
- new_contours.append(contour)
- contour_sizes = [(cv.contourArea(contour), contour) for contour in new_contours]
- # print(contour_sizes)
- biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
- peri = cv.arcLength(biggest_contour, True)
- biggest_contour = cv.approxPolyDP(biggest_contour, 0.04 * peri, True)
- coord_new = []
- for i in range(len(biggest_contour)):
- coord_new.append((biggest_contour[i][0][0]*100/512, biggest_contour[i][0][1]*100/512))
- center = (sum(i for i, _ in coord_new)/len(coord_new), sum(j for _, j in coord_new)/len(coord_new))
- def less(a, b):
- if (a[0] - center[0] >= 0 and b[0] - center[0] < 0):
- return True
- if (a[0] - center[0] < 0 and b[0] - center[0] >= 0):
- return False
- if (a[0] - center[0] == 0 and b[0] - center[0] == 0):
- if (a[1] - center[1] >= 0 or b[1] - center[1] >= 0):
- return a[1] > b[1]
- return b[1] > a[1]
- det = (a[0] - center[0]) * (b[1] - center[1]) - (b[0] - center[0]) * (a[1] - center[1])
- if (det < 0):
- return True
- if (det > 0):
- return False
- d1 = (a[0] - center[0]) * (a[0] - center[0]) + (a[1] - center[1]) * (a[1] - center[1])
- d2 = (b[0] - center[0]) * (b[0] - center[0]) + (b[1] - center[1]) * (b[1] - center[1])
- return d1 > d2
- def bubble_sort(nums):
- swapped = True
- while swapped:
- swapped = False
- for i in range(len(nums) - 1):
- if less(nums[i], nums[i + 1]):
- nums[i], nums[i + 1] = nums[i + 1], nums[i]
- swapped = True
- bubble_sort(coord_new)
- def PolygonArea(corners):
- n = len(corners)
- area = 0.0
- for i in range(n):
- j = (i + 1) % n
- area += corners[i][0] * corners[j][1]
- area -= corners[j][0] * corners[i][1]
- area = abs(area) / 2.0
- return area
- def rass(x, y, x1, y1):
- return pow((x-x1)**2 + (y-y1)**2, 0.5)
- final_coord = [(0, 0)] * len(biggest_contour)
- temp = 1111111
- n = 0
- new_coord = []
- for i in range(len(coordinates)):
- new_coord.append(((coordinates[i][0][0] + 50, coordinates[i][0][1] + 50), (coordinates[i][1][0] + 50, coordinates[i][1][1] + 50)))
- # new_coord = coordinates.copy()
- for i in range(len(coordinates)):
- a = coordinates[i][0][1] - coordinates[i][1][1]
- b = coordinates[i][1][0] - coordinates[i][0][0]
- c = coordinates[i][0][0] * coordinates[i][1][1] - coordinates[i][1][0] * coordinates[i][0][1]
- for j in range(len(coordinates)):
- if coordinates[i]==coordinates[j]:
- continue
- a1 = coordinates[j][0][1] - coordinates[j][1][1]
- b1 = coordinates[j][1][0] - coordinates[j][0][0]
- c1 = coordinates[j][0][0] * coordinates[j][1][1] - coordinates[j][1][0] * coordinates[j][0][1]
- y = (a*c1 - a1*c)/(a1*b - a*b1)
- x = (-b1*y-c1) / a1
- new_coord.append(((x, y), (x, y)))
- for i in range(len(coord_new)):
- for j in range(len(new_coord)):
- if rass(coord_new[i][0], coord_new[i][1], new_coord[j][0][0], new_coord[j][0][1]) < temp:
- temp = rass(coord_new[i][0], coord_new[i][1], new_coord[j][0][0], new_coord[j][0][1])
- final_coord[n] = (new_coord[j][0][0], new_coord[j][0][1])
- if rass(coord_new[i][0], coord_new[i][1], new_coord[j][1][0], new_coord[j][1][1]) < temp:
- temp = rass(coord_new[i][0], coord_new[i][1], new_coord[j][1][0], new_coord[j][1][1])
- final_coord[n] = (new_coord[j][1][0], new_coord[j][1][1])
- n += 1
- print(final_coord)
- print(PolygonArea(coord_new)*1788.92/7045.879364013672)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement