101 lines
3.0 KiB
Python
101 lines
3.0 KiB
Python
# -*- coding: UTF-8 -*-
|
||
"""
|
||
@Function :标记工具
|
||
"""
|
||
import cv2
|
||
from tool.algorithm.algtools.CoordinateTransformation import *
|
||
import numpy as np
|
||
|
||
|
||
# mouse callback function
|
||
def draw_circle(event, x, y, flags, param):
|
||
if event == cv2.EVENT_LBUTTONDOWN:
|
||
param.appendPoint(x, y)
|
||
#print("{0},{1}".format(x, y))
|
||
|
||
class MarkRange:
|
||
def __init__(self,img=None,imgWidth=512,imgHeight=512):
|
||
self._PointList=[]
|
||
if img is None:
|
||
self._img = np.zeros((imgWidth, imgHeight, 3), np.uint8)
|
||
else:
|
||
self._img=img.copy()
|
||
self._imgShow=self._img.copy()
|
||
self._MarkFlag=False
|
||
|
||
def doMark(self,pointNum=-1):
|
||
cv2.namedWindow('image')
|
||
cv2.setMouseCallback('image', draw_circle,self)
|
||
while (True):
|
||
cv2.imshow('image', self._imgShow)
|
||
code=cv2.waitKey(20)
|
||
if code==115:
|
||
break
|
||
if pointNum!=-1 and pointNum==len(self._PointList):
|
||
break
|
||
if self._MarkFlag==True:
|
||
self._MarkFlag=False
|
||
self._drawPoint()
|
||
|
||
cv2.destroyAllWindows()
|
||
return self._PointList
|
||
|
||
def appendPoint(self,x,y):
|
||
self._PointList.append([x,y])
|
||
self._MarkFlag=True
|
||
|
||
def _drawPoint(self):
|
||
for p in self._PointList:
|
||
cv2.circle(self._imgShow,p,2,[0,0,255],cv2.FILLED)
|
||
|
||
def Mark():
|
||
img = np.zeros((512, 512, 3), np.uint8)
|
||
markRag=MarkRange(img,400,300)
|
||
|
||
markRag.doMark(4)
|
||
|
||
def GetlonlatFromMark(markRag,ref_tif_path):
|
||
counterSize = [2]
|
||
pointNum=10
|
||
tif = ref_tif_path
|
||
# bmp = 'polarisation_fraction.bmp'
|
||
# img = cv2.imread(bmp,1)
|
||
# if img is None:
|
||
# print("img is none")
|
||
# return
|
||
outline = []
|
||
for item in counterSize:
|
||
# markRag = MasrkRange(img)
|
||
plist = markRag.doMark(-1) # 点击‘s’退出标记
|
||
if len(plist) > 0:
|
||
outline.append(plist)
|
||
|
||
gdal.AllRegister()
|
||
# dataset = gdal.Open(r"D:\\DATA\\雷达测试\\GaoFen3_20200528_HH_DB.tif")
|
||
dataset = gdal.Open(tif)
|
||
print('数据投影:')
|
||
print(dataset.GetProjection())
|
||
print('数据的大小(行,列):')
|
||
print('(%s %s)' % (dataset.RasterYSize, dataset.RasterXSize))
|
||
lonlatList=[]
|
||
fw = open('coords.txt', "w")
|
||
poly = 'POLYGON(('
|
||
for item in outline:
|
||
for cell in item:
|
||
projs = imagexy2geo(dataset, cell[1], cell[0])
|
||
coords = geo2lonlat(dataset,projs[0], projs[1])
|
||
coordstr="{0} {1},".format(coords[1],coords[0])
|
||
poly = poly + str(coordstr)
|
||
print(coordstr)
|
||
|
||
poly = poly[:-1]
|
||
poly +='))'
|
||
print(poly)
|
||
fw.write(poly)
|
||
fw.write('\n')
|
||
|
||
if __name__ == '__main__':
|
||
# mr = MarkRange(cv2.imread('polarisation_fraction.bmp',1))
|
||
mr = MarkRange(cv2.imread('D:\\1.tif', 1))
|
||
ref_tif_path = 'D:\\1.tif'
|
||
GetlonlatFromMark(mr,ref_tif_path) # 在图像上标记,输出图像轮廓点; 点击's'退出标记 |