OpencvForUnity学习:五:OpenCVforUnity中识别图片中的基础图形

目录

 

一、目的:

1、发现很多不熟悉,HandPoseEstimationExample案例手势识别地方很多代码看不懂,所以找点文章来练手,增强记忆

一、参考:

1、OpenCVforUnity中识别图片中的基础图形

①总结:good:能实现大部分;but:最后图片对比自己琢磨不出来;下面是我的步骤

一、自己操作

1、读取图片,进行高斯滤波、灰色图处理:

①我是将HandPoseEstimationExample案例的代码复制的,将Update里面内容清空了

 ①效果:

1、发现轮廓

 ①效果


一、目的:

1、发现很多不熟悉,HandPoseEstimationExample案例手势识别地方很多代码看不懂,所以找点文章来练手,增强记忆

一、参考:

1、OpenCVforUnity中识别图片中的基础图形

https://blog.csdn.net/u013293580/article/details/84710933

①总结:good:能实现大部分;but:最后图片对比自己琢磨不出来;下面是我的步骤

一、自己操作

1、读取图片,进行高斯滤波、灰色图处理:

①我是将HandPoseEstimationExample案例的代码复制的,将Update里面内容清空了

 

 

        /// <summary>
        /// 成功:打开图片进行高斯滤波处理+二值化处理
        /// 参考:OpenCVforUnity中识别图片中的基础图形 https://blog.csdn.net/u013293580/article/details/84710933
        /// </summary>
        private void Test_recognitionShap()
        {
            //读取图片
            scrMat = Imgcodecs.imread(Application.dataPath + "/Resources/test10/2.jpg",1);

            //定义Texture2D设置其宽高随scrMat材质颜色模式为RGBA32
            Texture2D texture = new Texture2D(scrMat.cols(), scrMat.rows(), TextureFormat.RGBA32, false);

            //图片颜色模式转换
            Imgproc.cvtColor(scrMat, scrMat, Imgproc.COLOR_BGR2GRAY);

            //图片高斯模糊处理
            Imgproc.GaussianBlur(scrMat, scrMat, new Size(5, 5), 0);

            //图片二值化处理
            Imgproc.threshold(scrMat, scrMat, 128, 255, Imgproc.THRESH_BINARY);

            //把Mat格式转换成texture格式
            Utils.matToTexture2D(scrMat, texture);

            //测试:摄像头画面在RawImage显示
            obj_showWebCam.GetComponent<RawImage>().texture = texture;
        }

 ①效果:

1、发现轮廓

      /// <summary>
        /// 测试:寻找轮廓
        ///  参考:OpenCVforUnity中识别图片中的基础图形 https://blog.csdn.net/u013293580/article/details/84710933
        /// </summary>
        private void Test_findContour()
        {
            //读取图片
            scrMat = Imgcodecs.imread(Application.dataPath + "/Resources/test10/1.jpg", 1);

            //必须让其变灰色,否则会报错
            Imgproc.cvtColor(scrMat, scrMat, Imgproc.COLOR_BGR2GRAY);

            //图片高斯模糊处理:需要对原图进行处理,否则出现的图片太接近原图了,不好让计算机计算
            Imgproc.GaussianBlur(scrMat, scrMat, new Size(5, 5), 0);

            //图片二值化处理:需要对原图进行处理,否则出现的图片太接近原图了,不好让计算机计算
            Imgproc.threshold(scrMat, scrMat, 128, 255, Imgproc.THRESH_BINARY);

            List<MatOfPoint> srcContours = new List<MatOfPoint>();
            Mat srcHierarchy = new Mat();

            //寻找轮廓
            Imgproc.findContours(scrMat, srcContours, srcHierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_NONE);

            for (int i = 0; i < srcContours.Count; i++)
            {
                //轮廓描边
                Imgproc.drawContours(scrMat, srcContours, i, new Scalar(255, 255, 255), 2, 8, srcHierarchy, 0, new Point());
                Point point = new Point();
                float[] radius = new float[1];
                //获取点集最小外接圆点
                Imgproc.minEnclosingCircle(new MatOfPoint2f(srcContours[i].toArray()), point, radius);
                //在圆点位置绘制圆形
                Imgproc.circle(scrMat, point, 7, new Scalar(0, 0, 255), -1);

            }


            //定义Texture2D设置其宽高随scrMat材质颜色模式为RGBA32
            Texture2D text2d_texture = new Texture2D(scrMat.cols(), scrMat.rows(), TextureFormat.RGBA32, false);

            //把Mat格式转换成texture格式
            Utils.matToTexture2D(scrMat, text2d_texture);


            //摄像头画面在RawImage显示
            obj_showWebCam.GetComponent<RawImage>().texture = text2d_texture;

        }

 ①效果

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、付费专栏及课程。

余额充值