API :Cv2.Line()
参数 | 说明 |
---|---|
img: | 源图像 |
pt1: | 第一个点 |
pt2: | 第二个点 |
color | 线条颜色 |
thickness: | 线条的厚度(粗细) |
lineType: | 线条类型 (枚举值) |
shift | 点坐标中的小数位数。[默认值为0] |
代码:
private static Mat src;
static void Main(string[] args)
{
loadImage();
}
private static void loadImage()
{
using (src = new Mat(@"C:\Users\wang\Desktop\s1.jpg", ImreadModes.AnyColor | ImreadModes.AnyDepth))
{
DrawLine();
using (new Window("SRC Image", WindowMode.AutoSize, src))
{
Cv2.WaitKey(0);
}
}
}
///
/// 绘制线条
///
private static void DrawLine()
{
Point p1 = new Point(20, 30);
Point p2;
p2.X = 300;
p2.Y = 30;
Scalar color = new Scalar(0, 0, 255);
Cv2.Line(src, p1, p2, color, 10, LineTypes.Link8);
}
API:Cv2.Rectangle()
参数 | 说明 |
---|---|
img: | 源图像 |
rect: | 举行对象 |
color | 线条颜色 |
thickness: | 线条的厚度(粗细) |
lineType: | 线条类型 (枚举值) |
shift | 点坐标中的小数位数。[默认值为0] |
代码:
///
/// 绘制矩形
///
private static void DrawRectangle()
{
Rect rect = new Rect(100, 200, 100, 100);
Scalar color = new Scalar(255, 0, 0);
Cv2.Rectangle(src, rect, color, 2, LineTypes.AntiAlias);//LineTypes.AntiAlias:反锯齿效果
}
代码:
///
/// 绘制椭圆
///
private static void DrawEllipse()
{
/*
* 参数:
* 1:背景图片
* 2:中心点 椭圆的长轴和短轴 (如果相等就是圆)
* 3,椭圆的旋转就角度
* 4:起始角度
* 5:结束角度
* 6:颜色
* 7:线条宽度 默认 1
* 8:边界类型 默认 Link8
* 9:中心坐标和坐标轴值的小数位数,默认0
*/
Cv2.Ellipse(src, new Point(src.Cols / 2, src.Rows / 2), new Size(src.Rows / 2, src.Cols / 2),90, 0, 360, new Scalar(0, 255, 0), 3, LineTypes.Link8);
}
///
/// 绘制圆圈
///
private static void DrawCricle()
{
Scalar color = new Scalar(0, 127, 255);
Point conter = new Point(src.Cols / 2, src.Rows / 2);
Cv2.Ellipse(src, conter, new Size(src.Cols / 4, src.Cols / 4), 90, 0, 360, color, 2, LineTypes.Link8);
}
代码:
///
/// 绘制多边形
///
private static void DrawPolygon()
{
List pts1 = new List
{
new Point(400,400),
new Point(400,500),
new Point(500,500),
new Point(500,400),
new Point(400,400)
};
List pts2 = new List
{
new Point(10,10),
new Point(10,150),
new Point(150,150),
new Point(150,10),
new Point(10,10)
};
List> pp = new List>() { pts1, pts2 }; //包含几个矩阵,就绘制几个多边形
Scalar color = new Scalar(0, 100, 33);
Cv2.FillPoly(src, pp, color, LineTypes.Link8, 0, new Point(0, 0));
}
代码:
private static void RandomDrawLine()
{
//Random r = new Random();
RNG rng = new RNG(12345); //生成高斯随机数
Point p1 = new Point();
Point p2 = new Point();
Mat m1 = new Mat(src.Size(), src.Type());
for (int i = 0; i < 5000; i++)
{
p1.X = rng.Uniform(0, src.Cols);//生成正太分布随机数 // r.Next(0, src.Cols);
p1.Y = rng.Uniform(0, src.Cols);// r.Next(0, src.Cols);
p2.X = rng.Uniform(0, src.Cols);// r.Next(0, src.Cols);
p2.Y = rng.Uniform(0, src.Cols); //r.Next(0, src.Cols);
Scalar color = new Scalar(rng.Uniform(0, 255), rng.Uniform(0, 255), rng.Uniform(0, 255));//(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
Cv2.Line(m1, p1, p2, color, 1);
using (new Window("Random Line", WindowMode.AutoSize, m1))
{
if (Cv2.WaitKey(50) > 0) //50毫秒画一条线段
{
break;
}
}
}
}
代码:
static void Main(string[] args)
{
loadImage();
}
private static void loadImage()
{
using (src = new Mat(@"C:\Users\whx\Desktop\opcvImage\s1.jpg", ImreadModes.AnyColor | ImreadModes.AnyDepth))
{
Mat dst = new Mat(src.Size(),src.Type());
src.CopyTo(dst);
////写字
Cv2.PutText(src, "Hello,OpenCvSharp!", new Point(300,200), HersheyFonts.HersheySimplex, 1,Scalar.All(255),2,LineTypes.Link4);
using (new Window("DST Image", WindowMode.Normal, dst))
using (new Window("SRC Image", WindowMode.Normal, src))
{
Cv2.WaitKey(0);
}
}
}