Advertisement
MagicWinnie

Untitled

Nov 23rd, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.71 KB | None | 0 0
  1. import numpy as np
  2. import cv2 as cv
  3.  
  4.  
  5.  
  6. coordinates = []
  7. def calc(xa, ya, xb, yb):    
  8.     if ya==yb:
  9.         x=50
  10.         y=(x-xa)*(yb-ya)/(xb-xa)+ya
  11.         x1=-50
  12.         y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
  13.     else:
  14.         y=50
  15.         x=(y-ya)*(xb-xa)/(yb-ya)+xa
  16.         y1=-50
  17.         x1=(y1-ya)*(xb-xa)/(yb-ya)+xa
  18.         if (x<-50 or x>50) and (x1<-50 or x1>50):
  19.             x=50
  20.             y=(x-xa)*(yb-ya)/(xb-xa)+ya
  21.             x1=-50
  22.             y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
  23.         elif (x<-50 or x>50) and (x1>=-50 or x1<=50):
  24.             x=50
  25.             y=(x-xa)*(yb-ya)/(xb-xa)+ya
  26.             if y<-50 or y>50:
  27.                 x=-50
  28.                 y=(x-xa)*(yb-ya)/(xb-xa)+ya
  29.         elif (x1<-50 or x1>50) and (x>=-50 or x<=50):
  30.             x1=50
  31.             y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
  32.             if y1<-50 or y1>50:
  33.                 x1=-50
  34.                 y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
  35.     temp = ((x+50, y+50), (x1+50, y1+50))
  36.     coordinates.append(temp)
  37.  
  38. # routes = eval(input())
  39. 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))]
  40. # 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))]
  41. # 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))]
  42. # 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))]
  43. for i in range(len(routes)):
  44.     calc(routes[i][0][0], routes[i][0][1], routes[i][1][0], routes[i][1][1])
  45.  
  46. hw = 1024
  47. image = np.zeros((hw,hw,3), np.uint8)
  48. s = 0
  49. def coord(x, y):
  50.     # global s
  51.     # if x != float(x) or y != float(x):
  52.     #     s -= 0.5
  53.     return (int(x*hw/100), int(y*hw/100))
  54.  
  55. image = cv.line(image, coord(50+50, -50+50), coord(50+50, 50+50), (255, 255, 255), thickness=2)
  56. image = cv.line(image, coord(-50+50, -50+50), coord(-50+50, 50+50), (255, 255, 255), thickness=2)
  57. image = cv.line(image, coord(-50+50, -50+50), coord(50+50, -50+50), (255, 255, 255), thickness=2)
  58. image = cv.line(image, coord(-50+50, 50+50), coord(50+50, 50+50), (255, 255, 255), thickness=2)
  59.  
  60. for i in range(len(coordinates)):
  61.     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)
  62.  
  63. # cv.imshow("img", image)
  64. # cv.waitKey()
  65. # cv.destroyAllWindows()
  66.  
  67. data = image
  68.  
  69.  
  70. gray = cv.cvtColor(data, cv.COLOR_BGR2GRAY)
  71. edged = cv.Canny(gray, 30, 200)
  72. _, contours, hierarchy = cv.findContours(edged, cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE) #cv.CHAIN_APPROX_TC89_KCOS)#cv.CHAIN_APPROX_SIMPLE)
  73. contours = sorted(contours, key=cv.contourArea)
  74. hierarchy = hierarchy.tolist()
  75. new_contours = []
  76. c = 0
  77.  
  78. for contour in contours:
  79.     area = cv.contourArea(contour)
  80.     # print(area)
  81.     # if area <=2 or area >= 300000:
  82.     #     continue
  83.     new_contours.append(contour)
  84.  
  85.  
  86. contour_sizes = [(cv.contourArea(contour), contour) for contour in new_contours]
  87. # print(contour_sizes)
  88. biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
  89.  
  90.  
  91.  
  92. peri = cv.arcLength(biggest_contour, True)
  93. biggest_contour = cv.approxPolyDP(biggest_contour, 0.04 * peri, True)
  94.  
  95. coord_new = []
  96. for i in range(len(biggest_contour)):
  97.     coord_new.append((biggest_contour[i][0][0]*100/512, biggest_contour[i][0][1]*100/512))
  98.  
  99.  
  100.  
  101. center = (sum(i for i, _ in coord_new)/len(coord_new), sum(j for _, j in coord_new)/len(coord_new))
  102.  
  103. def less(a, b):
  104.     if (a[0] - center[0] >= 0 and b[0] - center[0] < 0):
  105.         return True
  106.     if (a[0] - center[0] < 0 and b[0] - center[0] >= 0):
  107.         return False
  108.     if (a[0] - center[0] == 0 and b[0] - center[0] == 0):
  109.         if (a[1] - center[1] >= 0 or b[1] - center[1] >= 0):
  110.             return a[1] > b[1]
  111.         return b[1] > a[1]
  112.  
  113.     det = (a[0] - center[0]) * (b[1] - center[1]) - (b[0] - center[0]) * (a[1] - center[1])
  114.     if (det < 0):
  115.         return True
  116.     if (det > 0):
  117.         return False
  118.    
  119.     d1 = (a[0] - center[0]) * (a[0] - center[0]) + (a[1] - center[1]) * (a[1] - center[1])
  120.     d2 = (b[0] - center[0]) * (b[0] - center[0]) + (b[1] - center[1]) * (b[1] - center[1])
  121.     return d1 > d2
  122.  
  123. def bubble_sort(nums):
  124.     swapped = True
  125.     while swapped:
  126.         swapped = False
  127.         for i in range(len(nums) - 1):
  128.             if less(nums[i], nums[i + 1]):
  129.                 nums[i], nums[i + 1] = nums[i + 1], nums[i]
  130.                 swapped = True
  131.  
  132. bubble_sort(coord_new)
  133.  
  134.  
  135.  
  136. def PolygonArea(corners):
  137.     n = len(corners)
  138.     area = 0.0
  139.     for i in range(n):
  140.         j = (i + 1) % n
  141.         area += corners[i][0] * corners[j][1]
  142.         area -= corners[j][0] * corners[i][1]
  143.     area = abs(area) / 2.0
  144.     return area
  145.  
  146. def rass(x, y, x1, y1):
  147.     return pow((x-x1)**2 + (y-y1)**2, 0.5)
  148. final_coord = [(0, 0)] * len(biggest_contour)
  149. temp = 1111111
  150. n = 0
  151. new_coord = []
  152. for i in range(len(coordinates)):
  153.     new_coord.append(((coordinates[i][0][0] + 50, coordinates[i][0][1] + 50), (coordinates[i][1][0] + 50, coordinates[i][1][1] + 50)))
  154.  
  155. # new_coord = coordinates.copy()
  156. for i in range(len(coordinates)):
  157.     a = coordinates[i][0][1] - coordinates[i][1][1]
  158.     b = coordinates[i][1][0] - coordinates[i][0][0]
  159.     c = coordinates[i][0][0] * coordinates[i][1][1] - coordinates[i][1][0] * coordinates[i][0][1]
  160.  
  161.     for j in range(len(coordinates)):
  162.         if coordinates[i]==coordinates[j]:
  163.             continue
  164.         a1 = coordinates[j][0][1] - coordinates[j][1][1]
  165.         b1 = coordinates[j][1][0] - coordinates[j][0][0]
  166.         c1 = coordinates[j][0][0] * coordinates[j][1][1] - coordinates[j][1][0] * coordinates[j][0][1]
  167.  
  168.         y = (a*c1 - a1*c)/(a1*b - a*b1)
  169.         x = (-b1*y-c1) / a1
  170.         new_coord.append(((x, y), (x, y)))
  171.  
  172. for i in range(len(coord_new)):
  173.     for j in range(len(new_coord)):
  174.         if rass(coord_new[i][0], coord_new[i][1], new_coord[j][0][0], new_coord[j][0][1]) < temp:
  175.             temp = rass(coord_new[i][0], coord_new[i][1], new_coord[j][0][0], new_coord[j][0][1])
  176.             final_coord[n] = (new_coord[j][0][0], new_coord[j][0][1])
  177.         if rass(coord_new[i][0], coord_new[i][1], new_coord[j][1][0], new_coord[j][1][1]) < temp:
  178.             temp = rass(coord_new[i][0], coord_new[i][1], new_coord[j][1][0], new_coord[j][1][1])
  179.             final_coord[n] = (new_coord[j][1][0], new_coord[j][1][1])
  180.     n += 1
  181. print(final_coord)
  182. print(PolygonArea(coord_new)*1788.92/7045.879364013672)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement