因工作上常常遇到影像座標系的計算與轉換,因此整理了一下目前的計算方式與程式的介紹,基本上都是用很簡單的公式去實現,在此只是把公式轉換成程式可以運算的方法:
影像座標示意圖
座標轉換示意圖
程式介面圖
初始設定:
'主要是先設定picture的象限改成標準象限
'設定Form大小
Form1.Top = 0
Form1.Left = 0
Form1.Width = 10 * Screen.Width / Screen.TwipsPerPixelX
Form1.Height = 10 * Screen.Height / Screen.TwipsPerPixelY
'畫小圖設定
Picture1.ScaleHeight = -Picture1.ScaleHeight
Picture1.ScaleTop = -Picture1.ScaleHeight * 1 / 2
Picture1.ScaleLeft = -Picture1.ScaleWidth * 1 / 2
呼叫端:
Dim ImgAxisStartX As Double
Dim ImgAxisStartY As Double
Dim ImgAxisEndX As Double
Dim ImgAxisEndY As Double
Dim GetValue() As String
Dim M1 As Double
Dim Degree1 As Double
Dim Quadrant1 As String
'檢查 起點影像座標X 與 起點影像座標Y 的值 是否為整數
'使用的是CheckTextIsNumEricAndInteger Function來判斷
If CheckTextIsNumEricAndInteger(Text_StartX, Text_StartX.Text, "起點影像座標X") = True Then
ImgAxisStartX = Format(Text_StartX.Text)
Else
Exit Sub
End If
If CheckTextIsNumEricAndInteger(Text_StartY, Text_StartY.Text, "起點影像座標Y") = True Then
ImgAxisStartY = Format(Text_StartY.Text)
Else
Exit Sub
End If
'檢查 終點影像座標X 與 終點影像座標Y 的值 是否為整數
'使用的是CheckTextIsNumEricAndInteger Function來判斷
If CheckTextIsNumEricAndInteger(Text_EndX, Text_EndX.Text, "終點影像座標X") = True Then
ImgAxisEndX = Format(Text_EndX.Text)
Else
Exit Sub
End If
If CheckTextIsNumEricAndInteger(Text_EndY, Text_EndY.Text, "終點影像座標Y") = True Then
ImgAxisEndY = Format(Text_EndY.Text)
Else
Exit Sub
End If
'轉換影像座標系至標準座標系的點位轉換
'此時 Label_StartX.Caption...等,就是轉換後的座標
GetValue = ImgAxisTransformStandardAxis(Picture1, ImgAxisStartX, ImgAxisStartY)
Label_StartX.Caption = GetValue(0)
Label_StartY.Caption = GetValue(1)
GetValue = ImgAxisTransformStandardAxis(Picture1, ImgAxisEndX, ImgAxisEndY)
Label_EndX.Caption = GetValue(0)
Label_EndY.Caption = GetValue(1)
'計算斜率
GetValue = MountainAndAbsoluteDegreeCaculate(Val(Label_StartX.Caption), Val(Label_StartY.Caption), Val(Label_EndX.Caption), Val(Label_EndY.Caption))
M1 = Val(GetValue(0))
Degree1 = Val(GetValue(1))
Quadrant1 = GetValue(2)
Label_M1.Caption = Format(M1, "0.0000")
Label_D1.Caption = Format(Degree1, "0.0000")
Label_Q1.Caption = Format(Quadrant1, "0.0000")
'清空picture的畫圖
Picture1.Picture = Nothing
'畫picture的X,Y基準線
Picture1.DrawWidth = 1
Picture1.Line (-Picture1.Width, 0)-(Picture1.Width, 0), vbGreen
Picture1.Line (0, -Picture1.Height)-(0, Picture1.Height), vbGreen
'畫線與點位
Picture1.DrawWidth = 1
Picture1.Circle (Val(Label_StartX.Caption), Val(Label_StartY.Caption)), ((ImgAxisStartX - ImgAxisEndX) ^ 2 + (ImgAxisStartY - ImgAxisEndY) ^ 2) ^ 0.5, vbRed
Picture1.Line (Val(Label_StartX.Caption), Val(Label_StartY.Caption))-(Val(Label_EndX.Caption), Val(Label_EndY.Caption)), vbBlue
Picture1.DrawWidth = 3
Picture1.PSet (Val(Label_StartX.Caption), Val(Label_StartY.Caption)), vbRed
Picture1.PSet (Val(Label_EndX.Caption), Val(Label_EndY.Caption)), vbBlue
使用Function:
'將影像或圖片座標轉換為正規座標系(第四象限)
'用法範例:(PictureBox物件名稱,影像或物件X座標,影像或物件Y座標)
Function CheckTextIsNumEricAndInteger(TString As TextBox, WordString As String, DataTitle As String) As Boolean
'檢查是否為整數
'用法範例: (TextBox物件名稱,要檢查的字串,字串的Title)--->(Text1,Text1.Text,"影像座標X")
If TString.Text = "" Then
MsgBox (DataTitle + ",沒有數值")
Else
If IsNumeric(TString.Text) = True Then
If Int(WordString) = Format(WordString, "0.0000") Then
CheckTextIsNumEricAndInteger = True
Else
MsgBox (DataTitle + ",非整數")
End If
Else
MsgBox (DataTitle + ",非數字")
End If
End If
End Function
'檢查是否為整數
'用法範例: (TextBox物件名稱,要檢查的字串,字串的Title)--->(Text1,Text1.Text,"影像座標X")
Function MountainAndAbsoluteDegreeCaculate(StartX As Double, StartY As Double, EndX As Double, EndY As Double) As String()
'計算兩點成一線的斜率
'利用斜率與XY的關係式求絕對角度
'用法範例: (起點X座標,起點Y座標,終點X座標,終點Y座標)
Dim SendValue(2) As String
Dim SX As Double
Dim SY As Double
Dim EX As Double
Dim EY As Double
Dim M_PI As Double
M_PI = Atn(1) * 4
SX = StartX
SY = StartY
EX = EndX
EY = EndY
'計算斜率-----------------------------------------------------------
'非水平與垂直
If (SX <> EX) And SY <> EY Then
SendValue(0) = Format((SY - EY) / (SX - EX), "0.0000")
End If
'起點與終點同一點
If (SX = EX) And (SY = EY) Then
SendValue(0) = O
End If
'起點與終點之X值相同 Y值不同
If (SX = EX) Then
If (SY > EY) Then
SendValue(0) = -999999
Else
SendValue(0) = 999999
End If
End If
'起點與終點之Y值相同 X值不同
If (SY = EY) Then
SendValue(0) = 0
End If
'--------------------------------------------------------------------
'計算角度-----------------------------------------------------------
'起點與終點一樣
If EX = SX And EY = SY Then
SendValue(1) = 0
SendValue(2) = "起點終點同一點"
MountainAndAbsoluteDegreeCaculate = SendValue
Exit Function
End If
'第一象限
If EX > SX And EY > SY Then
SendValue(1) = Atn(Val(SendValue(0))) * 180 / M_PI
SendValue(2) = "第ㄧ象限"
MountainAndAbsoluteDegreeCaculate = SendValue
Exit Function
End If
'第二象限
If EX < SX And EY > SY Then
SendValue(1) = 180 + Atn(Val(SendValue(0))) * 180 / M_PI
SendValue(2) = "第二象限"
MountainAndAbsoluteDegreeCaculate = SendValue
Exit Function
End If
'第三象限
If EX < SX And EY < SY Then
SendValue(1) = 180 + Atn(Val(SendValue(0))) * 180 / M_PI
SendValue(2) = "第三象限"
MountainAndAbsoluteDegreeCaculate = SendValue
Exit Function
End If
'第四象限
If EX > SX And EY < SY Then
SendValue(1) = 360 + Atn(Val(SendValue(0))) * 180 / M_PI
SendValue(2) = "第四象限"
MountainAndAbsoluteDegreeCaculate = SendValue
Exit Function
End If
'90度
If EX = SX And EY > SY Then
SendValue(1) = 90
SendValue(2) = "垂直"
MountainAndAbsoluteDegreeCaculate = SendValue
Exit Function
End If
'270度
If EX = SX And EY < SY Then
SendValue(1) = 270
SendValue(2) = "垂直"
MountainAndAbsoluteDegreeCaculate = SendValue
Exit Function
End If
'0度
If EX > SX And EY = SY Then
SendValue(1) = 0
SendValue(2) = "垂直"
MountainAndAbsoluteDegreeCaculate = SendValue
Exit Function
End If
'0度
If EX < SX And EY = SY Then
SendValue(1) = 180
SendValue(2) = "垂直"
MountainAndAbsoluteDegreeCaculate = SendValue
Exit Function
End If
'--------------------------------------------------------------------
End Function
說明:
沒有留言:
張貼留言