webCamTexture转换为Mat后让RawImage显示

目录

 

一:目的:

1、因为需要找到手指的最高点,最终决定使用OpencvForUnity,所以想找到将摄像头画面转换为Mat,再将mat显示在Unity中。这样我就可以对Mat进行类似Opencv一样的操作

一、参考:

1、Unity如何将摄像头画面显示在cube或者plane上面

①总结:good:知道打开相机

2、OpenCVForUnity插件 配置基础开发环境

①总结:good;知道OpencvForUnity下载、使用

一、自己做:

1、注意:分辨率一定要注意,我在这里没有仔细写,摄像头的分辨率、RawImage的分辨率都是要注意大小


一:目的:

1、因为需要找到手指的最高点,最终决定使用OpencvForUnity,所以想找到将摄像头画面转换为Mat,再将mat显示在Unity中。这样我就可以对Mat进行类似Opencv一样的操作

一、参考:

1、Unity如何将摄像头画面显示在cube或者plane上面

https://blog.csdn.net/qq_40544338/article/details/102582442

①总结:good:知道打开相机

2、OpenCVForUnity插件 配置基础开发环境

https://blog.csdn.net/tpz789/article/details/91171127

①总结:good;知道OpencvForUnity下载、使用

一、自己做:

1、注意:分辨率一定要注意,我在这里没有仔细写,摄像头的分辨率、RawImage的分辨率都是要注意大小

/// <summary>
/// 功能:打开相机 ,保存图片失败,后序有时间再完善
/// 版本 : 
/// </summary>
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

using OpenCVForUnity;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.UnityUtils;

public class My_test03_openCamera : MonoBehaviour
{
    // UI 相关参数
    public RawImage rawImage;
    public RawImage rawImage_copy_camera;

    // 摄像机图片参数
    private WebCamTexture webCamTexture;
    
    void Start()
    {
        // 打开相机
        StartCoroutine("OpenCamera");
    }

    void Update()
    {
        if (rawImage.texture != null)
        {
            //测试3:webCamTextureToMat:没有报错
            Mat frameMat = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC4);
            Color32[] colors = new Color32[webCamTexture.width * webCamTexture.height];
            Utils.webCamTextureToMat(webCamTexture, frameMat, colors, false);

            //int i_width = (int)rawImage.GetComponentInParent<RectTransform>().rect.width;
            //int i_heigth = (int)rawImage.GetComponentInParent<RectTransform>().rect.height;
            int i_width = webCamTexture.width;
            int i_heigth = webCamTexture.height;

            Texture2D texture = new Texture2D(i_width, i_heigth, TextureFormat.RGBA32, false);
            Utils.matToTexture2D(frameMat, texture);
            rawImage_copy_camera.texture = texture;
        }
    }

    /// <summary>
    /// 使用协程打开相机函数
    /// </summary>
    /// <returns></returns>
    IEnumerator OpenCamera()
    {
        // 申请相机权限
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        // 判断是否有相机权限
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            // 获取相机设备
            WebCamDevice[] webCamDevices = WebCamTexture.devices;
            // 判断是否有相机设别
            if (webCamDevices != null && webCamDevices.Length > 0)
            {
                // 把 0 号设备(移动端后置摄像头)名称赋值
                string webCamName = webCamDevices[0].name;
                // 设置相机渲染宽高,并运行相机
                webCamTexture = new WebCamTexture(webCamName, Screen.width, Screen.height);
                webCamTexture.Play();
                // 把获取的图像渲染到画布上
                rawImage.texture = webCamTexture;
            }
        }
    }

}



 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值