我正在尝试从Unity中的两个摄像机拼接两个实时视频。 我曾经一次拼接两个实时视频,但是结果图像拼接速度太慢,相机的帧率也太低,大约为1FPS。
我正在使用来自opencvsharp的Stitcher类以及诸如ComposePanorama和EstimateTransform之类的函数来实现我的工作。 这是我的代码。
void Update()
{
IEnumerable<Mat> images = GenerateImages();
stitcher = Stitcher.Create(false);
//Some setting which may faster
stitcher.RegistrationResol = 0.2;
stitcher.WaveCorrection = false;
Stitcher.Status status = stitcher.EstimateTransform(images);
if (status != Stitcher.Status.OK)
{
Debug.Log("Can't stitch images, error code = " + (int)status);
return;
}
Stitcher.Status status1 = stitcher.ComposePanorama(images, panoMat);
if (status1 != Stitcher.Status.OK)
{
Debug.Log("Can't stitch images, error code = " + (int)status1);
return;
}
stitcher.Dispose();
foreach(Mat mat in images) {
mat.Dispose();
}
_Texture1 = MatToTexture2D(panoMat);
output.texture = _Texture1;
output.GetComponent<AspectRatioFitter>().aspectRatio = (float)_Texture1.width / (float)_Texture1.height;
}
IEnumerable<Mat> GenerateImages()
{
//Since I am using two rendertexture to connect my live camera
//I have to convert the video to texture2d
CvConvert.RenderTextureToTexture2D(RT_AVProLiveCamera[0], ref tex2D_1);
CvConvert.RenderTextureToTexture2D(RT_AVProLiveCamera[1], ref tex2D_2);
//Simple method to convert texture2d to mat
first_mat = Texture2DToMat(tex2D_1);
second_mat = Texture2DToMat(tex2D_2);
yield return first_mat;
yield return second_mat;
}
Texture2D MatToTexture2D(Mat mat)
{
t2d = new Texture2D(mat.Width, mat.Height);
t2d.LoadImage(mat.ToBytes());
t2d.Apply();
return t2d;
}
Mat Texture2DToMat(Texture2D tex2D)
{
//mat with fixed size has been create when start run the program
mat = Mat.FromImageData(tex2D.EncodeToPNG());
return mat;
}