OpenCVForUnity(七)基本绘图介绍


前言

今天我们来介绍一下,如何使用OpenCVForUnity画出各种基础图形.
本教程将为您介绍如何使用OpenCVForUnity库中的函数来进行基本的图像绘制操作。
您将学习如何使用point在图像中定义2D点,并了解Scalar的用途及其实用性。
您将学习如何使用Line、ellipse、rectangle和circle等OpenCVForUnity函数来画线、椭圆、矩形和圆圈。
最后,您将使用fillPoly函数来绘制填充的多边形。 期望通过本教程,您将具备一定的OpenCVForUnity基本图形绘制的基础。

先来看下今天要学习画出的内容

在这里插入图片描述


Point方法(点)

方法Point(x,y)是一个2D的点,主要用来记录位置。


Scalar方法(元素)

本教程中使用Scalar类型来表示一个包含4个元素的向量。其中,常用于OpenCV中传递像素值。
在本教程中,我们将使用Scalar类型来表示BGR颜色值,其中包含3个参数。
如果不需要使用最后一个参数,则可以省略。例如,如果需要提供一个颜色参数,我们可以这样表示:

Scalar color=new Scalar(0, 0, 255);

这里定义了一个颜色,与一般的设定不同,这里是BGR的颜色模式.


circle方法(画圆)

使用Imgproc方法可以在图像上画圈
Imgproc.circle(Mat容器, 位置 , 圈半径 ,颜色,线条厚度,线类型,小数位);
例下面的例子:

Imgproc.circle(inputMat, new Point(128, 128), 30, new Scalar(256, 0, 0), 5, 8, 0);

下面是效果图,如上代码我们画了一个红色的正圆。

在这里插入图片描述


ellipse方法(画椭圆)

使用ellipse可以画椭圆
ellipse(Mat容器,位置,大小,旋转角度,开始角度,结束角度,颜色,线条厚度,线类型,小数位)
例下面的例子:

Imgproc.ellipse(inputMat, new Point(256, 128), new Size(30, 50), 50, 0, 360, new Scalar(0, 256, 0), 5, 8, 0);

下面是效果图,如上代码我们画了一个绿色的椭圆。

在这里插入图片描述


line方法(画线)

使用line可以画线条
line(amt容器,起始位置 ,结束位置 ,颜色,线条厚度,线类型,小数位)
下面的例子:

Imgproc.line(inputMat, new Point(384, 128), new Point(512, 128), new Scalar(0, 256, 256), 5, 8, 0);

下面是效果图,如上代码我们画了一个蓝色的线。

在这里插入图片描述


rectangle方法(画矩形)

使用rectangle可以画矩形
rectangle(Mat容器,左上角点,右下角点,颜色,线类型,小数位)
例下面的例子:

Imgproc.rectangle(inputMat, new Point(400, 100), new Point(450, 150), new Scalar(256, 128, 0), 5, 8, 0);

下面是效果图,如上代码我们画了一个橙色的矩形。

在这里插入图片描述


fillPoly方法(画多边形)

使用fillPoly可以自定义多个多边形
Imgproc.fillPoly(Mat容器,点组, 颜色,线类型,小数位)
下面的例子:
定义了两个三角形点

Point[] ppt1 = new Point[3] { new Point(100, 250), new Point(300, 250), new Point(200, 350) };
Point[] ppt2 = new Point[3] { new Point(100, 400), new Point(300, 400), new Point(200, 500) };

定义点组将上面的点传进去

List<MatOfPoint> pts = new List<MatOfPoint>() { };
pts.Add(new MatOfPoint(ppt1));
pts.Add(new MatOfPoint(ppt2));

开始画多边形

Imgproc.fillPoly(inputMat, pts, new Scalar(255, 128, 125), 8, 0);

下面是效果图,为了展示此方法能同时绘制多组图形的特性,如上代码我们画了两个粉色的三角形。

在这里插入图片描述


完整工程:

然后下面是工程完整的内容:

//新建立一个Mat容器
Mat inputMat = new Mat(512, 512, CvType.CV_8UC4);
//画圈
Imgproc.circle(inputMat, new Point(100, 100), 30, new Scalar(256, 0, 0), 5, 8, 0);
//画椭圆
Imgproc.ellipse(inputMat, new Point(200, 100), new Size(30, 50), 50, 0, 360, new Scalar(0, 256, 0), 5, 8, 0);
//画线
Imgproc.line(inputMat, new Point(300, 100), new Point(350, 100), new Scalar(0, 256, 256), 5, 8, 0);
//画方
Imgproc.rectangle(inputMat, new Point(400, 100), new Point(450, 150), new Scalar(256, 128, 0), 5, 8, 0);
//画填充的多边形(这里以三角形做例子)
Point[] ppt1 = new Point[3] { new Point(100, 250), new Point(300, 250), new Point(200, 350) };
Point[] ppt2 = new Point[3] { new Point(100, 400), new Point(300, 400), new Point(200, 500) };
List<MatOfPoint> pts = new List<MatOfPoint>() { };
pts.Add(new MatOfPoint(ppt1));
pts.Add(new MatOfPoint(ppt2));
Imgproc.fillPoly(inputMat, pts, new Scalar(255, 128, 125), 8, 0);
// 将Mat对象转换回Unity Texture2D对象
Texture2D outputTexture = new Texture2D(inputMat.cols(), inputMat.rows(), TextureFormat.RGB24, false);
Utils.matToTexture2D(inputMat, outputTexture);
// 在Unity中显示
objA.GetComponent<Renderer>().material.mainTexture = outputTexture;

最后效果
在这里插入图片描述

结语

到这里在Unity中使用OpenCVForUnity绘制图形的内容就介绍完毕了,童鞋们会用它来绘制什么图形呢?赶紧动手试试吧!

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
发出的红包

打赏作者

小盖子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值