We compute homo ourselves

This commit is contained in:
Alexander Munch-Hansen 2019-02-28 13:08:42 +01:00
parent 9c1511dc8b
commit f2578eea0f

View File

@ -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<Renderer>();
// Debugging
//pls = GameObject.Find("pls");
//rend = pls.GetComponent<Renderer>();
pls = GameObject.Find("pls");
rend = pls.GetComponent<Renderer>();
}
@ -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;
}
}