Hello! I really need help with this, I’m not a programmer and I just know Unity basic things. I’m on an internship and I told my boss that I’m an animator and not a programmer but still he is making me work on this.
It’s my third day reading and looking at the examples on Unity and GitHub and I’m trying my best to understand what’s happening.
This is what I have so far. I’m trying to detect and image on my camera and then track it’s position and display a 3d object on top of it.
using UnityEngine;
using OpenCVForUnity;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.UnityUtils;
using UnityEngine.UI;
public class Junto : MonoBehaviour
{
public RawImage canvasCam;
private WebCamTexture webcamTexture;
public GameObject comida;
public Texture2D marker;
private Mat frameactual;
private Mat result;
void Start()
{
webcamTexture = new WebCamTexture();
canvasCam.texture = webcamTexture;
canvasCam.material.mainTexture = webcamTexture;
webcamTexture.Play();
RectTransform rt = canvasCam.GetComponent<RectTransform>();
//esto es lo que usan los elementos del UI para su tamano
rt.sizeDelta = new Vector2(webcamTexture.width, webcamTexture.height);
frameactual = new Mat(webcamTexture.height, webcamTexture.width, CvType.CV_8UC3);
result = new Mat();
comida.SetActive(false);
}
void Update()
{
Debug.Log(marker.isReadable);
if (webcamTexture.isPlaying)
{
Debug.Log("La camara va andando");
Texture2D texture = new Texture2D(webcamTexture.width, webcamTexture.height);
//Frame actual
texture.SetPixels(webcamTexture.GetPixels());
texture.Apply();
//obtener y aplicar cambios
Utils.texture2DToMat(texture, frameactual);
Mat markerMat = new Mat(marker.height, marker.width, CvType.CV_8UC3);
Utils.texture2DToMat(marker, markerMat);
Imgproc.matchTemplate(frameactual, markerMat, result, Imgproc.TM_CCOEFF_NORMED);
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLocation = mmr.maxLoc;
Debug.Log("Tamano del frame" + frameactual.size());
Debug.Log("Tamano del marker" + markerMat.size());
Debug.Log("Valor maximo de la coincidencia: " + mmr.maxVal);
if (mmr.maxVal > 0.8)
{
comida.SetActive(true);
Vector3 position = new Vector3((float)matchLocation.x, (float)matchLocation.y);
comida.transform.position = position;
}
else
{
comida.SetActive(false);
}
}
else
{
Debug.Log("No hay camara");
}
}
}