private void trackFilteredObject(ObjectTracker ot, Mat threshold)
{
Mat temp = new Mat();
threshold.copyTo(temp);
List <MatOfPoint> contours = new List <MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.findContours(temp, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);
if (hierarchy.rows() > 0)
{
for (int index = 0; index >= 0; index = (int)hierarchy.get(0, index)[0])
{
Moments moment = Imgproc.moments(contours[index]);
double area = moment.m00;
if (area > 10 * 10)
{
int x = (int)(moment.get_m10() / area);
int y = (int)(moment.get_m01() / area);
Vector2 point = new Vector2(x, y);
Vector3 dirRay = LocatableCameraUtils.PixelCoordToWorldCoord(_cameraToWorldMatrix, _projectionMatrix, _resolution, point);
Application.InvokeOnAppThread(() => {
ot.Sphere.transform.position = Camera.main.transform.position + new Vector3(0, ot.offset, 0);
SphereCollider collider = ot.Sphere.GetComponent <SphereCollider>();
// We inverse the ray source and dir to make the sphere collider work
Vector3 newPosRay = Camera.main.transform.position + dirRay * (collider.radius * 2);
Ray ray = new Ray(newPosRay, -dirRay);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, collider.radius * 3))
{
Vector3 pos = hit.point;
ot.gameObject.transform.position = pos;
}
}, false);
}
}
}
}