zoukankan      html  css  js  c++  java
  • Unity之读取本地图片

    1.下载Opencv for unity.

    2.把OpenCVForUnity下的StreamingAssets拖到Assets下。

    3.点击Tools->opencv for unity->set plugin import settings.

    4.

    using UnityEngine;
    using System.Collections;
    using OpenCVForUnity;
    using System;
    using System.Collections.Generic; 

    public class imageTest : MonoBehaviour {
        public Texture2D img; 

        // Use this for initialization
        void Start () {
            
        }
        
        // Update is called once per frame
        void Update () {
            if (Input.GetKeyDown(KeyCode.A)){
                int height = 500;
                string imgpath = "/Users/shiyuhuang/Downloads/ATOCAR/ATOCAR_UNITY_2/ATOCAR_UNITY/13.jpg";
                Mat imgMat2 = Imgcodecs.imread(imgpath);
                Mat imgMat = new Mat ();
                int new_w, new_h=500;
                double scale  = new_h/Convert.ToDouble(imgMat2.rows ());
                new_w = Convert.ToInt32 (imgMat2.cols () * scale);

                Imgproc.resize (imgMat2, imgMat,new Size( Convert.ToDouble(new_w), Convert.ToDouble(new_h)) );
                List<Mat> channels = new List<Mat>();
                OpenCVForUnity.Core.split (imgMat,channels);
                Mat a = new Mat ();
                a = channels [0];
                channels [0] = channels [2];
                channels [2] = a;
                OpenCVForUnity.Core.merge (channels,imgMat);

                Debug.Log("您按下了A键"+imgMat.size());
                SpriteRenderer spr = gameObject.GetComponent<SpriteRenderer>();

                Texture2D texture2d = new Texture2D (new_w, new_h); 
                Utils.matToTexture2D (imgMat, texture2d);
                Sprite sp = Sprite.Create(texture2d,new UnityEngine.Rect(0, 0, new_w, new_h), new Vector2(0.5f,0.5f) );//注意居中显示采用0.5f值  
                spr.sprite = sp;  

    //            gameObject.GetComponent<Renderer> ().material.mainTexture = texture2d;

    //            Mat imgMat2 = new Mat (texture2d.height, texture2d.width, CvType.CV_8UC4);

    //            Utils.texture2DToMat (texture2d, imgMat2);
    //            Imgcodecs.imwrite ("/Users/shiyuhuang/Downloads/ATOCAR/ATOCAR_UNITY_2/ATOCAR_UNITY/test2.jpg",imgMat2);
            }
        }
    }

  • 相关阅读:
    20170803 Airflow自带的API进行GET 和POST动作部分内容
    20170731 培训Bootstrap
    20170728 Celery项目 后台处理SQL SERVER的一个异常
    python 之 递归
    编译型语言和解释型语言的区别
    如何在命令行中让python2和python3同存
    bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级——分层图+dijkstra
    单调栈题目总结
    汕头市队赛SRM15
    codevs 1269 匈牙利游戏——次短路(spfa)
  • 原文地址:https://www.cnblogs.com/huangshiyu13/p/5985248.html
Copyright © 2011-2022 走看看