Home>

I want to process all images (0.png, 1.png, ...., 9.png) from 0 to 9 in Unity and write them out to an arbitrary folder. I stumbled upon reading the original image. I tried turning i = 0 to 9 in the for statement and loading it with Resources.Load (value of i), but an error occurred in this part.
OpenCV for Unity is used for image processing.

In the case of System.IO.FileInfo (), the method of specifying the image such as "link of the save destination" + i + ".png" worked, but this time it is necessary to put the image in the array and implement. There is?
If i have a website that you think would be helpful, please let me know and I will be grateful.

using UnityEngine;
using System.Collections;
#if UNITY_5_3 || UNITY_5_3_OR_NEWER
using UnityEngine.SceneManagement;
#endif
using OpenCVForUnity;
namespace OpenCVForUnityExample
{
    public class Edgetest: MonoBehaviour
    {
        private IEnumerator Cap () // coroutine declaration
        {
            long filesize;
            float total = 10;// number of generated images
            for (int i = 0;i<total;i ++)
            {
                    Texture2D imgTexture = Resources.Load ("Resource link containing the original image" + i + ".png") as Texture2D;
                    Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC1);
                    Utils.texture2DToMat (imgTexture, imgMat);
                    Debug.Log ("imgMat.ToString ()" + imgMat.ToString ());
                    Imgproc.Canny (imgMat, imgMat, 100, 200);
                    Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, true/* false * /);
                    Utils.matToTexture2D (imgMat, texture);
                    gameObject.GetComponent<Renderer>(). material.mainTexture = texture;
Take // // screenshot
                    ScreenCapture.CaptureScreenshot ("Destination link" + i + ".png");
                    filesize = 0;// reset file size
                while (filesize == 0)
                {
                    yield return null;// interrupt processing
                    // get the size of the file
                    System.IO.FileInfo fi;
                    if (i<= 9)
                    {
                        fi = new System.IO.FileInfo ("Destination link" + i + ".png");
                    }
                    else
                    {
                        fi = new System.IO.FileInfo ("Destination link" + i + ".png");
                    }
                    if (fi! = null)
                    {
                        filesize = fi.Length;
                    }
                }
            }
        }
        void Start ()
        {
            //this.Cube = GameObject.Find ("Cube");
        }
        void Update ()
        {
            if (Input.GetKeyDown ("space")) // start processing when space key is pressed
            {
                StartCoroutine ("Cap");// coroutine call
            }
        }
    }
}
  

Error statement
  NullReferenceException: Object reference not set to an instance of an object
  OpenCVForUnityExample.Edgetest +c__Iterator0.MoveNext () (at Assets/Edgetest.cs: 32)
  UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C: /buildslave/unity/build/Runtime/Export/Coroutines.cs: 17)
  UnityEngine.MonoBehaviour: StartCoroutine (String)
  OpenCVForUnityExample.Edgetest: Update () (at Assets/Edgetest.cs: 114)

  • Answer # 1

    Resources.Load<>() does not require an extension.
    Then I failed to load the image and returned null, but I decided to use it as it is, so it seems that the relevant error was output.