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;

        }

 ①效果

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值