OpenCVForUnity (seven) basic drawing introduction


foreword

Today we will introduce how to use OpenCVForUnity to draw various basic graphics.
This tutorial will introduce you how to use the functions in the OpenCVForUnity library to perform basic image drawing operations.
You'll learn how to use point to define 2D points in an image, and understand what Scalar is for and how useful it is.
You will learn how to draw lines, ellipses, rectangles and circles using OpenCVForUnity functions such as Line, ellipse, rectangle and circle.
Finally, you'll use the fillPoly function to draw the filled polygon. It is expected that through this tutorial, you will have a certain foundation for drawing basic graphics in OpenCVForUnity.

Let's take a look at what we will learn to draw today

insert image description here


Point method (point)

The method Point(x,y) is a 2D point, mainly used to record the position.


Scalar method (element)

This tutorial uses the Scalar type to represent a vector of 4 elements. Among them, it is often used to transfer pixel values ​​in OpenCV.
In this tutorial, we will use the Scalar type to represent the BGR color value, which contains 3 parameters.
The last parameter can be omitted if it is not required. For example, if we need to provide a color parameter, we can express it like this:

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

A color is defined here, which is different from the general setting, here is the color mode of BGR.


circle method (drawing a circle)

Use the Imgproc method to draw a circle on the image
Imgproc.circle(Mat container, position, circle radius, color, line thickness, line type, decimal place);
Example The following example:

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

The following is the effect diagram, as above code we draw a red perfect circle.

insert image description here


ellipse method (drawing an ellipse)

Use ellipse to draw an ellipse
ellipse (Mat container, position, size, rotation angle, start angle, end angle, color, line thickness, line type, decimal place)
example The following example:

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

The following is the effect diagram, as above code we draw a green ellipse.

insert image description here


line method (drawing a line)

Use line to draw lines
line (amt container, start position, end position, color, line thickness, line type, decimal places) the
following example:

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

The following is the effect diagram, as above code we draw a blue line.

insert image description here


The rectangle method (drawing a rectangle)

Use rectangle to draw a rectangle
rectangle (Mat container, upper left corner point, lower right corner point, color, line type, decimal place)
Example below:

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

The following is the rendering, as above code we draw an orange rectangle.

insert image description here


fillPoly method (drawing polygons)

Use fillPoly to customize multiple polygons
Imgproc.fillPoly(Mat container, point group, color, line type, decimal place)
The following example:
two triangle points are defined

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) };

Define a point group and pass the above points into it

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

start drawing polygons

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

The following is the effect diagram. In order to show the characteristics of this method that can draw multiple groups of graphics at the same time, we drew two pink triangles in the above code.

insert image description here


Complete project:

Then the following is the complete content of the project:

//新建立一个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;

final effect
insert image description here

epilogue

So far, the content of using OpenCVForUnity to draw graphics in Unity has been introduced. What graphics will children's shoes use to draw? Hurry up and try it out!

Guess you like

Origin blog.csdn.net/ww1351646544/article/details/131656625