예제 #1
0
        public static void PutTextWithCenter(Mat img, Point textCenter, string text,
                                             Scalar textColor, HersheyFonts font, double fontScale,
                                             int thickness, LineTypes lineType)
        {
            Size  textSize     = GetTextSize(text, font, fontScale);
            Point textLocation = new Point(textCenter.X - textSize.Width / 2, textCenter.Y + textSize.Height / 2);

            Cv2.PutText(img, text, textLocation, font, fontScale, textColor, thickness, lineType);
        }
예제 #2
0
 /// <summary>
 /// Prints tracks information.
 /// </summary>
 /// <param name="tracks">List of tracks.</param>
 /// <param name="imgSource">Input image (depth=IPL_DEPTH_8U and num. channels=3).</param>
 /// <param name="imgDest">Output image (depth=IPL_DEPTH_8U and num. channels=3).</param>
 /// <param name="mode">Render mode. By default is CV_TRACK_RENDER_ID.</param>
 /// <param name="textColor"></param>
 /// <param name="fontFace"></param>
 /// <param name="fontScale"></param>
 /// <param name="thickness"></param>
 public static void RenderTracks(CvTracks tracks, Mat imgSource, Mat imgDest, RenderTracksMode mode,
                                 Scalar textColor, HersheyFonts fontFace = HersheyFonts.HersheySimplex, double fontScale = 1d, int thickness = 1)
 {
     if (tracks == null)
     {
         throw new ArgumentNullException(nameof(tracks));
     }
     tracks.Render(imgSource, imgDest, mode, textColor, fontFace, fontScale, thickness);
 }
예제 #3
0
        /// <summary>
        /// Prints tracks information.
        /// </summary>
        /// <param name="imgSource">Input image (depth=IPL_DEPTH_8U and num. channels=3).</param>
        /// <param name="imgDest">Output image (depth=IPL_DEPTH_8U and num. channels=3).</param>
        /// <param name="mode">Render mode. By default is CV_TRACK_RENDER_ID.</param>
        /// <param name="textColor"></param>
        /// <param name="fontFace"></param>
        /// <param name="fontScale"></param>
        /// <param name="thickness"></param>
        public void Render(Mat imgSource, Mat imgDest, RenderTracksMode mode, Scalar textColor,
                           HersheyFonts fontFace = HersheyFonts.HersheySimplex, double fontScale = 1d, int thickness = 1)
        {
            if (imgSource == null)
            {
                throw new ArgumentNullException(nameof(imgSource));
            }
            if (imgDest == null)
            {
                throw new ArgumentNullException(nameof(imgDest));
            }
            if (imgDest.Type() != MatType.CV_8UC3)
            {
                throw new ArgumentException("imgDest.Depth != U8 || imgDest.NChannels != 3");
            }

            if (mode != RenderTracksMode.None)
            {
                foreach (KeyValuePair <int, CvTrack> kv in this)
                {
                    int     key   = kv.Key;
                    CvTrack value = kv.Value;

                    if ((mode & RenderTracksMode.Id) == RenderTracksMode.Id)
                    {
                        if (value.Inactive == 0)
                        {
                            Cv2.PutText(imgDest, key.ToString(), (Point)value.Centroid,
                                        fontFace, fontScale, textColor, thickness);
                        }
                    }
                    if ((mode & RenderTracksMode.BoundingBox) == RenderTracksMode.BoundingBox)
                    {
                        if (value.Inactive > 0)
                        {
                            Cv2.Rectangle(
                                imgDest,
                                new Point(value.MinX, value.MinY),
                                new Point(value.MaxX - 1, value.MaxY - 1),
                                new Scalar(50, 0, 0));
                        }
                        else
                        {
                            Cv2.Rectangle(
                                imgDest,
                                new Point(value.MinX, value.MinY),
                                new Point(value.MaxX - 1, value.MaxY - 1),
                                new Scalar(255, 0, 0));
                        }
                    }
                }
            }
        }
예제 #4
0
        public static void DrawTextTest(string text           = "Hello, world", int x = 100, int y = 100,
                                        HersheyFonts fontFace = HersheyFonts.HersheyPlain, double fontScale = 1, byte red = 0, byte green = 0, byte blue = 0, int thickness = 1, LineTypes lineType = LineTypes.Link8, bool bottomLeftOrigin = false)
        {
            Glb.DrawMatAndHist0(Glb.matSrc);

            Mat    mDst  = Glb.matSrc.Clone();
            Scalar color = new Scalar(blue, green, red);

            mDst.PutText(text, new Point(x, y), fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin);
            Glb.DrawMatAndHist1(mDst);

            Glb.DrawMatAndHist2(null);
            mDst.Dispose();
        }
예제 #5
0
        void Start()
        {
            // some constants for drawing
            const int          textPadding   = 2;
            const HersheyFonts textFontFace  = HersheyFonts.HersheyPlain;
            double             textFontScale = System.Math.Max(this.texture.width, this.texture.height) / 512.0;
            Scalar             boxColor      = Scalar.DeepPink;
            Scalar             textColor     = Scalar.White;

            // load alphabet
            AlphabetOCR alphabet = new AlphabetOCR(model.bytes);

            // scan image
            var image = Unity.TextureToMat(this.texture);
            IList <AlphabetOCR.RecognizedLetter> letters = alphabet.ProcessImage(image);

            foreach (var letter in letters)
            {
                int line;
                var bounds = Cv2.BoundingRect(letter.Rect);

                // text box.
                var textData = string.Format("{0}: {1}%", letter.Data, System.Math.Round(letter.Confidence * 100));
                var textSize = Cv2.GetTextSize(textData, textFontFace, textFontScale, 1, out line);
                var textBox  = new Rect(
                    bounds.X + (bounds.Width - textSize.Width) / 2 - textPadding,
                    bounds.Bottom,
                    textSize.Width + textPadding * 2,
                    textSize.Height + textPadding * 2
                    );

                // draw shape
                image.Rectangle(bounds, boxColor, 2);
                image.Rectangle(textBox, boxColor, -1);
                image.PutText(textData, textBox.TopLeft + new Point(textPadding, textPadding + textSize.Height), textFontFace, textFontScale, textColor, (int)(textFontScale + 0.5));
            }

            // result
            UnityEngine.Texture2D texture = Unity.MatToTexture(image);

            // output
            RawImage rawImage = gameObject.GetComponent <RawImage>();

            rawImage.texture = texture;

            var transform = gameObject.GetComponent <UnityEngine.RectTransform>();

            transform.sizeDelta = new UnityEngine.Vector2(image.Width, image.Height);
        }
예제 #6
0
        internal static void DrawText(Mat frame, int x, int y, string text)
        {
            HersheyFonts fontFace  = HersheyFonts.HersheySimplex;
            double       fontScale = (double)frame.Height / _stdHeight;
            int          thickness = frame.Height / _stdHeight + 1;
            Size         size      = Cv2.GetTextSize(nameof(DrawText), fontFace, fontScale, thickness, out int baseline);

            string[] lines = text.Split('\n');
            for (int i = 0; i < lines.Length; i++)
            {
                string line   = lines[i];
                var    cursor = new Point(x, y + (i + 1) * (size.Height + baseline));
                Cv2.PutText(frame, line, cursor, fontFace, fontScale, Scalar.Red, thickness, LineTypes.AntiAlias);
            }
        }
예제 #7
0
        public Mat ImageWithText(out int correctNumber)
        {
            Scalar       color     = new Scalar(255);
            HersheyFonts font      = HersheyFonts.HersheyPlain;
            double       fontScale = 1.0;
            int          thickness = 1;
            LineTypes    lineType  = LineTypes.Link4;
            string       text      = "Mizu?";

            Size textSize = GraphicsHelper.GetTextSize("Mizu?", font, fontScale);
            Mat  result   = new Mat(textSize.Height + 10, textSize.Width + 20, MatType.CV_8UC1, new Scalar(0));

            GraphicsHelper.PutTextWithCenter(result, new Point(result.Cols / 2, result.Rows / 2),
                                             text, color, font, fontScale, thickness, lineType);

            correctNumber = 8;
            return(result);
        }
예제 #8
0
        public void Text(string text, int x, int y, Scalar?col = null, float size = SIZE_NORMAL,
                         HersheyFonts font = FONT_SERIF, Mat frame = null)
        {
            if (frame == null)
            {
                frame = Processor.Output;
            }

            for (int ox = -1; ox <= 1; ox++)
            {
                for (int oy = -1; oy <= 1; oy++)
                {
                    Cv2.PutText(frame, text, new Point(x + ox, y + oy), font, FontSize * size, BLACK, 2, LineTypes.AntiAlias);
                }
            }

            Cv2.PutText(frame, text, new Point(x, y), font, FontSize * size, col ?? TextColor, 2, LineTypes.AntiAlias);
        }
예제 #9
0
        public static Size GetTextSize(string text, HersheyFonts font, double fontScale)
        {
            int baseline = 0;

            return(Cv2.GetTextSize(text, font, fontScale, 1, out baseline));
        }
예제 #10
0
 /// <summary>
 /// Prints tracks information.
 /// </summary>
 /// <param name="tracks">List of tracks.</param>
 /// <param name="imgSource">Input image (depth=IPL_DEPTH_8U and num. channels=3).</param>
 /// <param name="imgDest">Output image (depth=IPL_DEPTH_8U and num. channels=3).</param>
 /// <param name="mode">Render mode. By default is CV_TRACK_RENDER_ID.</param>
 /// <param name="textColor"></param>
 /// <param name="fontFace"></param>
 /// <param name="fontScale"></param>
 /// <param name="thickness"></param>
 public static void RenderTracks(CvTracks tracks, Mat imgSource, Mat imgDest, RenderTracksMode mode,
     Scalar textColor, HersheyFonts fontFace = HersheyFonts.HersheySimplex, double fontScale = 1d, int thickness = 1)
 {
     if (tracks == null)
         throw new ArgumentNullException(nameof(tracks));
     tracks.Render(imgSource, imgDest, mode, textColor, fontFace, fontScale, thickness);
 }
예제 #11
0
 /// <summary>
 /// returns bounding box of the text string
 /// </summary>
 /// <param name="text"></param>
 /// <param name="fontFace"></param>
 /// <param name="fontScale"></param>
 /// <param name="thickness"></param>
 /// <param name="baseLine"></param>
 /// <returns></returns>
 public static Size GetTextSize(string text, HersheyFonts fontFace,
     double fontScale, int thickness, out int baseLine)
 {
     if (String.IsNullOrEmpty(text))
         throw new ArgumentNullException(text);
     return NativeMethods.core_getTextSize(text, (int)fontFace, fontScale, thickness, out baseLine);
 }
예제 #12
0
 /// <summary>
 /// renders text string in the image
 /// </summary>
 /// <param name="img"></param>
 /// <param name="text"></param>
 /// <param name="org"></param>
 /// <param name="fontFace"></param>
 /// <param name="fontScale"></param>
 /// <param name="color"></param>
 /// <param name="thickness"></param>
 /// <param name="lineType"></param>
 /// <param name="bottomLeftOrigin"></param>
 public static void PutText(InputOutputArray img, string text, Point org,
     HersheyFonts fontFace, double fontScale, Scalar color,
     int thickness = 1, LineTypes lineType = LineTypes.Link8, bool bottomLeftOrigin = false)
 {
     if (img == null)
         throw new ArgumentNullException(nameof(img));
     if (String.IsNullOrEmpty(text))
         throw new ArgumentNullException(text);
     img.ThrowIfDisposed();
     NativeMethods.core_putText(img.CvPtr, text, org, (int)fontFace, fontScale, color,
         thickness, (int)lineType, bottomLeftOrigin ? 1 : 0);
     img.Fix();
 }
예제 #13
0
 /// <summary>
 /// renders text string in the image
 /// </summary>
 /// <param name="text"></param>
 /// <param name="org"></param>
 /// <param name="fontFace"></param>
 /// <param name="fontScale"></param>
 /// <param name="color"></param>
 /// <param name="thickness"></param>
 /// <param name="lineType"></param>
 /// <param name="bottomLeftOrigin"></param>
 public void PutText(string text, Point org,
     HersheyFonts fontFace, double fontScale, Scalar color,
     int thickness = 1,
     LineTypes lineType = LineTypes.Link8,
     bool bottomLeftOrigin = false)
 {
     Cv2.PutText(this, text, org, fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin);
 }
예제 #14
0
    // Update is called once per frame
    void Update()
    {
        //string name = "Form";



        //Texture2D inputTexture = (Texture2D)Resources.Load("DocumentScanner/" + name);

        //Texture2D inputTexture = new Texture2D(webcamTexture.width, webcamTexture.height);


        //inputTexture.SetPixels(webcamTexture.GetPixels());
        //inputTexture.Apply();


        scanner.Settings.NoiseReduction = noiseReduction_test;                                          // real-world images are quite noisy, this value proved to be reasonable
        scanner.Settings.EdgesTight     = EdgesTight_test;                                              // higher value cuts off "noise" as well, this time smaller and weaker edges
        scanner.Settings.ExpectedArea   = ExpectedArea_test;                                            // we expect document to be at least 20% of the total image area

        scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.Grayscale;


/*         if (!isCaptured)
 *      {
 *          inputPlane.GetComponent<Renderer>().material.mainTexture = webcamTexture;
 *          inputPlane.transform.localScale = new Vector3((float)webcamTexture.width / (float)webcamTexture.height, 1, 1);
 *      } */

        //Input.GetKeyDown(KeyCode.Space)
        if (InteractWithScanner.isNewImage && InteractWithScanner.isNewMode)
        {
            //string name = "Form";
            //Texture2D inputTexture = (Texture2D)Resources.Load("DocumentScanner/" + name);

            /* Texture2D inputTexture = new Texture2D(webcamTexture.width, webcamTexture.height);
             *
             *
             * inputTexture.SetPixels(webcamTexture.GetPixels());
             * inputTexture.Apply();
             *
             * inputPlane.GetComponent<Renderer>().material.mainTexture = inputTexture;
             * inputPlane.transform.localScale = new Vector3((float)inputTexture.width / (float)inputTexture.height, 1, 1); */


            Texture2D inputTexture = (Texture2D)inputPlane.GetComponent <Renderer>().material.mainTexture;


            if (InteractWithScanner.modeSelected == 0 || InteractWithScanner.modeSelected == 1 || InteractWithScanner.modeSelected == 2)
            {
                isCaptured = true;

                //savedTexture = inputTexture;
                savedTexture     = InteractWithScanner.virtualPhoto;
                beginningTexture = InteractWithScanner.virtualPhoto;

                beginingMat = OpenCvSharp.Unity.TextureToMat(beginningTexture);

                allSelectedPoints = new Point[4];

                pointCounter = 0;

                autoFailed = false;
            }

            if (InteractWithScanner.modeSelected == 1 || InteractWithScanner.modeSelected == 2)
            {
                scanner.Input = OpenCvSharp.Unity.TextureToMat(inputTexture);


                // should we fail, there is second try - HSV might help to detect paper by color difference
                // if (!scanner.Success)
                //     // this will drop current result and re-fetch it next time we query for 'Success' flag or actual data
                //     scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale;

                if (!scanner.Success)
                {
                    Mat          failMat      = OpenCvSharp.Unity.TextureToMat(inputTexture);
                    var          textData     = string.Format("{0}", "No contour guess. Use Manual");
                    HersheyFonts textFontFace = HersheyFonts.HersheyPlain;
                    failMat.PutText(textData, new Point(failMat.Width / 2, failMat.Height / 2), textFontFace, 20, Scalar.White);

                    Texture2D outputTexture = OpenCvSharp.Unity.MatToTexture(failMat);

                    inputPlane.GetComponent <Renderer>().material.mainTexture = outputTexture;

                    //hardcoded values need to be changed

/*                     inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0f, 1f);
 *                  inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(1f, -1f); */

                    /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                     * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f);  */


                    //inputPlane.transform.localScale = new Vector3( currScale.x*((float)resultContoured.Width / (float)resultContoured.Height), currScale.y, currScale.z);

                    textureNew = outputTexture;

                    savedTexture = outputTexture;

                    autoFailed = true;

                    isCaptured = false;
                }
                else
                {
                    Mat resultContoured = null;



                    for (int i = 0; i < scanner.PaperShape.Length; i++)
                    {
                        float currPointX = (float)scanner.PaperShape[i].X;
                        float currPointY = (float)scanner.PaperShape[i].Y;


                        currPointX *= (float)inputTexture.width;
                        currPointY *= (float)inputTexture.height;

                        currPointY = inputTexture.height - currPointY;

/*                         currPointX /= (float)inputTexture.width;
 *                      currPointY /= (float)inputTexture.height;
 *
 *                      currPointX *= (float)inputTexture.width/2f;
 *                      currPointY *= (float)inputTexture.height/2f;
 *
 *
 *                      currPointY += ((float)inputTexture.width/2.0f)*0.375f;
 *                      currPointX += ((float)inputTexture.width/2.0f)*0.5f; */

                        scanner.PaperShape[i] = new Point((int)currPointX, (int)currPointY);
                    }



                    resultContoured = JustImageContour(scanner.Input, scanner.PaperShape);

                    allSelectedPoints = scanner.PaperShape;

                    Point2f[] currPointsCon = new Point2f[4];

                    for (int i = 0; i < allSelectedPoints.Length; i++)
                    {
                        resultContoured.DrawMarker(allSelectedPoints[i].X, allSelectedPoints[i].Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 50, LineTypes.Link8, 5);

                        currPointsCon[i] = allSelectedPoints[i];
                    }


                    Texture2D outputTexture = OpenCvSharp.Unity.MatToTexture(resultContoured);

                    inputPlane.GetComponent <Renderer>().material.mainTexture = outputTexture;

                    //hardcoded values need to be changed

                    /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                     * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                    //inputPlane.transform.localScale = new Vector3( currScale.x*((float)resultContoured.Width / (float)resultContoured.Height), currScale.y, currScale.z);

                    textureNew = outputTexture;

                    /* outputPlane.GetComponent<Renderer>().material.mainTexture = webcamTexture;
                     * outputPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1); */



                    Mat test = OpenCvSharp.Demo.MatUtilities.UnwrapShape(beginingMat, currPointsCon);


                    //Mat resultUnwarpped = null;

                    //Point[] noContour = new Point[1];

                    //resultUnwarpped = JustImageContour(scanner.Output, noContour);
                    Texture2D unwrappedTexture = OpenCvSharp.Unity.MatToTexture(test);

                    unwrappedPlane.GetComponent <Renderer>().material.mainTexture = unwrappedTexture;



                    //unwrappedPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);

                    //isCaptured = true;


                    savedTexture = outputTexture;
                }
            }



            if (!autoFailed)
            {
                InteractWithScanner.isNewImage = false;
                InteractWithScanner.isNewMode  = false;
            }
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            isCaptured = false;
        }

        if (isCaptured)
        {
            if (InteractWithScanner.modeSelected == 0 || InteractWithScanner.modeSelected == 1)
            {
                RaycastHit hit;
                //var ray = Camera.main.ScreenPointToRay(Input.mousePosition);



                Mat matCurrent = null;

                if (Physics.Raycast(casterObj_scanner.transform.position, -casterObj_scanner.transform.forward, out hit, 100) && hit.transform.name == "inputPlane")
                {
                    inputPlane.GetComponent <Renderer>().material.mainTexture = savedTexture;

                    //hardcoded values need to be changed

                    /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                     * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */

                    //inputPlane.transform.localScale = new Vector3( currScale.x*((float)savedTexture.width / (float)savedTexture.height), currScale.y, currScale.z);


                    Renderer rend = hit.transform.GetComponent <Renderer>();

                    Texture2D tex     = rend.material.mainTexture as Texture2D;
                    Vector2   pixelUV = hit.textureCoord;

                    //pixelUV.x += pixelUV.x*0.75f;
                    pixelUV.y = 1 - pixelUV.y;

                    pixelUV.x *= tex.width;
                    pixelUV.y *= tex.height;


                    /* pixelUV.y += (tex.width/2)*0.375f;
                     * pixelUV.x += (tex.width/2)*0.5f;
                     */

                    matCurrent = OpenCvSharp.Unity.TextureToMat(tex);
                    Point currPoint = new Point(pixelUV.x, pixelUV.y);


                    if (mouseMoveHeld)
                    {
                        matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(0, 255, 0), MarkerStyle.Cross, 50, LineTypes.Link8, 5);
                    }
                    else
                    {
                        matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(255, 0, 0), MarkerStyle.Cross, 50, LineTypes.Link8, 5);
                    }



                    textureNew = OpenCvSharp.Unity.MatToTexture(matCurrent);

                    inputPlane.GetComponent <Renderer>().material.mainTexture = textureNew;
                    //hardcoded values need to be changed

                    /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                     * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                    //inputPlane.transform.localScale = new Vector3( currScale.x*((float)textureNew.width / (float)textureNew.height), currScale.y, currScale.z);


                    if (raySelect.GetStateDown(handType_right))
                    {
                        if (InteractWithScanner.modeSelected == 0)
                        {
                            if (pointCounter <= 4)
                            {
                                matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 50, LineTypes.Link8, 5);


                                if (pointCounter > 3)
                                {
                                    pointCounter = 0;

                                    allSelectedPoints = new Point[4];

                                    matCurrent = OpenCvSharp.Unity.TextureToMat(beginningTexture);

                                    matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 50, LineTypes.Link8, 5);
                                }


                                allSelectedPoints[pointCounter] = currPoint;

                                textureNew = OpenCvSharp.Unity.MatToTexture(matCurrent);



                                inputPlane.GetComponent <Renderer>().material.mainTexture = textureNew;
                                //hardcoded values need to be changed

                                /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                                 * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                                //inputPlane.transform.localScale = new Vector3( currScale.x*((float)textureNew.width / (float)textureNew.height), currScale.y, currScale.z);

                                savedTexture = textureNew;
                            }
                            if (pointCounter == 3)
                            {
                                //Point[] allSelectedPoints_order = new Point[4];

                                allSelectedPoints = SortCorners(allSelectedPoints);



                                Mat resultContoured = null;
                                resultContoured = JustImageContour(matCurrent, allSelectedPoints);

                                Texture2D contourTexture = OpenCvSharp.Unity.MatToTexture(resultContoured);

                                inputPlane.GetComponent <Renderer>().material.mainTexture = contourTexture;
                                //hardcoded values need to be changed

                                /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                                 * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                                //inputPlane.transform.localScale = new Vector3( currScale.x*((float)resultContoured.Width / (float)resultContoured.Height), currScale.y, currScale.z);


                                savedTexture = contourTexture;

                                Point2f[] currPointsCon = new Point2f[4];

                                for (int i = 0; i < allSelectedPoints.Length; i++)
                                {
                                    currPointsCon[i] = allSelectedPoints[i];
                                }

                                Mat test = OpenCvSharp.Demo.MatUtilities.UnwrapShape(beginingMat, currPointsCon);

                                Texture2D unwrappedTexture = OpenCvSharp.Unity.MatToTexture(test);

                                unwrappedPlane.GetComponent <Renderer>().material.mainTexture = unwrappedTexture;
                                //unwrappedPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);
                            }


                            pointCounter++;
                        }
                        else if (InteractWithScanner.modeSelected == 1 && !selectedForChange)
                        {
                            closePoint = new Point(-1, -1);
                            foundIndex = -1;

                            // First  selected for now maybe change later
                            for (int i = 0; i < allSelectedPoints.Length; i++)
                            {
                                float distBetweenP = Mathf.Sqrt(Mathf.Pow((currPoint.X - allSelectedPoints[i].X), 2f) + Mathf.Pow((currPoint.Y - allSelectedPoints[i].Y), 2f));
                                if (distBetweenP < distTresh)
                                {
                                    closePoint = allSelectedPoints[i];

                                    foundIndex = i;

                                    selectedForChange = true;

                                    break;
                                }
                            }

                            if (selectedForChange)
                            {
                                inputPlane.GetComponent <Renderer>().material.mainTexture = beginningTexture;
                                //hardcoded values need to be changed

                                /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                                 * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                                //inputPlane.transform.localScale = new Vector3( currScale.x*((float)beginningTexture.width / (float)beginningTexture.height), currScale.y, currScale.z);

                                Mat currMat = OpenCvSharp.Unity.TextureToMat(beginningTexture);



                                for (int i = 0; i < allSelectedPoints.Length; i++)
                                {
                                    if (i != foundIndex)
                                    {
                                        currMat.DrawMarker(allSelectedPoints[i].X, allSelectedPoints[i].Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 50, LineTypes.Link8, 5);
                                    }
                                }


                                Texture2D changedTexture = OpenCvSharp.Unity.MatToTexture(currMat);

                                inputPlane.GetComponent <Renderer>().material.mainTexture = changedTexture;
                                //hardcoded values need to be changed

                                /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                                 * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                                //inputPlane.transform.localScale = new Vector3( currScale.x*((float)textureNew.width / (float)textureNew.height), currScale.y, currScale.z);

                                savedTexture = changedTexture;
                            }
                        }
                    }

                    if (raySelect.GetState(handType_right) && selectedForChange)
                    {
                        mouseMoveHeld = true;
                    }

                    if (raySelect.GetStateUp(handType_right) && selectedForChange)
                    {
                        allSelectedPoints[foundIndex] = currPoint;



                        Mat currMat = OpenCvSharp.Unity.TextureToMat(beginningTexture);

                        currMat = JustImageContour(currMat, allSelectedPoints);


                        Point2f[] currPointsCon = new Point2f[4];

                        for (int i = 0; i < allSelectedPoints.Length; i++)
                        {
                            currMat.DrawMarker(allSelectedPoints[i].X, allSelectedPoints[i].Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 50, LineTypes.Link8, 5);

                            currPointsCon[i] = allSelectedPoints[i];
                        }


                        Texture2D changedTexture = OpenCvSharp.Unity.MatToTexture(currMat);

                        inputPlane.GetComponent <Renderer>().material.mainTexture = changedTexture;
                        //hardcoded values need to be changed

                        /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                         * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                        //inputPlane.transform.localScale = new Vector3( currScale.x*((float)textureNew.width / (float)textureNew.height), currScale.y, currScale.z);

                        savedTexture = changedTexture;


                        Mat test = OpenCvSharp.Demo.MatUtilities.UnwrapShape(beginingMat, currPointsCon);

                        Texture2D unwrappedTexture = OpenCvSharp.Unity.MatToTexture(test);

                        unwrappedPlane.GetComponent <Renderer>().material.mainTexture = unwrappedTexture;
                        //unwrappedPlane.transform.localScale = new Vector3((float)test.Width / (float)test.Height, 1, 1);



                        mouseMoveHeld = false;

                        selectedForChange = false;
                    }
                }


                if (clearSelect.GetLastStateDown(handType_right))
                {
                    pointCounter      = 0;
                    allSelectedPoints = new Point[4];


                    inputPlane.GetComponent <Renderer>().material.mainTexture = beginningTexture;
                    //hardcoded values need to be changed

                    /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                     * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                    //inputPlane.transform.localScale = new Vector3( currScale.x*((float)beginningTexture.width / (float)beginningTexture.height), currScale.y, currScale.z);

                    savedTexture = beginningTexture;

                    if (InteractWithScanner.modeSelected == 1)
                    {
                        InteractWithScanner.isNewImage = true;
                        InteractWithScanner.isNewMode  = true;

                        isCaptured = false;
                    }
                }
            }
        }


        //if (operationMode == 1)
        //{
        //    scanner.Input = OpenCvSharp.Unity.TextureToMat(webcamTexture);


        //    // should we fail, there is second try - HSV might help to detect paper by color difference
        //    if (!scanner.Success)
        //        // this will drop current result and re-fetch it next time we query for 'Success' flag or actual data
        //        scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale;



        //    Mat resultContoured = null;

        //    resultContoured = JustImageContour(scanner.Input, scanner.PaperShape);



        //    Texture2D outputTexture = OpenCvSharp.Unity.MatToTexture(resultContoured);

        //    outputPlane.GetComponent<Renderer>().material.mainTexture = outputTexture;
        //    outputPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);

        //    inputPlane.GetComponent<Renderer>().material.mainTexture = webcamTexture;
        //    inputPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);


        //    Mat resultUnwarpped = null;

        //    Point[] noContour = new Point[1];

        //    resultUnwarpped = JustImageContour(scanner.Output, noContour);
        //    Texture2D unwrappedTexture = OpenCvSharp.Unity.MatToTexture(resultUnwarpped);

        //    unwrappedPlane.GetComponent<Renderer>().material.mainTexture = unwrappedTexture;
        //    unwrappedPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);
        //}
        //else
        //{

        //    if (!isCaptured)
        //    {
        //        inputPlane.GetComponent<Renderer>().material.mainTexture = webcamTexture;
        //        inputPlane.transform.localScale = new Vector3((float)webcamTexture.width / (float)webcamTexture.height, 1, 1);
        //    }



        //    if (Input.GetKeyDown(KeyCode.Space))
        //    {


        //        Texture2D inputTexture = new Texture2D(webcamTexture.width, webcamTexture.height);


        //        inputTexture.SetPixels(webcamTexture.GetPixels());
        //        inputTexture.Apply();

        //        inputPlane.GetComponent<Renderer>().material.mainTexture = inputTexture;
        //        inputPlane.transform.localScale = new Vector3((float)webcamTexture.width / (float)webcamTexture.height, 1, 1);

        //        isCaptured = true;

        //        savedTexture = inputTexture;

        //        beginningTexture = inputTexture;

        //        allSelectedPoints = new Point[4];

        //        pointCounter = 0;

        //    }

        //    if (Input.GetKeyDown(KeyCode.Escape))
        //    {
        //        isCaptured = false;
        //    }


        //    if (isCaptured)
        //    {


        //        RaycastHit hit;
        //        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        //        Mat matCurrent = null;

        //        if (Physics.Raycast(ray, out hit) && hit.transform.name == "inputPlane")
        //        {

        //            inputPlane.GetComponent<Renderer>().material.mainTexture = savedTexture;
        //            inputPlane.transform.localScale = new Vector3((float)savedTexture.width / (float)savedTexture.height, 1, 1);

        //            Renderer rend = hit.transform.GetComponent<Renderer>();

        //            Texture2D tex = rend.material.mainTexture as Texture2D;
        //            Vector2 pixelUV = hit.textureCoord;
        //            pixelUV.x *= tex.width;
        //            pixelUV.y *= tex.height;

        //            matCurrent = OpenCvSharp.Unity.TextureToMat(tex);
        //            Point currPoint = new Point(pixelUV.x, matCurrent.Height - pixelUV.y);


        //            matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(255, 0, 0), MarkerStyle.Cross, 100, LineTypes.Link8, 5);


        //            Texture2D textureNew = OpenCvSharp.Unity.MatToTexture(matCurrent);

        //            inputPlane.GetComponent<Renderer>().material.mainTexture = textureNew;
        //            inputPlane.transform.localScale = new Vector3((float)textureNew.width / (float)textureNew.height, 1, 1);


        //            if (Input.GetMouseButtonDown(0))
        //            {


        //                //if (pointCounter > 4)
        //                //{
        //                //    pointCounter = 0;

        //                //    allSelectedPoints = new Point[4];


        //                //    //inputPlane.GetComponent<Renderer>().material.mainTexture = beginningTexture;
        //                //    //inputPlane.transform.localScale = new Vector3((float)beginningTexture.width / (float)beginningTexture.height, 1, 1);



        //                //}


        //                //if (Physics.Raycast(ray, out hit) && hit.transform.name == "inputPlane")
        //                //{



        //                //Debug.Log(pixelUV);

        //                //Mat matCurrent = OpenCvSharp.Unity.TextureToMat(tex);
        //                //Point currPoint = new Point(pixelUV.x, matCurrent.Height - pixelUV.y);



        //                //else if (pointCounter > 3)
        //                //{
        //                //    pointCounter = 0;

        //                //    allSelectedPoints = new Point[4];


        //                //    //inputPlane.GetComponent<Renderer>().material.mainTexture = beginningTexture;
        //                //    //inputPlane.transform.localScale = new Vector3((float)beginningTexture.width / (float)beginningTexture.height, 1, 1);

        //                //    savedTexture = beginningTexture;
        //                //}
        //                if (pointCounter <=4)
        //                {

        //                    matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 100, LineTypes.Link8, 5);


        //                    if (pointCounter>3)
        //                    {
        //                        pointCounter = 0;

        //                        allSelectedPoints = new Point[4];

        //                        matCurrent = OpenCvSharp.Unity.TextureToMat(beginningTexture);

        //                        matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 100, LineTypes.Link8, 5);
        //                    }


        //                    allSelectedPoints[pointCounter] = currPoint;

        //                    textureNew = OpenCvSharp.Unity.MatToTexture(matCurrent);



        //                    inputPlane.GetComponent<Renderer>().material.mainTexture = textureNew;
        //                    inputPlane.transform.localScale = new Vector3((float)textureNew.width / (float)textureNew.height, 1, 1);

        //                    savedTexture = textureNew;



        //                }
        //                if (pointCounter == 3)
        //                {
        //                    Debug.Log("HERE");
        //                    Mat resultContoured = null;
        //                    resultContoured = JustImageContour(matCurrent, allSelectedPoints);

        //                    Texture2D contourTexture = OpenCvSharp.Unity.MatToTexture(resultContoured);

        //                    inputPlane.GetComponent<Renderer>().material.mainTexture = contourTexture;
        //                    inputPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);


        //                    savedTexture = contourTexture;



        //                }


        //                pointCounter++;

        //                //if (pointCounter > 3)
        //                //{
        //                //    //pointCounter = 0;

        //                //    //allSelectedPoints = new Point[4];


        //                //    //inputPlane.GetComponent<Renderer>().material.mainTexture = beginningTexture;
        //                //    //inputPlane.transform.localScale = new Vector3((float)beginningTexture.width / (float)beginningTexture.height, 1, 1);

        //                //   // savedTexture = beginningTexture;
        //                //}



        //                //tex.SetPixel((int)pixelUV.x, (int)pixelUV.y, Color.black);
        //                //tex.Apply();
        //                //}
        //            }


        //        }

        //        if (Input.GetMouseButtonDown(1))
        //        {
        //            pointCounter = 0;
        //            allSelectedPoints = new Point[4];


        //            inputPlane.GetComponent<Renderer>().material.mainTexture = beginningTexture;
        //            inputPlane.transform.localScale = new Vector3((float)beginningTexture.width / (float)beginningTexture.height, 1, 1);

        //        }



        //    }


        //}
    }