|
发表于 2014-3-15 13:39:55
|
显示全部楼层
写了个简单的算法,可以输出旋转的角度,我看楼上的不是用GDI做旋转,旋转的就不改了。
简单做了下优化,时间在1.5秒左右;不算太好。:
得分情况:scoreX:308,scoreY:1115,scoreU:400,scoreD:1023
旋转角度:-90
计算用时:1.34375
-------------
得分情况:scoreX:33964,scoreY:36196,scoreU:1084,scoreD:69076
旋转角度:-180
计算用时:1.59375
-------------
回头再发整个项目程序;- Option Explicit
- 'Add by xmxoxo 用于分析图像
- Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long
- '2014-3-15 add by xmxoxo
- '判断图像需要旋转的方向;思路:二值化后保留头发,判断头发所在的方位
- Public Function GetDirection(objPic As PictureBox, sngBright As Single) As Integer
- Dim x As Long, y As Long
- Dim scoreX As Long, scoreY As Long
- Dim scoreU As Long, scoreD As Long
- Dim lngColor As Long
- Dim intBright As Integer
-
- '寻找黑色的头发所在的地方
- For x = 1 To objPic.width
- For y = 1 To objPic.Height
- lngColor = GetPixel(objPic.hDC, x, y)
- If lngColor < 8323072 Then
- intBright = Bright(lngColor)
-
- If intBright < sngBright Then
- If x <= objPic.width / 2 Then
- scoreX = scoreX + 1
- Else
- scoreY = scoreY + 1
- End If
- If y <= objPic.Height / 2 Then
- scoreU = scoreU + 1
- Else
- scoreD = scoreD + 1
- End If
-
- End If
- End If
- Next
- Next
-
- Debug.Print "得分情况:scoreX:" & scoreX & ",scoreY:" & scoreY & ",scoreU:" & scoreU & ",scoreD:" & scoreD
- If Abs(scoreX - scoreY) / (scoreX + scoreY) < 0.5 Then
- '左右基本对称,判断上下
- If Abs(scoreU - scoreD) / (scoreU + scoreD) < 0.5 Then
- GetDirection = 0 '上下基本对称,图像可能不是一个头像
- Else
- If scoreU > scoreD Then
- GetDirection = 0 '头在上,正确
- Else
- GetDirection = -180 '头在下,翻转180
- End If
- End If
-
- Else
- If scoreX > scoreY Then
- GetDirection = 90
- Else
- GetDirection = -90
- End If
- End If
- End Function
- '计算像素点的亮度
- Private Function Bright(value As Long) As Integer
- On Error Resume Next
- Dim sHex As String
- Dim lngT As Long
- Dim r As Byte, g As Byte, b As Byte
-
-
- If Color < 0 Then
- Bright = 0
- Exit Function
- End If
-
- r = value Mod 256
- lngT = value / 256
-
- g = lngT Mod 256
- lngT = lngT / 256
-
- b = lngT Mod 256
-
- If r > g Then
- If r > b Then
- Bright = r * 100 / 255
- Else
- Bright = b * 100 / 255
- End If
- Else
- If g > b Then
- Bright = g * 100 / 255
- Else
- Bright = b * 100 / 255
- End If
- End If
-
- If Err Then
- Err.Clear
- End If
- End Function
复制代码 |
|