0

I have visual studio 2017 and unity 2018. I'm trying to work with a script of a neural network to be attached to an object in the scene that, in order to be trained, makes use of the bitmap class. The problem is that the console of unity gives me the error: 'The type or namespace 'Bitmap' could not be found'. Does anyone know how I can do ? I'm making use of the directives :

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks; 

Here is the script:  

 

     public void Train()
            {
                //Convert to Pixel Array
                Bitmap img = new Bitmap(@"C:\Users\Marc\Desktop\media\resoureces\sites.jpeg");
                double[,,] pixelvalues = new double[3, img.Width, img.Height];
                for (int i = 0; i < img.Width; i++)
                {
                    for (int j = 0; j < img.Height; j++)
                    {
                        Color pixel = img.GetPixel(i, j);
                        pixelvalues[0, i, j] = pixel.R;
                        pixelvalues[1, i, j] = pixel.G;
                        pixelvalues[2, i, j] = pixel.B;
                        //if (pixel == *somecondition *)
                        //{
                        //    **Store pixel here in a array or list or whatever**
                        //}
                    }
                }

derHugo
  • 83,094
  • 9
  • 75
  • 115
MarcDed
  • 15
  • 2
  • AFAIK, you can't use `System.Drawing` from within Unity. Do you actually need to use `System.Drawing.Bitmap` and not a `Byte[]` or `UInt32[]` array? – Dai Jan 18 '21 at 18:49
  • `Bitmap.GetPixel` is very slow, btw - if you use a raw array (or `unsafe` array with `LockBits`) you'll get significantly faster performance, btw. – Dai Jan 18 '21 at 18:51
  • Also, why are you using a 3D array of `double`? RGB arrays have integral values. Also note that multi-dimensional arrays in .NET are also slower than they should be: https://stackoverflow.com/questions/468832/why-are-multi-dimensional-arrays-in-net-slower-than-normal-arrays – Dai Jan 18 '21 at 18:52
  • I need to use a bitmap or something from which I can extract pixel values for the convolutional neural network. Do you know how I can do ? – MarcDed Jan 18 '21 at 18:54
  • Then use an external program to save `sites.jpg` to a raw raster representation on-disk *first* and then read that into a `Byte[]` or `UInt32[]` - you don't need `System.Drawing` in Unity for that. – Dai Jan 18 '21 at 20:14

1 Answers1

0

Find System.Drawing.dll in your Unity Editor Install Folder.

C:\UnityEditors\2018.4.28f1\Editor\Data\MonoBleedingEdge\lib\mono\gac\System.Drawing\4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll

Move this dll into your Assets/Plugins folder:
AssetsPlugins

Make sure that you uncheck Validate References:
UncheckValidateRef

Success:
enter image description here

Selzier
  • 101
  • 1
  • 6
  • 19