diff --git a/AR-2b/Assets/Homo_script.cs b/AR-2b/Assets/Homo_script.cs index 8e58c6d..c3a8a86 100644 --- a/AR-2b/Assets/Homo_script.cs +++ b/AR-2b/Assets/Homo_script.cs @@ -5,6 +5,7 @@ using OpenCVForUnity.UnityUtils; using OpenCVForUnity.UtilsModule; using UnityEngine; using OpenCVForUnity.Calib3dModule; +using System.Linq; using Vuforia; public class Homo_script : MonoBehaviour @@ -38,6 +39,8 @@ public class Homo_script : MonoBehaviour private GameObject pls; + private bool manualComputation = true; + void Start() { @@ -55,8 +58,8 @@ public class Homo_script : MonoBehaviour rend = pls.GetComponent(); // Debugging - //pls = GameObject.Find("pls"); - //rend = pls.GetComponent(); + pls = GameObject.Find("pls"); + rend = pls.GetComponent(); } @@ -127,13 +130,27 @@ public class Homo_script : MonoBehaviour Point imgPnt4 = new Point(imagePoints.get(3, 0)); Mat camImgCopy = camImageMat.clone(); Mat outputMat = camImgCopy.clone(); - Imgproc.circle(camImageMat, imgPnt1, 5, new Scalar(255, 0, 0, 255)); - Imgproc.circle(camImageMat, imgPnt2, 5, new Scalar(0, 255, 0, 255)); - Imgproc.circle(camImageMat, imgPnt3, 5, new Scalar(0, 0, 255, 255)); - Imgproc.circle(camImageMat, imgPnt4, 5, new Scalar(255, 255, 0, 255)); - Mat homo = Calib3d.findHomography(imagePoints, dstPoints); + if (false) + { + Imgproc.circle(camImageMat, imgPnt1, 5, new Scalar(255, 0, 0, 255)); + Imgproc.circle(camImageMat, imgPnt2, 5, new Scalar(0, 255, 0, 255)); + Imgproc.circle(camImageMat, imgPnt3, 5, new Scalar(0, 0, 255, 255)); + Imgproc.circle(camImageMat, imgPnt4, 5, new Scalar(255, 255, 0, 255)); + } + + + + Mat homo = new Mat(); + if (manualComputation) + { + homo = ComputeHomo(imagePoints, dstPoints); + } + else + { + homo = Calib3d.findHomography(imagePoints, dstPoints); + } Imgproc.warpPerspective(camImgCopy, outputMat, homo, new Size(outputMat.width(), outputMat.height())); @@ -155,5 +172,40 @@ public class Homo_script : MonoBehaviour } } + Mat ComputeHomo(MatOfPoint2f imgPoints, MatOfPoint2f destPoints) + { + + + Mat H = new Mat(8, 1, CvType.CV_32FC1); + Mat A = new Mat(8, 8, CvType.CV_32FC1); + Mat b = new Mat(8, 1, CvType.CV_32FC1); + + + for (int i = 0; i < 4; i++) + { + var u = destPoints.get(i, 0)[0]; + var v = destPoints.get(i, 0)[1]; + + b.put(i * 2, 0, u); + b.put((i * 2)+1, 0, v); + + var x = imgPoints.get(i, 0)[0]; + var y = imgPoints.get(i, 0)[1]; + + A.put(i*2, 0, x, y, 1, 0, 0, 0, -u*x, -u*y); + A.put((i * 2)+1, 0, 0, 0, 0, x, y, 1, -v*x, -v*y); + + } + + Core.solve(A, b, H); + + Mat ShitsReal = new Mat(3, 3, CvType.CV_32FC1); + ShitsReal.put(0, 0, H.get(0,0)[0], H.get(1,0)[0], H.get(2,0)[0]); + ShitsReal.put(1, 0, H.get(3,0)[0], H.get(4,0)[0], H.get(5,0)[0]); + ShitsReal.put(2, 0, H.get(6,0)[0], H.get(7,0)[0], 1); + + return ShitsReal; + } + }