def get_image_xy_corner(self):
"""get ?artesian coordinates from raster"""
import cv2
if not self.image_path:
return False
image_xy_corners = []
img = cv2.imread(self.image_path, cv2.IMREAD_GRAYSCALE)
imagem = (255 - img)
try:
ret, thresh = cv2.threshold(imagem, 10, 128, cv2.THRESH_BINARY)
try:
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
except Exception:
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = hierarchy[0]
hierarhy_contours = [[] for _ in range(len(hierarchy))]
for fry in range(len(contours)):
currentContour = contours[fry]
currentHierarchy = hierarchy[fry]
cc = []
# epsilon = 0.0005 * cv2.arcLength(contours[len(contours) - 1], True)
approx = cv2.approxPolyDP(currentContour, self.epsilon, True)
if len(approx) > 2:
for c in approx:
cc.append([c[0][0], c[0][1]])
parent_index = currentHierarchy[3]
index = fry if parent_index < 0 else parent_index
hierarhy_contours[index].append(cc)
image_xy_corners = [c for c in hierarhy_contours if len(c) > 0]
return image_xy_corners
except Exception as ex:
self.error(ex)
return image_xy_corners
评论列表
文章目录