沿着上一篇关于图片裁切反向操作。演示Core.hconcat()水平拼接, Core.vconcat()垂直拼接函数的使用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using OpenCVForUnity;
public class Pano : MonoBehaviour
{
public Texture2D t2d;
public RawImage output;
public AspectRatioFitter fitter;
void Start()
{
// 读取图像
Mat _image0 = Imgcodecs.imread(Application.dataPath + "/0.jpg");
Mat _image1 = Imgcodecs.imread(Application.dataPath + "/1.jpg");
Mat _image2 = Imgcodecs.imread(Application.dataPath + "/2.jpg");
Mat _image3 = Imgcodecs.imread(Application.dataPath + "/3.jpg");
Mat image0 = new Mat();
Mat image1 = new Mat();
Mat image2 = new Mat();
Mat image3 = new Mat();
// 统一尺寸
Imgproc.resize(_image0, image0, new Size(512, 512));
Imgproc.resize(_image1, image1, new Size(512, 512));
Imgproc.resize(_image2, image2, new Size(512, 512));
Imgproc.resize(_image3, image3, new Size(512, 512));
/* 竖向矩阵排列
Mat combine = new Mat(256, 1024, CvType.CV_8UC3);
combine.push_back(image0);
combine.push_back(image1);
combine.push_back(image2);
combine.push_back(image3);
Debug.Log(combine.width() + "*" + combine.height());
*/
Debug.Log(image0.width() + "*" + image0.height());
List<Mat> list1 = new List<Mat>();
list1.Add(image0);
list1.Add(image1);
List<Mat> list2 = new List<Mat>();
list2.Add(image2);
list2.Add(image3);
Mat combine = new Mat(2048, 512, CvType.CV_8UC3);
Mat combine1 = new Mat(1024,512, CvType.CV_8UC3);
Mat combine2 = new Mat(1024, 512, CvType.CV_8UC3);
List<Mat> list3 = new List<Mat>();
list3.Add(combine1);
list3.Add(combine2);
//两两拼接
Core.hconcat(list1, combine1); //横向拼接
Core.hconcat(list2, combine2);
//Core.hconcat(list3, combine);
Core.vconcat(list3, combine); //竖向拼接
float ratio = (float)combine.width() / (float)combine.height();
fitter.aspectRatio = ratio;
t2d = new Texture2D(combine.width(), combine.height());
Debug.Log(t2d.width + "*" + t2d.height);
Utils.matToTexture2D(combine, t2d);
output.texture = t2d;
}
}
输出: