OpenCVForUnity 笔记

10 篇文章 3 订阅

读取相关

读取一张图片并转化成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
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";
                //Loop play
                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);
Works with Unity Cloud Build iOS & Android support Windows10 UWP support WebGL support Win & Mac & Linux Standalone support Preview support in the Editor OpenCV for Unity is an Assets Plugin for using OpenCV 3.4.2 from within Unity. Official Site | ExampleCode | Android Demo WebGL Demo | Tutorial & Demo Video | Forum | API Reference | Support Modules Features: - Since this package is a clone of OpenCV Java, you are able to use the same API as OpenCV Java 3.4.2 (link). - You can image processing in real-time by using the WebCamTexture capabilities of Unity. (real-time face detection works smoothly on iPhone 5) - Provides a method to interconversion of Unity's Texture2D and OpenCV's Mat. - IDisposable is implemented in many classes.You can manage the resources with the “using” statement. - Examples of integration with other publisher assets are available.(e.g. PlayMaker, NatCam, NatCorder) ExampleCode using OpenCV for Unity is available. MarkerBased AR Example MarkerLess AR Example FaceTracker Example FaceSwapper Example FaceMask Example RealTime FaceRecognition Example GoogleVR with OpenCV for Unity Example Kinect with OpenCV for Unity Example AVPro with OpenCV for Unity Example HoloLens with OpenCV for Unity Example PlayMakerActions for OpenCVforUnity NatCam with OpenCVForUnity Example NatCorder with OpenCVForUnity Example OpenCV for Unity uses OpenCV under 3-clause BSD License; see Third-Party Notices.txt file in package for details. System Requirements Build Win Standalone & Preview Editor : Windows 7 or later Build Mac Standalone & Preview Editor : OSX 10.9 or later
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值