1. python如何定義坐標
def coordinate(x, y):
if x>0:
if y>0:
a = 1
else:
a = 2
else:
if y>0:
a = 3
else:
a = 4
return a #根據x,y返回aif __name__ == "__main":
print 'Please insert X,Y'
x = input('Please insert X')
y = input('Please insert Y')
print coordinate(x,y)
2. python將高斯坐標轉換經緯度 經緯度坐標與高斯坐標的轉換代碼
#網上搜來的
# 高斯坐標轉經緯度演算法 # B=大地坐標X # C=大地坐標Y # IsSix=6度帶或3度帶
import math
def GetLatLon2(B, C,IsSix):
#帶號
D = math.trunc( C/ 1000000)
#中央經線(單位:弧度)
K = 0
if IsSix:
K = D * 6 - 3 #6度帶計算
else:
K = D * 3 #3度帶計算
L = B/(6378245*(1-0.006693421623)*1.0050517739)
M = L +(0.00506237764 * math.sin(2*L)/2-0.00001062451*math.sin(4*L)/4+0.0000002081*math.sin(6*L)/6)/1.0050517739
N = L +(0.00506237764 * math.sin(2*M)/2-0.00001062451*math.sin(4*M)/4+0.0000002081*math.sin(6*M)/6)/1.0050517739
O = L +(0.00506237764 * math.sin(2*N)/2-0.00001062451*math.sin(4*N)/4+0.0000002081*math.sin(6*N)/6)/1.0050517739
P = L +(0.00506237764 * math.sin(2*O)/2-0.00001062451*math.sin(4*O)/4+0.0000002081*math.sin(6*O)/6)/1.0050517739
Q = L +(0.00506237764 * math.sin(2*P)/2-0.00001062451*math.sin(4*P)/4+0.0000002081*math.sin(6*P)/6)/1.0050517739
R = L +(0.00506237764 * math.sin(2*Q)/2-0.00001062451*math.sin(4*Q)/4+0.0000002081*math.sin(6*Q)/6)/1.0050517739
S = math.tan(R)
T = 0.006738525415*(math.cos(R))**2
U = 6378245/math.sqrt(1-0.006693421623*(math.sin(R))**2)
V = 6378245*(1-0.006693421623)/(math.sqrt((1-0.006693421623*(math.sin(R))**2)))**3
W = 5+3*S**2+T-9*T*S**2
X = 61+90*S**2+45*S**4
Y = 1+2*S**2+T**2
Z = 5+28*S**2+24*S**4+6*T+8*T*S**2
Lat= (180/math.pi)*(R-(C-D*1000000-500000)**2*S/(2*V*U)+(C-D*1000000-500000)**4*W/(24*U**3*V)-(C-D*1000000-500000)**6*X/(7200*U**5*V))
Lon= (180/math.pi)*(C-D*1000000-500000)*(1-(C-D*1000000-500000)**2*Y/(6*U**2)+(C-D*1000000-500000)**4*Z/(120*U**4))/(U*math.cos(P))
Lat = Lat
Lon = K + Lon
return (Lon, Lat)
3. python怎麼在一群點集中,提取中心坐標
回答你的問題費老大勁了,opencv用的不熟
我運行輸出:
row=14,col=5
14 5 248 242 234 237
gray2 crop: 220 232 219 5
中心點坐標(109,108),圖片寬高(w=218, h=217)
[95, 134] [83, 173]
斜率k= 3.25
#解題思路:因為我下載的你的圖片,黑色周圍還有白色,所以先去除了周圍的白色,保證只有黑色部分圖片
#對圖片轉灰度圖,這樣方便計算,每個像素點的值就是一個0-255的值,0為黑色,255為白色
#然後獲得灰度圖的row和col,與正常思維的width和height相反,row對應height,col對應width
#遍歷row和col,先獲得第一個白點,再獲得最後一個白點,然後根據這兩個坐標執行tan計算斜率k值
#讀取圖像
img2=cv2.imread("../img/blackWhite.png")
#轉成單通道黑白圖
gray=cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
cv2.imshow('img2',img2)
cv2.imshow('gray',gray)
sp=gray.shape
rows=sp[0]
cols=sp[1]
cr=0
cl=0
#裁剪左邊和上邊空白
forrowinrange(rows):
isBreak=False
forcolinrange(cols):
ifgray[row,col]==0:
cr=row
cl=col
print(" row=%d,col=%d"%(row,col))
isBreak=True
break
#print(gray[row,col],end='')
#if(col==cols-1):
#print(" row=%d,col=%d-------------------------------"%(row,col))
ifisBreak==True:
break
print(cr,cl,rows,cols,rows-cr,cols-cl)
gray2=gray[cr:rows-cr,cl:cols-cl]
cv2.imshow('gray2',gray2)
#裁剪右下角空白
sp=gray2.shape
rows=sp[0]
cols=sp[1]
row=rows-1
print("gray2crop:",rows,cols,row,col)
whilerow!=0:
isBreak=False
col=cols-1
whilecol!=0:
ifgray2[row,col]==0:
isBreak=True
break
col-=1
ifisBreak==True:
break
row-=1
gray3=gray2[0:row+1,0:col+1]
cv2.imshow('gray3',gray3)
#對裁剪後的gray3求中心點坐標
sp=gray3.shape
rows=sp[0]
cols=sp[1]
x=cols//2
y=rows//2
print("中心點坐標(%d,%d),圖片寬高(w=%d,h=%d)"%(x,y,cols,rows))
#求斜率(y2-y1)/(x2-x1)
#遍歷出第一個白點和最後一個白點,做計算
x1y1=[]
x2y2=[]
#求第一個白點坐標
forrowinrange(rows):
isBreak=False
forcolinrange(cols):
ifgray3[row,col]==255:
x1y1.append(col)
x1y1.append(row)
isBreak=True
break
ifisBreak==True:
break
#求最後一個白點坐標
row=rows-1
whilerow!=0:
isBreak=False
col=cols-1
whilecol!=0:
ifgray2[row,col]==255:
x2y2.append(col)
x2y2.append(row)
isBreak=True
break
col-=1
ifisBreak==True:
break
row-=1
print(x1y1,x2y2)
#計算斜率tan值
k=abs(x2y2[1]-x1y1[1])/abs(x2y2[0]-x1y1[0])
print("斜率k=",k)
cv2.waitKey(0)
cv2.destroyAllWindows()