Vb.net Emgu图像识别处理常用函数

Sub MatView(ByRef M As Mat, Pic As PictureBox, Optional Full As Boolean = False)
    If M.Height <= Pic.Height AndAlso M.Width <= Pic.Width Then Pic.Image = M.ToBitmap() : Return
    If Full Then Pic.Image = M.ToBitmap() : Pic.Width = M.Width : Pic.Height = M.Height : Return

    Dim R As New Mat, scale As Double = M.Height / M.Width
    CvInvoke.Resize(M, R, New Size(Pic.Height / scale, Pic.Height)) '缩放
    Pic.Width = R.Width
    Pic.Height = R.Height

    Pic.Image = R.ToBitmap()
  End Sub
  Sub SaveMat(ByRef M As Mat, Optional FileName As String = "", Optional FileDir As String = "")
    Dim Path As String = AppPath & "\Mat\" & Now.ToString("yyyy-MM-dd") & "\"
    If FileDir <> "" Then Path = AppPath & "\Pick\" & FileDir & "\"
    If IO.Directory.Exists(Path) = False Then IO.Directory.CreateDirectory(Path) '文件夹是否存在,不存在而创建
    M.Save(Path & IIf(FileName <> "", FileName, Now.ToString("hh-mm-ss-ffff")) & ".jpg")
  End Sub
  Function OpenMat(FileName) As Mat
    If FileName = "" Then
      Dim f As New OpenFileDialog
      f.Filter = "(*.jpg)|*.jpg|(*.txt)|*.txt"
      If f.ShowDialog() <> DialogResult.OK Then Return New Mat
      FileName = f.FileName
    End If
    Return New Mat(FileName, ImreadModes.Grayscale) '灰度8位
  End Function
  Function MatThres(ByRef M As Mat, threshold As Double) As Mat '图像阀值
    Dim dst As New Mat, R As New Mat
    ' CvInvoke.EqualizeHist(M, dst) '提高亮度,灰度图像均衡
    CvInvoke.Threshold(M, R, threshold, &HFF, ThresholdType.Binary) '阀值

    'CvInvoke.AdaptiveThreshold(M, R, 255, AdaptiveThresholdType.GaussianC, ThresholdType.Binary, 31, 5)

    Return R
  End Function
  Function MatCompare(ByRef M1 As Mat, ByRef M2 As Mat) As Mat '两图比较
    Dim R As New Mat
    'CvInvoke.AbsDiff(M1, M2, R)
    CvInvoke.Compare(M1, M2, R, CmpType.NotEqual)
    Return R
  End Function
  Function MatCls(ByRef M As Mat, Optional size As Integer = 7) As Mat '去除噪点
    Dim R As New Mat
    CvInvoke.MedianBlur(M, R, size)
    Return R
  End Function

  Function MatBlur(ByRef M As Mat, Optional size As Integer = 7) As Mat '糊模
    Dim R As New Mat
    CvInvoke.Blur(M, R, New Size(size, size), New Point(-1, -1))
    Return R
  End Function

  Function MatMedBlur(ByRef M As Mat, Optional size As Integer = 7) As Mat '中值滤波
    Dim R As New Mat
    CvInvoke.MedianBlur(M, R, size)
    Return R
  End Function

  Function MatGaussBlur(ByRef M As Mat, Optional size As Integer = 9) As Mat '高斯糊模
    Dim R As New Mat
    CvInvoke.GaussianBlur(M, R, New Size(size, size), 0)
    Return R
  End Function
  Function MatInverted(ByRef M As Mat) As Mat '颜色返相
    Dim b() As Byte = M.GetRawData()
    For i As Integer = 0 To b.Length - 1
      b(i) = &HFF - b(i)
    Next
    M.SetTo(b)
    Return M
  End Function
  Function MatCanny(ByRef M As Mat) As Mat '轮廓加强
    Dim R As New Mat
    CvInvoke.Canny(M, R, 150, 160)
    Return R
  End Function
  Function MatArr(ByRef M As Mat) As Array  'Mat转为数组
    Return M.GetData()
  End Function

  Function ArrMat(arr As Array, ByRef T As Mat) '数组转Mat
    Return New Mat(T.Height, T.Width, T.Depth, T.NumberOfChannels, Marshal.UnsafeAddrOfPinnedArrayElement(arr, 0), T.Width * T.NumberOfChannels)
  End Function
  Function MatRgb(ByRef M As Mat) As Mat '转换成为rgb空间格式
    If M.NumberOfChannels = 3 Then Return M
    Dim R As New Mat
    CvInvoke.CvtColor(M, R, ColorConversion.Gray2Bgr)
    Return R
  End Function
  Function MatGray(ByRef M As Mat) As Mat '转换成为单通灰色
    If M.NumberOfChannels = 1 Then Return M
    Dim R As New Mat
    CvInvoke.Merge(M, R)
    Return R
  End Function

  Function MatSelect(ByRef M As Mat) As Rectangle '选择区域
    Dim ScreenArea As Rectangle = Screen.GetWorkingArea(New Point(-1, -1))
    If M.Width <= ScreenArea.Width Then Return CvInvoke.SelectROI("Selct Area1", M, False, True)
    Dim R As New Mat, scale As Double = (ScreenArea.Width / M.Width)
    CvInvoke.Resize(M, R, New Size(M.Width * scale, M.Height * scale)) '缩放
    Dim Rect As Rectangle = CvInvoke.SelectROI("Selct Area2", R, False, True)
    If Rect = Nothing Then Return Rect
    Return New Rectangle(Rect.X / scale, Rect.Y / scale, Rect.Width / scale, Rect.Height / scale)
  End Function
  Function MatCrop(ByRef M As Mat, Rect As Rectangle) As Mat '裁剪
    Dim R As New Mat
    CvInvoke.GetRectSubPix(M, Rect.Size, New PointF(Rect.X + Rect.Width / 2, Rect.Y + Rect.Height / 2), R, DepthType.Default)
    Return R
  End Function
  Sub MatText(ByRef M As Mat, str As String) '画字

    CvInvoke.PutText(M, str, New Point(100, 100), FontFace.HersheySimplex, 3, New MCvScalar(255, 255, 255, 255), 2, LineType.Filled, True)
  End Sub
  Sub MatRect(ByRef M As Mat, Rect As Rectangle) '画框
    CvInvoke.Rectangle(M, Rect, New MCvScalar(0, 0, &HFF), 3, LineType.Filled, 0)
  End Sub

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值