Пример #1
0
    private void renderImage(int j)
    {
        byte[] colorChangedPic = null;
        if (resultMan.getSelectedPalette() == 0)
        {
            if (pre1886ColorChangedTexture == null)
            {
                pre1886ColorChangedTexture = changeColor(unalteredScene, centers);
            }
            colorChangedPic = pre1886ColorChangedTexture.EncodeToJPG();
        }
        else if (resultMan.getSelectedPalette() == 1)
        {
            if (post1886ColorChangedTexture == null)
            {
                post1886ColorChangedTexture = changeColor(unalteredScene, centers);
            }
            colorChangedPic = post1886ColorChangedTexture.EncodeToJPG();
        }
        else if (resultMan.getSelectedPalette() == 2)
        {
            if (finalYearsColorChangedTexture == null)
            {
                finalYearsColorChangedTexture = changeColor(unalteredScene, centers);
            }
            colorChangedPic = finalYearsColorChangedTexture.EncodeToJPG();
        }


        string image64 = Convert.ToBase64String(colorChangedPic);

        // Copy this structure to add in change color
        clientSocket = new ClientSocketScript(image64, "GetBrushStrokes", resultMan.getBrushStrokeIndex(), resultMan.getBrushValues());
        clientSocket.Start();
        while (clientSocket.getResponse() == null)
        {
            clientSocket.Update();
        }

        byte[] bytes = Convert.FromBase64String(clientSocket.getResponse());
        clientSocket.Stop();
        Texture2D texture = new Texture2D(1, 1);

        texture.LoadImage(bytes);
        texture.Apply();
        Sprite sprite = Sprite.Create(texture, new UnityEngine.Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100);

        images[j].sprite = sprite;
        unDisplayImages();
        images[j].enabled = true;
        resultMan.setCurrentDisplayedImageName(images[j].name);
        this.loadingScreen.SetActive(false);
    }
Пример #2
0
    private void screenShot()
    {
        Debug.Log(Application.persistentDataPath);
        path = Application.persistentDataPath + "/UnalteredScene.jpg";
        //ScreenCapture.CaptureScreenshot("UnalteredScene.png");
        //ScreenCapture.CaptureScreenshot(path);
        RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 24);

        camera.targetTexture = rt;
        Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);

        camera.Render();
        RenderTexture.active = rt;
        texture.ReadPixels(new UnityEngine.Rect(0, 0, Screen.width, Screen.height), 0, 0);
        texture.Apply();
        byte[] shot = texture.EncodeToJPG();

        camera.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);

        File.WriteAllBytes(path, shot);
        Debug.Log(path);

        string base64Image = Convert.ToBase64String(shot);

        clientSocket = new ClientSocketScript(base64Image, "GetColorPalette", resultMan.getBrushStrokeIndex(), resultMan.getBrushValues());
        clientSocket.Start();
        while (clientSocket.getResponse() == null)
        {
            clientSocket.Update();
        }
        int paletteIndex = Int32.Parse(clientSocket.getResponse());

        if (resultMan.getSelectedPalette() == -1)
        {
            resultMan.setSelectedPalette(paletteIndex);
        }
        resultMan.setChosenDynamicPalette(paletteIndex);
        clientSocket.Stop();
    }
Пример #3
0
    private void DisplayImage(string path)
    {
        if (System.IO.File.Exists(path))
        {
            path = Application.persistentDataPath + "/" + imageName + ".jpg";
            byte[]    bytes   = System.IO.File.ReadAllBytes(path);
            Texture2D texture = new Texture2D(1, 1);
            texture.LoadImage(bytes);
            unalteredScene = new Texture2D(1, 1);
            unalteredScene.LoadImage(bytes);
            Texture2D orgTexture = new Texture2D(1, 1);
            orgTexture.LoadImage(bytes);

            float aspectRatio = texture.width / texture.height;


            if (Screen.orientation == ScreenOrientation.Portrait)
            {
                int width = (int)Math.Round(480 * aspectRatio);
                texture        = ScaleTexture(texture, texture.width, texture.height);
                unalteredScene = ScaleTexture(texture, texture.width, texture.height);
                orgTexture     = ScaleTexture(orgTexture, texture.width, texture.height);
            }
            else
            {
                int height = (int)Math.Round(480 / aspectRatio);
                texture        = ScaleTexture(texture, texture.width, texture.height);
                unalteredScene = ScaleTexture(texture, texture.width, texture.height);
                orgTexture     = ScaleTexture(orgTexture, texture.width, texture.height);
            }

            //OPENCV Color Picker
            Mat OrgTextureMat = new Mat(orgTexture.height, orgTexture.width, CvType.CV_8UC4);
            Utils.texture2DToMat(orgTexture, OrgTextureMat);


            Mat samples    = OrgTextureMat.reshape(1, OrgTextureMat.cols() * OrgTextureMat.rows());
            Mat samples32f = new Mat();
            samples.convertTo(samples32f, CvType.CV_32F, 1.0 / 255.0);

            Mat          labels   = new Mat();
            TermCriteria criteria = new TermCriteria(TermCriteria.COUNT, 100, 1);
            centers = new Mat();
            Core.kmeans(samples32f, 10, labels, criteria, 1, Core.KMEANS_PP_CENTERS, centers);

            //centers.convertTo(centers, CvType.CV_8UC1, 255.0);
            //centers.reshape(3);

            Debug.Log(centers);
            Debug.Log(centers.rows());
            Debug.Log(centers.cols());

            texture = changeColor(texture, centers);
            //OPENCV
            Mat TextureMat     = new Mat(texture.height, texture.width, CvType.CV_8UC4);
            Mat CopyTextureMat = new Mat(texture.height, texture.width, CvType.CV_8UC4);

            Imgproc.cvtColor(CopyTextureMat, CopyTextureMat, Imgproc.COLOR_RGB2BGRA);

            Utils.texture2DToMat(texture, TextureMat);
            Utils.texture2DToMat(texture, CopyTextureMat);
            Imgproc.cvtColor(CopyTextureMat, CopyTextureMat, Imgproc.COLOR_RGB2BGRA);
            Imgcodecs.imwrite("D:\\Thesis\\Outputs\\ColorChange_" + imageName + ".jpg", CopyTextureMat);

            //Mat refImg = new Mat(texture.height, texture.width, CvType.CV_8UC4);
            Mat Canvas = new Mat(texture.height, texture.width, CvType.CV_8UC4);
            //int[] radius = new int[] { 8, 4, 2 };

            if (resultMan.getSelectedPalette() == 0)
            {
                pre1886ColorChangedTexture = texture;
            }
            else if (resultMan.getSelectedPalette() == 1)
            {
                post1886ColorChangedTexture = texture;
            }
            else if (resultMan.getSelectedPalette() == 2)
            {
                finalYearsColorChangedTexture = texture;
            }

            // ZMQ BRUSH STROKE ALGORITHM
            byte[] colorChangedPic = texture.EncodeToJPG();
            string image64         = Convert.ToBase64String(colorChangedPic);

            // Copy this structure to add in change color
            clientSocket = new ClientSocketScript(image64, "GetBrushStrokes", resultMan.getBrushStrokeIndex(), resultMan.getBrushValues());
            clientSocket.Start();
            while (clientSocket.getResponse() == null)
            {
                clientSocket.Update();
            }


            bytes = Convert.FromBase64String(clientSocket.getResponse());
            clientSocket.Stop();
            texture.LoadImage(bytes);

            /*Mat grayMat = new Mat(TextureMat.rows(), TextureMat.cols(), CvType.CV_8UC1);
             * Mat gradientx = new Mat(TextureMat.rows(), TextureMat.cols(), CvType.CV_8UC1);
             * Mat gradienty = new Mat(TextureMat.rows(), TextureMat.cols(), CvType.CV_8UC1);
             *
             * Mat rgbaMat = new Mat(texture.height, texture.width, CvType.CV_8UC4);
             * Utils.texture2DToMat(texture, rgbaMat);
             *
             * Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
             * Imgproc.Scharr(grayMat, gradientx, rgbaMat.depth(), 1, 0, 1 / 15.36);
             * Imgproc.Scharr(grayMat, gradienty, rgbaMat.depth(), 0, 1, 1 / 15.36);
             *
             * //int gradient_smoothing_radius = Math.Round(Math.Max(rgbaMat.dims) / 50);
             * //chosen stroke scale: 2
             * //chosen gradient smoothing radius: 16
             * Imgproc.GaussianBlur(gradientx, gradientx, new Size(2 * 16 + 1, 2 * 16 + 1), 0);
             * Imgproc.GaussianBlur(gradienty, gradienty, new Size(2 * 16 + 1, 2 * 16 + 1), 0);
             *
             * Imgproc.medianBlur(rgbaMat, rgbaMat, 11);
             *
             * List<int> gridx = new List<int>();
             * List<int> gridy = new List<int>();
             * int index = 0;
             * System.Random rnd = new System.Random();
             *
             * //new grid
             * for (int i = 0; i < texture.width; i += 3)
             * {
             *  for (int j = 0; j < texture.height; j += 3)
             *  {
             *      int x = rnd.Next(-1, 2) + i;
             *      int y = rnd.Next(-1, 2) + j;
             *
             *      gridy.Add(y % texture.height);
             *      gridx.Add(x % texture.width);
             *      index++;
             *  }
             * }
             * //shuffle grid
             * int n = gridy.Count;
             * while (n > 1)
             * {
             *  n--;
             *  int k = rnd.Next(n + 1);
             *  int temp = gridy[k];
             *  gridy[k] = gridy[n];
             *  gridy[n] = temp;
             *
             *  temp = gridx[k];
             *  gridx[k] = gridx[n];
             *  gridx[n] = temp;
             * }
             *
             * int batch_size = 10000;
             * Debug.Log(gridx.Count + " " + gridy.Count);
             * List<Color32> pixels = new List<Color32>();
             * List<Color32> orgPixels = new List<Color32>();
             * double shortest = 50, longest = 0, angleShort = 0;
             * for (int h = 0; h < index - 1; h += batch_size)
             * {
             *  pixels = new List<Color32>();
             *  orgPixels = new List<Color32>();
             *  int endpoint = h + batch_size;
             *  if (endpoint > index - 1)
             *      endpoint = index - 1;
             *  //get the color from the texture
             *  for (int px = h; px < endpoint; px++)
             *  {
             *      Color32 cpixel = texture.GetPixel(gridx[px], (gridy[px] - (texture.height - 1)) * (-1));
             *      pixels.Add(cpixel);
             *      cpixel = orgTexture.GetPixel(gridx[px], (gridy[px] - (orgTexture.height - 1)) * (-1));
             *      orgPixels.Add(cpixel);
             *  }
             *  int cindex = 0;
             *  for (int px = h; px < endpoint; px++)
             *  {
             *      int x = gridx[px],
             *          y = gridy[px];
             *
             *      //get color
             *      Color32 cpixel;
             *      //use color of pixel
             *      int cprob = rnd.Next(1, 11);
             *
             *      //if(cprob <= 10) {
             *      Color a, b;
             *      a = orgPixels[cindex];
             *      b = pixels[cindex];
             *      //here lol
             *      cpixel = (a + b) / 2;
             *      //cpixel = a;
             *      //} else {
             *      //if (cprob > 5) {
             *      List<Color32> c_palette = color_palette;
             *      c_palette.Remove(pixels[cindex]);
             *      cprob = 0;
             *      cprob = rnd.Next(0, c_palette.Count - 1);
             *      a = c_palette[cprob];
             *      b = cpixel;
             *      //cpixel = (a  + b )/2;
             *      //cpixel = b*1.1f;
             *
             *
             *      //cpixel = a;
             *
             *      //}
             *      cindex++;
             *      //get angle
             *      double length = Math.Round(2 + 2 * Math.Sqrt(Math.Sqrt(gradienty.get(y, x)[0] * gradienty.get(y, x)[0] + gradientx.get(y, x)[0] * gradientx.get(y, x)[0])));
             *      double angle = (180 / Math.PI) * (Math.Atan2(gradienty.get(y, x)[0], gradientx.get(y, x)[0])) + 90;
             *      double lengthb = 1;
             *      if (length < shortest)
             *      {
             *          shortest = length;
             *          angleShort = angle;
             *      }
             *      if (length > longest)
             *          longest = length;
             *      if (length > 2 && angle != 90)
             *      {
             *          length /= 3;
             *      }
             *      else
             *      {
             *          angle += 80;
             *          length = 10;
             *          lengthb = 2;
             *          float H, S, V;
             *          Color.RGBToHSV(b, out H, out S, out V);
             *          float sat = cprob * 0.01f;
             *          sat += 1;
             *          S *= sat;
             *          cpixel = Color.HSVToRGB(H, S, V);
             *      }
             *      Imgproc.ellipse(rgbaMat, new Point(x, y), new Size(length, lengthb), angle, 0, 360, new Scalar(cpixel.r, cpixel.g, cpixel.b), -1, Imgproc.LINE_AA);
             *  }
             *  //Imgcodecs.imwrite("D:\\Thesis\\Outputs\\PredecessorBrushStroke" + h + ".jpg", rgbaMat);
             * }
             * Debug.Log("Longest : " + longest);
             * Debug.Log("Shortest : " + shortest);
             * Debug.Log("Angle : " + angleShort);
             * Utils.matToTexture2D(rgbaMat, texture);*/


            //Imgproc.cvtColor(Canvas, Canvas, Imgproc.COLOR_RGB2BGRA);
            //Utils.matToTexture2D(Canvas, texture);

            //texture = changeColor(texture, centers);

            Imgproc.cvtColor(Canvas, Canvas, Imgproc.COLOR_RGB2BGRA);
            Utils.texture2DToMat(texture, Canvas);
            Imgproc.cvtColor(Canvas, Canvas, Imgproc.COLOR_RGB2BGRA);

            //Imgcodecs.imwrite("D:\\Thesis\\Outputs\\OldBrushStroke_" + imageName + "_Final.jpg", Canvas);
            //Imgcodecs.imwrite("D:\\Thesis\\Outputs\\OldBrushStroke_" + imageName +"_ColorChanged.jpg", Canvas);

            texture.Apply();

            Sprite sprite = Sprite.Create(texture, new UnityEngine.Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100);

            string imageNameToBeRendered = "Image" + resultMan.getBrushStrokeIndex().ToString() + "_";

            if (resultMan.getSelectedPalette() == 0)
            {
                imageNameToBeRendered += "Pre";
            }
            else if (resultMan.getSelectedPalette() == 1)
            {
                imageNameToBeRendered += "Post";
            }
            else if (resultMan.getSelectedPalette() == 2)
            {
                imageNameToBeRendered += "Final";
            }

            for (int i = 0; i < images.Length; i++)
            {
                if (imageNameToBeRendered.Contains(images[i].name))
                {
                    images[i].sprite  = sprite;
                    images[i].enabled = true;
                    images[i].GetComponentInChildren <ImageParameters>().setBrushStrokeOpacities(resultMan.getBrushValues());
                    images[i].GetComponentInChildren <ImageParameters>().setColorPaletteSelected(resultMan.getSelectedPalette());
                    images[i].GetComponentInChildren <ImageParameters>().setBrushStrokeIndex(resultMan.getBrushStrokeIndex());
                    resultMan.setCurrentDisplayedImageName(images[i].name);
                }
            }

            //screenshot = sprite;
            arcamera.SetActive(false);

            resultMan.setIsRendered(true);
            resultMan.resetColorPaletteButtons();

            /*Mat grad_x = new Mat();
             * Mat grad_y = new Mat();
             * Mat abs_grad_x = new Mat();
             * Mat abs_grad_y = new Mat();
             * Mat dst = new Mat();
             * Mat final = new Mat();
             * Mat sobelMat = new Mat(texture.height, texture.width, CvType.CV_8UC4);
             *
             *
             * Utils.texture2DToMat(texture, sobelMat);
             *
             * Imgproc.GaussianBlur(sobelMat, sobelMat, new Size(3, 3), 0, 0);
             * Imgproc.cvtColor(sobelMat, dst, Imgproc.COLOR_RGBA2GRAY);
             *
             * Imgproc.Sobel(dst, grad_x, CvType.CV_16S, 1, 0, 3, 1, 0);
             * Core.convertScaleAbs(grad_x, abs_grad_x);
             *
             * Imgproc.Sobel(dst, grad_y, CvType.CV_16S, 0, 1, 3, 1, 0);
             * Core.convertScaleAbs(grad_y, abs_grad_y);
             *
             * Core.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, final);
             *
             * Imgcodecs.imwrite("D:\\Thesis\\Outputs\\Final_Edge.jpg", final);*/
        }
    }