Advertisement
11eimilia11

ble

Feb 12th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. import numpy as np
  2. import math
  3. import cv2
  4. from numpy import ndarray
  5. from Lista02 import FuncoesML as fun
  6.  
  7. #lendo a imagem
  8. circles = []
  9. circles = fun.loadFiles('C:/Users/Auricelia/Desktop/DataSetsML/New_shapes_dataset/hexagons/*.jpg',circles)
  10.  
  11. #convertendo para níveis de cinza
  12.  
  13. circleeee = circles[0]
  14. circles = fun.grayConversion(circles)
  15.  
  16. #aplicando o filtro gaussiano
  17. circles = fun.blurConversion(circles, 5, 0)
  18.  
  19. #convertendo para binária
  20. circles = fun.binaryConversion(circles, 255, 31)
  21.  
  22. #invertendo as cores
  23. circles = fun.invertConversion(circles)
  24.  
  25. # extraindo momentos da imagem
  26.  
  27. im2, countours, hierachy = cv2.findContours(circles[0], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  28. cv2.drawContours(circleeee, countours, -1, (0,255,0), 3)
  29.  
  30. peri = cv2.arcLength(countours[0], True)  # perimetro
  31.  
  32. aproxx = cv2.approxPolyDP(countours[0], 0.04 * peri, True)  # vertices
  33.  
  34.  
  35. print('Vertices list', aproxx)
  36. print('shape', aproxx.shape)
  37. print('dtype',aproxx.dtype)
  38. print('type',type(aproxx))
  39.  
  40. x1 = ndarray.item(aproxx,(0,0,0))         # recupera o primeiro elemento do ndarray
  41. y1 = ndarray.item(aproxx,(0,0,1))
  42. x2 = ndarray.item(aproxx,(1,0,0))
  43. y2 = ndarray.item(aproxx,(1,0,1))
  44.  
  45. bla = np.reshape(aproxx, (aproxx.shape[0], (aproxx.shape[1]*aproxx.shape[2])))
  46.  
  47. print('approxPolyDp resultado', aproxx)
  48. print('angle',fun.angleBetweenPoints(x1,y1,x2,y2))
  49. def angle3pt(a, b, c):
  50.     """Counterclockwise angle in degrees by turning from a to c around b
  51.        Returns a float between 0.0 and 360.0"""
  52.     ang = math.degrees(
  53.         math.atan2(c[1] - b[1], c[0] - b[0]) - math.atan2(a[1] - b[1], a[0] - b[0]))
  54.     return ang + 360 if ang < 0 else ang
  55.  
  56.  
  57. if(len(bla) >= 3):
  58.  
  59.     for x in range(len(bla)):
  60.  
  61.         if x == 0:
  62.             print("CASO INICIAL")
  63.             bla3 = angle3pt(bla[x],bla[x+1],bla[len(bla)-1])
  64.             print(bla3)
  65.             #print("v0= ", bla[x][0] , ",", bla[x][1], " v1= ", bla[x+1][0],",", bla[x+1][1], " v2= ", bla[len(bla)-1][0], "," , bla[len(bla)-1][1])
  66.  
  67.         if x == len(bla)-1:
  68.             print("CASO FINAL")
  69.             bla4 = angle3pt(bla[x], bla[0], bla[x-1])
  70.             print(bla4)
  71.             print("v0= ", bla[x][0], ",", bla[x][1], " v1= ", bla[0][0], ",", bla[0][1], " v2= ", bla[x-1][0], "," , bla[x-1][1])
  72.  
  73.         if x > 0 and x < len(bla)-1 :
  74.             print("RESTO DOS CASOS")
  75.             bla5 = angle3pt(bla[x],bla[x+1],bla[x-1])
  76.             print(bla5)
  77.             bla5 = 0
  78.             print("v0= ", bla[x][0], ",", bla[x][1], " v1= ", bla[x+1][0], ",", bla[x+1][1], " v2= ", bla[x-1][0], ",", bla[x - 1][1])
  79.  
  80.  
  81. cv2.imshow("a",circleeee)
  82. cv2.waitKey()
  83. print()
  84. print(bla[1][0])
  85.  
  86.  
  87. print('a = ',aproxx[len(aproxx) - 1][0],' ',aproxx[len(aproxx)-1][1])
  88. print('b = ',)
  89. print('angle',angle3pt((aproxx[len(aproxx) - 1][0],aproxx[len(aproxx)-1][1]),(aproxx[0][0],aproxx[0][1]),(aproxx[1][0],aproxx[1][1])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement