import os
import argparse
from osgeo import ogr,gdal
from matplotlib import pyplot as plt
from osgeo import gdal
import matplotlib
import matplotlib.patches as patches
from osgeo import gdal
from PIL import Image
from scipy.spatial import cKDTree
import numpy as np
from tools.DotaOperator import DotaObj,createDota,readDotaFile,writerDotaFile
import argparse
import math
from math import ceil, floor

pngpath=r"D:\港口\切片结果\Geo_bc2-sm-org-vv-20231016t135315-008424-0020e8-01_3.png"
txtpath=r"D:\港口\切片结果\Geo_bc2-sm-org-vv-20231016t135315-008424-0020e8-01_3.txt"



pngdata=np.array(Image.open(pngpath))
dotalist=readDotaFile(txtpath)

plt.figure()
plt.imshow(pngdata)
# 绘制每个目标的矩形框并标注坐标
for i in range(len(dotalist)):
    # 提取x和y坐标
    x_coords = [dotalist[i].x1, dotalist[i].x2, dotalist[i].x3, dotalist[i].x4]
    y_coords = [dotalist[i].y1, dotalist[i].y2, dotalist[i].y3, dotalist[i].y4]

    # 计算最小外接矩形（AABB）
    x_min, x_max = min(x_coords), max(x_coords)
    y_min, y_max = min(y_coords), max(y_coords)
    width = x_max - x_min
    height = y_max - y_min

    # 绘制无填充矩形框（仅红色边框）
    rect = patches.Rectangle(
        (x_min, y_min), width, height,
        linewidth=2, edgecolor='red', facecolor='none'  # 关键：facecolor='none'
    )
    plt.gca().add_patch(rect)

    # ax.annotate(f'({x},{y})', xy=(x, y), xytext=(5, 5),
    #                textcoords='offset points', fontsize=10,
    #                bbox=dict(boxstyle='round,pad=0.5', fc='white', alpha=0.8))

    # 在矩形中心标注目标编号
    center_x = sum(x_coords) / 4
    center_y = sum(y_coords) / 4
    plt.text(center_x, center_y, str(i),
            ha='center', va='center', fontsize=6, color='red')


plt.show()



