Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import math
- import cv2
- from numpy import ndarray
- from Lista02 import FuncoesML as fun
- #lendo a imagem
- circles = []
- circles = fun.loadFiles('C:/Users/Auricelia/Desktop/DataSetsML/New_shapes_dataset/hexagons/*.jpg',circles)
- #convertendo para níveis de cinza
- circleeee = circles[0]
- circles = fun.grayConversion(circles)
- #aplicando o filtro gaussiano
- circles = fun.blurConversion(circles, 5, 0)
- #convertendo para binária
- circles = fun.binaryConversion(circles, 255, 31)
- #invertendo as cores
- circles = fun.invertConversion(circles)
- # extraindo momentos da imagem
- im2, countours, hierachy = cv2.findContours(circles[0], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- cv2.drawContours(circleeee, countours, -1, (0,255,0), 3)
- peri = cv2.arcLength(countours[0], True) # perimetro
- aproxx = cv2.approxPolyDP(countours[0], 0.04 * peri, True) # vertices
- print('Vertices list', aproxx)
- print('shape', aproxx.shape)
- print('dtype',aproxx.dtype)
- print('type',type(aproxx))
- x1 = ndarray.item(aproxx,(0,0,0)) # recupera o primeiro elemento do ndarray
- y1 = ndarray.item(aproxx,(0,0,1))
- x2 = ndarray.item(aproxx,(1,0,0))
- y2 = ndarray.item(aproxx,(1,0,1))
- bla = np.reshape(aproxx, (aproxx.shape[0], (aproxx.shape[1]*aproxx.shape[2])))
- print('approxPolyDp resultado', aproxx)
- print('angle',fun.angleBetweenPoints(x1,y1,x2,y2))
- def angle3pt(a, b, c):
- """Counterclockwise angle in degrees by turning from a to c around b
- Returns a float between 0.0 and 360.0"""
- ang = math.degrees(
- math.atan2(c[1] - b[1], c[0] - b[0]) - math.atan2(a[1] - b[1], a[0] - b[0]))
- return ang + 360 if ang < 0 else ang
- if(len(bla) >= 3):
- for x in range(len(bla)):
- if x == 0:
- print("CASO INICIAL")
- bla3 = angle3pt(bla[x],bla[x+1],bla[len(bla)-1])
- print(bla3)
- #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])
- if x == len(bla)-1:
- print("CASO FINAL")
- bla4 = angle3pt(bla[x], bla[0], bla[x-1])
- print(bla4)
- print("v0= ", bla[x][0], ",", bla[x][1], " v1= ", bla[0][0], ",", bla[0][1], " v2= ", bla[x-1][0], "," , bla[x-1][1])
- if x > 0 and x < len(bla)-1 :
- print("RESTO DOS CASOS")
- bla5 = angle3pt(bla[x],bla[x+1],bla[x-1])
- print(bla5)
- bla5 = 0
- 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])
- cv2.imshow("a",circleeee)
- cv2.waitKey()
- print()
- print(bla[1][0])
- print('a = ',aproxx[len(aproxx) - 1][0],' ',aproxx[len(aproxx)-1][1])
- print('b = ',)
- 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