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";
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、效果展示

写文字

不断画线(是一个动态的过程)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值