我试图在Unity中使用EmguCV。在“更新”功能中,我只是从VideoCapture抓取了一个(1920x1080)帧并将其应用于精灵。目前,由于某种原因,此方法会占用大量内存,并且场景非常漫长。
void Update()
{
_frame = cap.QueryFrame();
Image<Bgr, byte> currImage = _frame.ToImage<Bgr, byte>();
//CvInvoke.Imshow("test", _frame);
Destroy(cameraTex);
cameraTex = TextureConvert.ImageToTexture2D<Bgr, byte>(currImage, FlipType.Horizontal);
RenderTexture(cameraTex);
ResizeTexture(cameraTex);
}
private void RenderTexture(Texture2D texture)
{
Image image = this.GetComponent<Image>();
Sprite.DestroyImmediate(image.sprite);
image.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
private void ResizeTexture(Texture2D texture)
{
Image image = this.GetComponent<Image>();
var transform = image.rectTransform;
transform.sizeDelta = new Vector2(texture.width, texture.height);
//transform.position = new Vector3(-texture.width / 2, -texture.height / 2);
transform.anchoredPosition = new Vector2(0, 0);
}