读取相关
读取一张图片并转化成Mat
Texture2D imgTexture = Resources.Load("dst2") as Texture2D;
Mat ImgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC1);
Debug.Log("imgTexture" + imgTexture.height + "-" + imgTexture.width);
Utils.texture2DToMat(imgTexture, ImgMat);
Mat转换为Texture
Texture2D texture = new Texture2D(resultImg.cols(), resultImg.rows(), TextureFormat.RGBA32, false);
Utils.matToTexture2D(resultImg, texture);
读取一个视频
public void InitVideo()
{
if (isPlaying)
return;
capture = new VideoCapture();
capture.open(VIDEO_FILENAME);
if (!capture.isOpened())
{
Debug.LogError("capture.isOpened() is false. ");
capture.release();
return;
}
Debug.Log("CAP_PROP_FORMAT: " + capture.get(Videoio.CAP_PROP_FORMAT));
Debug.Log("CAP_PROP_POS_MSEC: " + capture.get(Videoio.CAP_PROP_POS_MSEC));
Debug.Log("CAP_PROP_POS_FRAMES: " + capture.get(Videoio.CAP_PROP_POS_FRAMES));
Debug.Log("CAP_PROP_POS_AVI_RATIO: " + capture.get(Videoio.CAP_PROP_POS_AVI_RATIO));
Debug.Log("CAP_PROP_FRAME_COUNT: " + capture.get(Videoio.CAP_PROP_FRAME_COUNT));
Debug.Log("CAP_PROP_FPS: " + capture.get(Videoio.CAP_PROP_FPS));
Debug.Log("CAP_PROP_FRAME_WIDTH: " + capture.get(Videoio.CAP_PROP_FRAME_WIDTH));
Debug.Log("CAP_PROP_FRAME_HEIGHT: " + capture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
double ext = capture.get(Videoio.CAP_PROP_FOURCC);
Debug.Log("CAP_PROP_FOURCC: " + (char)((int)ext & 0XFF) + (char)(((int)ext & 0XFF00) >> 8) + (char)(((int)ext & 0XFF0000) >> 16) + (char)(((int)ext & 0XFF000000) >> 24));
videoMat = new Mat();
capture.grab();
capture.retrieve(videoMat, 0);
isPlaying = true;
}
循环播放视频
void Update ()
{
if (isPlaying)
{
string videosavePath = Application.persistentDataPath + "/video.jpg";
if (capture.get(Videoio.CAP_PROP_POS_FRAMES) >= capture.get(Videoio.CAP_PROP_FRAME_COUNT))
capture.set(Videoio.CAP_PROP_POS_FRAMES, 0);
if (capture.grab())
{
capture.retrieve(videoMat, 0);
Imgproc.rectangle(videoMat, new Point(0, 0), new Point(videoMat.cols(), videoMat.rows()), new Scalar(0, 0, 255), 3);
Utils.fastMatToTexture2D(videoMat, videoMat);
}
}
}
存储相关
存储一个Mat
string videosavePath = Application.persistentDataPath + "/video.jpg";
Imgcodecs.imwrite(videosavePath, videoMat);
Mat处理相关
新建Mat
grayMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC3);
画线
Imgproc.line(bgMat, new Point(0, 0 + i), new Point(bgMat.cols(), -bgMat.cols() + i), new Scalar(0), 1);
提取边界
Imgproc.Canny(lineMat, lineMat, 20, 120);
翻转
Core.flip(lineMat, lineMat, 0);
旋转
Texture2D tempTexture = Resources.Load("MainT2") as Texture2D;
Mat img2Mat = new Mat(tempTexture.height, tempTexture.width, CvType.CV_8UC3);
Utils.texture2DToMat(tempTexture, img2Mat);
Debug.Log("img2Mat.ToString() " + img2Mat.ToString());
float angle = UnityEngine.Random.Range(0, 360), scale = 1.0f;
Point center = new Point(img2Mat.cols() * 0.5f, img2Mat.rows() * 0.5f);
Mat affine_matrix = Imgproc.getRotationMatrix2D(center, angle, scale);
Imgproc.warpAffine(img2Mat, img2Mat, affine_matrix, img2Mat.size());
拿到Mat的像素
for (int i = 0; i < lineMat.cols(); i++)
{
for (int j = 0; j < lineMat.rows(); j++)
{
Debug.Log(lineMat.get(j, i)[0]);
}
}
高斯模糊
Imgproc.GaussianBlur (grayMat, lineMat, new Size (3, 3), 0);
反色
Core.bitwise_not(lineMat, lineMat);
遮罩
lineMat.copyTo(lineMat, maskMat);