VB.NetOpenCv人脸检测
2021-04-20 22:29阅读:

网上的代码大多还是Python 或 C++ ;早知 Python 那么流行,有点后悔学vb.net 了
代码:
Private Sub Button2_Click(sender As Object, e As EventArgs)
Handles Button2.Click
Dim img As Mat
img =
Cv2.ImRead(Application.StartupPath & '\two.jpeg')
'读取含待检测人脸的图片
Dim gray As New Mat
Cv2.CvtColor(img, gray,
ColorConversionCodes.RGB2GRAY)
'转换灰色
'OpenCV人脸识别分类器
Dim classifier = New
CascadeClassifier(Application.StartupPath &
'\haarcascade_frontalfac
e_default.xml')
Dim mypen As New
Pen(Color.Blue)
Dim facesDetected() As Rect
'检测人脸,并返回人脸位置数据
facesDetected =
classifier.DetectMultiScale(gray, 1.1, 4, 0, New
OpenCvSharp.Size(32, 32)) '第3个参数 4
即检测次数4次符合条件才算人脸
If (facesDetected.Length > 0)
Then
For Each facerect
In facesDetected
Dim
a1 As New Drawing.Point(1, 1)
Dim
b1 As New Drawing.Point(5, 5)
'框出的嘴巴眼睛 针对头摆正的还可以,头歪着的就不怎么样了
'框出人脸
'Cv2.Rectangle(img, a1, b1, (0, 255, 0), 2)
Cv2.Rectangle(img, New OpenCvSharp.Point(facerect.X, facerect.Y),
New OpenCvSharp.Point(facerect.X + facerect.Width, facerect.Y +
facerect.Height), Scalar.FromRgb(232, 247, 3), 1, LineTypes.Link8,
0)
'框出左眼 Python cv2.circle(img, (x + w // 4, y + h // 4 + 30),
min(w // 8, h // 8), color)
Cv2.Circle(img, New OpenCvSharp.Point(facerect.X + facerect.Width \
4, facerect.Y + facerect.Height \ 4 + 30), facerect.Height \ 8,
Scalar.FromRgb(232, 247, 3))
'框出右眼 Python cv2.circle(img, (x + 3 * w // 4, y + h // 4 +
30), min(w // 8, h // 8), color)
Cv2.Circle(img, New OpenCvSharp.Point(facerect.X + 3 *
facerect.Width \ 4, facerect.Y + facerect.Height \ 4 + 30),
facerect.Height \ 8, Scalar.FromRgb(232, 247, 3))
'框出嘴巴 Python cv2.rectangle(img, (x + 3 * w // 8, y +
3 * h // 4),(x + 5 * w // 8, y + 7 * h // 8), color)
Cv2.Rectangle(img, New OpenCvSharp.Point(facerect.X + 3 *
facerect.Width \ 8, facerect.Y + 3 * facerect.Height \ 4), New
OpenCvSharp.Point(facerect.X + 5 * facerect.Width \ 8, facerect.Y +
7 * facerect.Height \ 8), Scalar.FromRgb(232, 247, 3), 1,
LineTypes.Link8, 0)
Next
Cv2.ImShow('检测到人脸数量:' & facesDetected.Length, img)
Cv2.WaitKey(10)
Cv2.WaitKey(0)
Cv2.DestroyAllWindows()
'释放所有的窗体资源
End If
End Sub