Widen_boundary/main2.py

185 lines
3.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: UTF-8 -*-
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : 多边形等距缩放.py
@data 2021/7/5 15:53
@Desciption :
@Version :
@License :
'''
import cv2
import numpy as np
def scale(data, sec_dis):
"""多边形等距缩放
Args:
data: 多边形按照逆时针顺序排列的的点集
sec_dis: 缩放距离
Returns:
缩放后的多边形点集
"""
num = len(data)
scal_data = []
for i in range(num):
x1 = data[(i) % num][0] - data[(i - 1) % num][0]
y1 = data[(i) % num][1] - data[(i - 1) % num][1]
x2 = data[(i + 1) % num][0] - data[(i) % num][0]
y2 = data[(i + 1) % num][1] - data[(i) % num][1]
d_A = (x1 ** 2 + y1 ** 2) ** 0.5
d_B = (x2 ** 2 + y2 ** 2) ** 0.5
Vec_Cross = (x1 * y2) - (x2 * y1)
if (d_A * d_B==0):
continue
sin_theta = Vec_Cross / (d_A * d_B)
if (sin_theta==0):
continue
dv = sec_dis / sin_theta
v1_x = (dv / d_A) * x1
v1_y = (dv / d_A) * y1
v2_x = (dv / d_B) * x2
v2_y = (dv / d_B) * y2
PQ_x = v1_x - v2_x
PQ_y = v1_y - v2_y
Q_x = data[(i) % num][0] + PQ_x
Q_y = data[(i) % num][1] + PQ_y
scal_data.append([Q_x, Q_y])
return scal_data
data = [[454 , 76],
[448 ,78],
[444, 81],
[440 , 85],
[438, 90],
[437, 96],
[436 ,101],
[434, 107],
[432 ,112],
[431 ,117],
[430 ,123],
[429, 129],
[428, 134],
[427, 140],
[427, 145],
[427, 151],
[427 ,157],
[427 ,163],
[427, 169],
[427, 175],
[427, 181],
[427, 187],
[428, 193],
[428 ,199],
[429, 204],
[429, 210],
[429 ,216],
[430, 222],
[431, 227],
[431, 233],
[431, 239],
[432, 245],
[433, 250],
[434, 256],
[435 ,261],
[436 ,267],
[437 ,272],
[438, 278],
[439, 283],
[441, 289],
[442, 294],
[443, 300],
[445, 305],
[446, 310],
[448, 316],
[450, 321],
[453, 330],
[461, 334],
[466, 336],
[471, 338],
[477 ,340],
[482, 340],
[488, 341],
[494, 341],
[500, 341],
[506 ,341],
[511 ,340],
[517, 339],
[523 ,338],
[528, 337],
[533, 335],
[539, 333],
[544, 331],
[549, 329],
[553, 326],
[558, 322],
[562, 318],
[566, 313],
[568, 308],
[569, 303],
[570, 297],
[570, 291],
[569, 285],
[569, 280],
[568, 274],
[567, 268],
[566 ,263],
[566 ,257],
[564 ,252],
[564 ,246],
[562, 241],
[561, 235],
[560, 230],
[560, 224],
[558 ,219],
[558, 213],
[556, 207],
[555, 202],
[554, 196],
[553, 191],
[552, 185],
[550, 180],
[549, 174],
[548, 169],
[547 ,164],
[545 ,158],
[543 ,153],
[541 ,148],
[539 ,143],
[536 ,138],
[533 ,133],
[530, 128],
[528 ,124],
[524 ,119],
[520 ,115],
[515, 111],
[511, 108],
[506 ,106],
[501 ,104],
[495 ,102],
[490, 101],
[485 , 99],
[480 , 97],
[475 , 93],
[471 , 89],
[468 , 84],
[465, 80],
[460 , 76]]
data1 = scale(data,-100)
print(data1)
temp = np.ones((1300,1000,3), np.uint8) * 255
#cv2.polylines(temp, data1, 1, 255)
cv2.polylines(temp , [np.array(data , dtype=np.int32)], True, (255, 0, 0), 1)
cv2.polylines(temp , [np.array(data1 , dtype=np.int32)], True, (0, 0, 255), 1)
cv2.imshow ("img",temp)
cv2.imwrite("1.jpg",temp)
cv2.waitKey(0)