• [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。

    1. using OpenCvSharp;
    2. using OpenCvSharp.Util;

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

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

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

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

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

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

    1. using System;
    2. using System.IO;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7. using OpenCvSharp;
    8. public class GameController : MonoBehaviour
    9. {
    10. public RawImage Capture;
    11. private WebCamTexture Tex;
    12. // Start is called before the first frame update
    13. void Start()
    14. {
    15. ///打开相机
    16. StartCoroutine(OpenCamera());
    17. }
    18. // Update is called once per frame
    19. void Update()
    20. {
    21. if (Tex != null && Tex.didUpdateThisFrame)
    22. {
    23. Mat Frame = OpenCvSharp.Unity.TextureToMat(Tex);
    24. ///
    25. 图像处理.....
    26. ///
    27. Destroy(Capture.texture);
    28. Capture.texture = OpenCvSharp.Unity.MatToTexture(Frame);
    29. }
    30. }
    31. void OnApplicationQuit()
    32. {
    33. StopCamera();
    34. }
    35. IEnumerator OpenCamera()
    36. {
    37. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
    38. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
    39. {
    40. WebCamDevice[] device = WebCamTexture.devices;
    41. string deviceName = device[0].name;
    42. Tex = new WebCamTexture(deviceName, 1920, 1080);
    43. Tex.Play();
    44. }
    45. }
    46. void StopCamera()
    47. {
    48. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
    49. {
    50. WebCamDevice[] device = WebCamTexture.devices;
    51. string deviceName = device[0].name;
    52. Tex.Stop();
    53. }
    54. }
    55. }

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

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

  • 相关阅读:
    Hadoop大数据技术 伪分布式集群搭建快速入门教程Centos7
    Activity的启动模式
    java计算机毕业设计高校实习管理平台系统MyBatis+系统+LW文档+源码+调试部署
    华为HCIE云计算之FA云桌面发放(Microsoft AD方式)
    您与1秒钟测量两千个尺寸之间仅差一台智能测径仪!
    【Linux】常用基本指令汇总
    cocos create csv 配置文件
    Vite + React + Ant Design构建项目
    大数据热点城市波动图案例【CSS3实现 + 原理分析 + 源码获取】
    JAVA集合
  • 原文地址:https://blog.csdn.net/qq_36251561/article/details/127807205