绘制直线
参考代码:
/* 画线 */
Point p1 = (20, 30);
Point p2;
p2.x = 300;
p2.y = 300;
Scalar color = Scalar(0, 0, 255);
line(src, p1, p2, color, 3, LINE_8);
绘制矩形
参考代码:
/* 画矩形 */
Rect rect = Rect(200, 100, 300, 300);
Scalar color = Scalar(255, 0, 0);
rectangle(src, rect, color, 2, LINE_8);
绘制椭圆
参考代码:
/* 绘制椭圆 */
Scalar color = Scalar(0, 255, 0);
ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 4), 90,0, 360, color, 2, LINE_4);
绘制圆
参考代码:
/* 绘制圆 */
Scalar color = Scalar(255, 0, 255);
Point center = Point(src.cols / 2, src.rows / 2);
circle(src, center, 150, color, 1, 8);
绘制文字
参考代码:
Mat src;
src = imread("E:/1.png"); putText(src,"hellowopenCV",Point(10,100),CV_FONT_HERSHEY_COMPLEX,1.0,Scalar(12,23,200),3,8);
imshow("output image",src);
waitKey(0);
绘制随机直线
参考代码:
Mat src;
src = imread("E:/1.png");
RNG rng(12345);
Point p1;
Point p2;
Mat bg = Mat::zeros(src.size(), src.type());
while (1)
{
for (int i = 0; i < 10000; i++)
{
p1.x = rng.uniform(0, src.cols);
p1.y = rng.uniform(0, src.rows);
p2.x = rng.uniform(0, src.cols);
p2.y = rng.uniform(0, src.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
line(bg, p1, p2, color, 1, 8);
imshow("input", bg);
if (waitKey(50) > 0)
{
break;
}
}
}
效果图: