示例#1
0
        public static ImageRgb toImgRgb(this Bitmap bmp)
        {
            ImageRgb img = new ImageRgb();

            img.resize(bmp.Width, bmp.Height);
            try
            {
                unsafe
                {
                    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, img.width(), img.height()),
                                                      System.Drawing.Imaging.ImageLockMode.ReadOnly,
                                                      System.Drawing.Imaging.PixelFormat.Format24bppRgb);

                    byte[] rawData = new byte[bmpData.Stride * bmpData.Height];
                    Marshal.Copy(bmpData.Scan0, rawData, 0, rawData.Length);

                    Marshal.Copy(rawData, 0, img.getRawImage(), rawData.Length);


                    bmp.UnlockBits(bmpData);
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Exception while accessing image: " + e);
            }

            return(img);
        }
        /// <summary>
        /// Self explicit
        /// </summary>
        /// <param name="v"></param>
        /// <param name="img"></param>
        protected void Vector2Image(float[] v, ref ImageRgb img)
        {
            double t0 = Time.now();

            img.resize(w, h);
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    img.pixel(i, j).r = (byte)(v[j * w + i] * 255);
                    img.pixel(i, j).g = (byte)(v[j * w + i + 1] * 255);
                    img.pixel(i, j).b = (byte)(v[j * w + i + 2] * 255);
                }
            }
            double t1 = Time.now();

            //Console.WriteLine("Vector2img : " + (t1 - t0).ToString());
        }
    // Use this for initialization
    void Start()
    {
        Debug.Log ("Initialising Network ... ");
        Network.init();
        Debug.Log ("Initialising Port");
        imagePort = new BufferedPortImageRgb();
        Debug.Log ("Open Port");
        imagePort.open (sourcePortName);

        Debug.Log ("Connect Port");
        Network.connect(sourcePortName, destPortName);

        //RenderTexture.active = view;
        resWidth = view.width;
        resHeight = view.height;
        temp2D = new Texture2D(resWidth,resHeight, TextureFormat.ARGB32, false);

        texImage = imagePort.prepare();

        //set resolution and parameters of image
        texImage.resize(resWidth, resHeight);
        texImage.setTopIsLowIndex(false);
        texImage.setQuantum(1);
        texImage.zero();

        System.IntPtr imagePtr = texImage.getRawImage();
        int imageSize = texImage.getRawImageSize();
        byteArray = new byte[imageSize];

        newPtr = Marshal.AllocHGlobal(byteArray.Length);
    }