[Unity]OCR识别--OpenCV篇

    在Unity使用OCR技术大致会写三篇吧,主要是介绍一下OpenCV、例子里的OCRHMMDecoder和Tesseract。心急的同学可以直接跳过前两篇,直接看Tesseract。就当是我水个博客吧。

一、下载并导入OpenCV插件

OpenCV plus Unity | Integration | Unity Asset StoreUse the OpenCV plus Unity from Paper Plane Tools on your next project. Find this integration tool & more on the Unity Asset Store.https://assetstore.unity.com/packages/tools/integration/opencv-plus-unity-85928      这款是完全免费的,直接添加进你的资源就好。

      下载完成后,新建Unity工程(2D或者3D),在Window-->Package Manager里找到,先DownLoad然后再Import。(注意+号旁边的Packages是可以切换的,切换成My Assets) 

    导入后大概率会有报错

    error CS0227: Unsafe code may only appear if compiling with /unsafe

    打开File-->Build Settings-->Project Settings-->Player-->Other Settings,勾选unsafe即可。

 二、namespace和函数说明

         常用的namespce如下,需要注意的是有时候明明已经引入了namespace,但是还是会和Unity的冲突,这时候你就必须带上opencv的namespace。

using OpenCvSharp;
using OpenCvSharp.Util;

       在OpenCV中二转灰度、二值化等与图像处理相关的函数直接封装进了Mat类中,例如

//从纹理转成Mat
Mat Img=OpenCvSharp.Unity.TextureToMat(Texture2D);
//从Mat转成纹理
Texture2D tex=OpenCvSharp.Unity.MatToTexture(Mat);
//灰度化
Mat tempImg = Img.CvtColor(ColorConversionCodes.BGR2GRAY);
//中值滤波
Mat tempImg = Img.MedianBlur(5);
//二值化
Mat tempImg = Img.Threshold(185, 255, ThresholdTypes.BinaryInv);
///如果你要是嫌弃上面写的太麻烦还可以一步到位
 Mat tempImg = Img.CvtColor(ColorConversionCodes.BGR2GRAY).MedianBlur(5).Threshold(185, 255, ThresholdTypes.BinaryInv);
//找轮廓
Point[][] contours;
HierarchyIndex[] hierarchy;
Img.FindContours(out contours, out hierarchy, RetrievalModes.List, ContourApproximationModes.ApproxNone);

      在OpenCV中经常使用的imshow、circle等函数则被封装进了Cv2里,例如

//从纹理转成Mat
Mat Img=OpenCvSharp.Unity.TextureToMat(Texture2D);
//绘制矩形
Cv2.Rectangle(Img, Roi, new Scalar(0, 255, 255));
//画圆
Cv2.Circle(Frame,new Point(0,0),5, new Scalar(0, 255, 255));
//显示(这个一定要慎用)
Cv2.ImShow("Image",Img);

三、从摄像头获取图像显示 

    这部分和Unity一样了,插件也是通过调用WebCamTexture来打开摄像头的。需要一个显示画面的RawImage和WebCamTexture。将WebCamTexture转换成Mat,再将Mat转换成Texture2D。中间可以加上图像处理的一些函数。例如


using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using OpenCvSharp;


public class GameController : MonoBehaviour
{
    public RawImage Capture;
    private WebCamTexture Tex;

    // Start is called before the first frame update
    void Start()
    {
        ///打开相机
        StartCoroutine(OpenCamera());

    }

    // Update is called once per frame
    void Update()
    {
        
        if (Tex != null && Tex.didUpdateThisFrame)
        {
            Mat Frame = OpenCvSharp.Unity.TextureToMat(Tex);
           
            ///
              图像处理.....
            ///
            Destroy(Capture.texture);
            Capture.texture = OpenCvSharp.Unity.MatToTexture(Frame);
        }
     
    }

    void OnApplicationQuit()
    {
        StopCamera();
    }

    IEnumerator OpenCamera()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            
            WebCamDevice[] device = WebCamTexture.devices;
            string deviceName = device[0].name;
            Tex = new WebCamTexture(deviceName, 1920, 1080);
            Tex.Play();
        }
    }

    void StopCamera()
    {
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] device = WebCamTexture.devices;
            string deviceName = device[0].name;
            Tex.Stop();
        }
    }

}

      下面要说的才是重点,有的同学肯定发现了,在替换贴图之前多了“Destroy(Capture.texture);”这是因为在Unity里不断给图像更换贴图会触发一个“内存泄漏”的漏洞。在PC运行还好,但是打包成安卓的话,程序坚持不了多久就会因为内存泄漏而闪退。因此在更新贴图之前要释放原先的贴图。

       至此为止,OpenCV的环境搭建完成!

Works with Unity Cloud Build iOS & Android support Windows10 UWP support WebGL support Win & Mac & Linux Standalone support Preview support in the Editor OpenCV for Unity is an Assets Plugin for using OpenCV 3.4.2 from within Unity. Official Site | ExampleCode | Android Demo WebGL Demo | Tutorial & Demo Video | Forum | API Reference | Support Modules Features: - Since this package is a clone of OpenCV Java, you are able to use the same API as OpenCV Java 3.4.2 (link). - You can image processing in real-time by using the WebCamTexture capabilities of Unity. (real-time face detection works smoothly on iPhone 5) - Provides a method to interconversion of Unity's Texture2D and OpenCV's Mat. - IDisposable is implemented in many classes.You can manage the resources with the “using” statement. - Examples of integration with other publisher assets are available.(e.g. PlayMaker, NatCam, NatCorder) ExampleCode using OpenCV for Unity is available. MarkerBased AR Example MarkerLess AR Example FaceTracker Example FaceSwapper Example FaceMask Example RealTime FaceRecognition Example GoogleVR with OpenCV for Unity Example Kinect with OpenCV for Unity Example AVPro with OpenCV for Unity Example HoloLens with OpenCV for Unity Example PlayMakerActions for OpenCVforUnity NatCam with OpenCVForUnity Example NatCorder with OpenCVForUnity Example OpenCV for Unity uses OpenCV under 3-clause BSD License; see Third-Party Notices.txt file in package for details. System Requirements Build Win Standalone & Preview Editor : Windows 7 or later Build Mac Standalone & Preview Editor : OSX 10.9 or later
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值