opencv_绘制形状和文字
#include<opencv2\opencv.hpp>#include<highgui.h>#include<iostream>using namespace std;using namespace cv;Mat bgImage;const char* drawdemo_win = "draw shapes and text demo";voi...
·
以下程序可以在图像上写文字,画圆,画矩形,画线条,写文字。还可以不断画线,非常美。
#include<opencv2\opencv.hpp>
#include<highgui.h>
#include<iostream>
using namespace std;
using namespace cv;
Mat bgImage;
const char* drawdemo_win = "draw shapes and text demo";
void MyLines();
void MyRectangle();
void MyEllipse();
void MyCircle();
void RandomLineDemo();
int main(int argc, char** argv)
{
// read image
bgImage = imread("C:/Users/qxq/Pictures/image/1.jpg");
if (bgImage.empty())
{
printf("Could not load image...");
return -1;
}
//MyLines();//画线条
//MyRectangle();//画长方形
// MyEllipse();//画椭圆
//MyCircle();//画圆
写文字
//putText(bgImage, "Hello opencv", Point(100, 100), CV_FONT_BLACK, 2.0, Scalar(12, 20, 200), 3, 8);
//不断画线
RandomLineDemo();
/*namedWindow(drawdemo_win, CV_WINDOW_AUTOSIZE);
imshow(drawdemo_win, bgImage);*/
waitKey(0);
return 0;
}
void MyLines()
{
Point p1 = Point(10, 20);
Point p2;
p2.x = 300;
p2.y = 300;
Scalar color = Scalar(0, 0, 255);
cv::line(bgImage, p1, p2, color, 4, LINE_8);//那个4是调整线条的粗细的
}
void MyRectangle()
{
Rect rect = Rect(200,100,300,300);
Scalar color = cvScalar(255, 0, 0);
rectangle(bgImage, rect, color, 2, LINE_8);
}
void MyEllipse()
{
Scalar color = Scalar(0, 255, 0);
ellipse(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), Size(bgImage.cols / 4, bgImage.rows / 8), 90, 0, 360, color, LINE_8);
}
void MyCircle()
{
Scalar color = Scalar(0, 255, 255);
Point center = Point(bgImage.cols / 2, bgImage.rows / 2);
circle(bgImage, center, 150, color, 2, 8);
}
void RandomLineDemo()//画随机的线条
{
RNG rng(12345);//生成随机数
Point pt1;
Point pt2;
Mat bg = Mat::zeros(bgImage.size(), bgImage.type());
namedWindow("Random Line Demo", CV_WINDOW_AUTOSIZE);
for (int i = 0; i < 100000; i++)
{
pt1.x = rng.uniform(0, bgImage.cols);
pt2.x = rng.uniform(0, bgImage.cols);
pt1.y = rng.uniform(0, bgImage.rows);
pt2.y = rng.uniform(0, bgImage.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
if (waitKey(50) > 0)
{
break;
}
line(bg, pt1, pt2, color, 1, 8);
imshow("Random Line Demo", bg);
}
}
2、效果展示
写文字
不断画线(是一个动态的过程)
更多推荐
已为社区贡献1条内容
所有评论(0)