# -- coding: utf-8 -- import os import sys import threading import ctypes from ctypes import * import platform import time import cv2 # 4.2.0.32 import numpy as np WIDTH = 9344 HEIGH = 7000 step = 40 rect_conner_points = [] edge_rect_points = [] # 生成一个随机颜色 def generate_random_color(): color_bgr = np.random.randint(0, 255, size=(3, )) return color_bgr.tolist() def cal_edge_rect(p1, p2, idx, im): print(p1, p2) tmp = [] if idx == 1: tmp.append([p1[0]-step,p1[1]-step]) tmp.append([p1[0]-step,p1[1]+step*3]) tmp.append([p2[0]+step*3,p2[1]-step]) tmp.append([p2[0]-step,p2[1]-step]) elif idx == 2: tmp.append([p1[0]+step,p1[1]-step]) tmp.append([p1[0]-step*3,p1[1]-step]) tmp.append([p2[0]+step,p2[1]+step*3]) tmp.append([p2[0]+step,p2[1]-step]) elif idx == 3: tmp.append([p1[0]+step,p1[1]+step]) tmp.append([p1[0]+step,p1[1]-step*3]) tmp.append([p2[0]-step*3,p2[1]+step]) tmp.append([p2[0]+step,p2[1]+step]) else: tmp.append([p1[0]+step*3,p1[1]+step]) tmp.append([p1[0]-step,p1[1]+step]) tmp.append([p2[0]-step,p2[1]+step]) tmp.append([p2[0]-step,p2[1]-step*3]) edge_rect_points.append(tmp) pts = np.array(tmp, np.int32) # pts = [pts] color = (0, 255, 0) color = generate_random_color() # if idx == 4: cv2.polylines(im, [pts], True, color, 5) def cal_edge_rect2(p1, p2, idx, im): print(p1, p2) tmp = [] if idx == 1: tmp.append([p1[0]-step,p1[1]]) tmp.append([p1[0]+step,p1[1]]) tmp.append([p2[0],p2[1]+step]) tmp.append([p2[0],p2[1]-step]) elif idx == 2: tmp.append([p1[0],p1[1]-step]) tmp.append([p1[0],p1[1]+step]) tmp.append([p2[0]-step,p2[1]]) tmp.append([p2[0]+step,p2[1]]) elif idx == 3: tmp.append([p1[0]+step,p1[1]]) tmp.append([p1[0]-step,p1[1]]) tmp.append([p2[0],p2[1]-step]) tmp.append([p2[0],p2[1]+step]) else: tmp.append([p1[0],p1[1]+step]) tmp.append([p1[0],p1[1]-step]) tmp.append([p2[0]+step,p2[1]]) tmp.append([p2[0]-step,p2[1]]) edge_rect_points.append(tmp) pts = np.array(tmp, np.int32) # pts = [pts] color = (0, 255, 0) color = generate_random_color() # if idx == 4: cv2.polylines(im, [pts], True, color, 5) with open('rct.txt', 'r') as file: # im = np.zeros((HEIGH,WIDTH,3), np.uint8) im = cv2.imread("cloud.png") # im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # _,im= cv2.threshold(im, 20, 255, cv2. THRESH_BINARY) # im = cv2.merge([im, im, im]) while True: line1 = file.readline() if not line1: break line2 = file.readline() x = int(line1) + 80 +40 y = int(line2) + 40 rect_conner_points.append([x, y]) cv2.circle(im, (x,y), 25, (255, 0, 0) , -1) # print(rect_conner_points) rpts = rect_conner_points cal_edge_rect2(rpts[0], rpts[1], 1, im) cal_edge_rect2(rpts[1], rpts[2], 2, im) cal_edge_rect2(rpts[2], rpts[3], 3, im) cal_edge_rect2(rpts[3], rpts[0], 4, im) cal_edge_rect2(rpts[4], rpts[5], 1, im) cal_edge_rect2(rpts[5], rpts[6], 2, im) cal_edge_rect2(rpts[6], rpts[7], 3, im) cal_edge_rect2(rpts[7], rpts[4], 4, im) print(edge_rect_points) im = cv2.resize(im, (1800, 1400)) cv2.imshow("im", im) cv2.waitKey(0) cv2.destroiyAllWindows() with open('self_define_point.txt', 'w') as file2: print("11") for i in range(8): for y in range(4): print(str(edge_rect_points[i][y][0])) print(str(edge_rect_points[i][y][1])) file2.write(str(edge_rect_points[i][y][0])+'\n') file2.write(str(edge_rect_points[i][y][1])+'\n') exit()