OpenCV入门————文字处理

程序说明:

RNG(uint64 state):产生随机数,state:起始随机数据的64位值Size getTextSize(const string& text, int fontFace, double fontScale, int thickness, int baseLine):计算文字符串的长和宽*
text:输入的文字符串
fontFace:字体;此字体将用于putText()函数
fontScale:字体大小;与putText相同
thickness:字体笔画粗细;与putText相同
baseLine:文字底线的Y坐标
此函数用于算出文字用参数的字体,与笔画粗细所涵盖的方形区块

putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness, int lineType, bool bottomLeftOrigin):在图像中绘制文字字符串
img:要显示文字的图像
text:要显示的文字
org:文字在图像的左下角
fontFace:字体(详细字体对照可百度);字体可以与FONT_ITALIC结合
fontScale:字体大小
color: 文字的颜色
thickness:文字笔画的粗细度
lineType:笔画的形态,此参数与划线的函数相同
bottomLeftOrigin:此参数可选,若有此参数(true值),字符串图的起点在左下

目前OpenCV还不支持中文

#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
#include<stdio.h>

using namespace cv;

//声明全局变量
const int NUMBER = 100;
const int DELAY = 5;

const int window_width = 900;
const int window_height = 600;
int x_1 = -window_width / 2;
int x_2 = window_width * 3 / 2;
int y_1 = -window_width / 2;
int y_2 = -window_width * 3 / 2;

//声明函数
static Scalar randomColor(RNG& rng);											//产生随机颜色
int Drawing_Random_line(Mat image, char* window_name, RNG rng);					//绘制直线
int Drawing_Random_Rectangles(Mat image, char* window_name, RNG rng);			//绘制长方形
int Drawing_Random_Ellipses(Mat image, char* window_name, RNG rng);				//绘制椭圆
int Drawing_Random_Polylines(Mat image, char* window_name, RNG rng);			//绘制多边形(plotlines)
int Drawing_Random_Filled_Polygons(Mat image, char* window_name, RNG rng);		//绘制多边形
int Drawing_Random_Circles(Mat image, char* window_name, RNG rng);				//绘制圆
int Displaying_Random_Text(Mat image, char* window_name, RNG rng);				//绘制文字
int Displaying_Big_End(Mat image, char* window_name, RNG rng);					//文字测试,是否可以显示中文

int main(void)
{
	int c;

	//窗口名称
	char window_name[] = "Drawing_2 Tutorial";

	//建立随机对象,并设置起始值
	RNG rng(0xFFFFFFFF);

	//声明内容为0的矩阵
	Mat image = Mat::zeros(window_height, window_width, CV_8UC3);

	//显示窗口
	imshow(window_name, image);

	//绘制直线
	c = Drawing_Random_line(image, window_name, rng);
	if (c != 0)
		return 0;

	//绘制长方形
	c = Drawing_Random_Rectangles(image, window_name, rng);
	if (c != 0)
		return 0;

	//绘制椭圆
	c = Drawing_Random_Ellipses(image, window_name, rng);
	if (c != 0)
		return 0;

	//绘制连接线(polylines)
	c = Drawing_Random_Polylines(image, window_name, rng);
	if (c != 0)
		return 0;
	//绘制多边形
	c = Drawing_Random_Filled_Polygons(image, window_name, rng);
	if (c != 0)
		return 0;

	//绘制圆
	c = Drawing_Random_Circles(image, window_name, rng);
	if (c != 0)
		return 0;

	//绘制文字
	c = Displaying_Random_Text(image, window_name, rng);
	if (c != 0)
		return 0;

	//结局
	c = Displaying_Big_End(image, window_name, rng);

	waitKey(0);
	return 0;


}


//定义函数

//产生随机颜色
static Scalar randomColor(RNG &rng)
{
	int icolor = (unsigned) rng;
	return Scalar(icolor & 255, (icolor >> 8) & 255, (icolor >> 16) & 255);
}

//绘制直线
int Drawing_Random_line(Mat image, char* window_name, RNG rng)
{
	Point pt1, pt2;

	for (int i = 0; i < NUMBER; i++)
	{
		pt1.x = rng.uniform(x_1, x_2);
		pt1.y = rng.uniform(y_1, y_2);
		pt2.x = rng.uniform(x_1, x_2);
		pt2.y = rng.uniform(y_1, y_2);

		line(image, pt1, pt2, randomColor(rng), rng.uniform(1,10), 8);
		imshow(window_name, image);
		if (waitKey(DELAY) >= 0)
			return -1;
	}
	return 0;
} 

//绘制长方形
int Drawing_Random_Rectangles(Mat image, char* window_name, RNG rng)
{
	Point pt1, pt2;
	int lineType = 8;
	int thickness = rng.uniform(-3, 10);

	for (int i = 0; i < NUMBER; i++)
	{
		pt1.x = rng.uniform(x_1, x_2);
		pt1.y = rng.uniform(y_1, y_2);
		pt2.x = rng.uniform(x_1, x_2);
		pt2.y = rng.uniform(y_1, y_2);

		rectangle(image, pt1, pt2, randomColor(rng), MAX(thickness, -1), lineType);

		imshow(window_name, image);
		if (waitKey(DELAY) >= 0)
			return -1;
	}
	return 0;
}

int Drawing_Random_Ellipses(Mat image, char* window_name, RNG rng)
{
	int lineType = 8;
	for(int i = 0; i<NUMBER; i++)
	{
		Point center;
		center.x = rng.uniform(x_1, x_2);
		center.y = rng.uniform(y_1, y_2);

		Size axes;
		axes.width = rng.uniform(0, 200);
		axes.height = rng.uniform(0,200);

		double angle = rng.uniform(0, 180);
		//椭圆绘制函数
		ellipse(image, center, axes, angle, angle - 100, angle + 200, 
				randomColor(rng), rng.uniform(-1, 9), lineType);
		
		imshow(window_name, image);
		if (waitKey(DELAY) >= 0)
			return -1;
	}
	return 0;
}

//绘制多边形(polylines)
int Drawing_Random_Polylines(Mat image, char* window_name, RNG rng)
{
	int lineType = 8;

	for (int i = 0; i < NUMBER; i++)
	{
		Point pt[2][3];
		pt[0][0].x = rng.uniform(x_1, x_2);
		pt[0][0].y = rng.uniform(y_1, y_2);
		pt[0][1].x = rng.uniform(x_1, x_2);
		pt[0][1].y = rng.uniform(y_1, y_2);
		pt[0][2].x = rng.uniform(x_1, x_2);
		pt[0][2].y = rng.uniform(y_1, y_2);
		pt[1][0].x = rng.uniform(x_1, x_2);
		pt[1][0].y = rng.uniform(y_1, y_2);
		pt[1][1].x = rng.uniform(x_1, x_2);
		pt[1][1].y = rng.uniform(y_1, y_2);
		pt[1][2].x = rng.uniform(x_1, x_2);
		pt[1][2].y = rng.uniform(y_1, y_2);

		const Point* ppt[2] = {pt[0], pt[1]};
		int npt[] = { 3,3 };

		polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1, 10), lineType);
		
		imshow(window_name, image);
		if (waitKey(DELAY) >= 0)
			return -1;
	}
	return 0;
}

//绘制多边形
int Drawing_Random_Filled_Polygons(Mat image, char* window_name, RNG rng)
{
	int lineType = 8;

	for (int i = 0; i < NUMBER; i++)
	{
		Point pt[2][3];
		pt[0][0].x = rng.uniform(x_1, x_2);
		pt[0][0].y = rng.uniform(y_1, y_2);
		pt[0][1].x = rng.uniform(x_1, x_2);
		pt[0][1].y = rng.uniform(y_1, y_2);
		pt[0][2].x = rng.uniform(x_1, x_2);
		pt[0][2].y = rng.uniform(y_1, y_2);
		pt[1][0].x = rng.uniform(x_1, x_2);
		pt[1][0].y = rng.uniform(y_1, y_2);
		pt[1][1].x = rng.uniform(x_1, x_2);
		pt[1][1].y = rng.uniform(y_1, y_2);
		pt[1][2].x = rng.uniform(x_1, x_2);
		pt[1][2].y = rng.uniform(y_1, y_2);

		const Point* ppt[2] = { pt[0], pt[1] };
		int npt[] = { 3, 3 };
		fillPoly(image, ppt, npt, 2, randomColor(rng), lineType);
		imshow(window_name, image);
		if (waitKey(DELAY) >= 0)
			return -1;
	}
	return 0;
}

//绘制圆
int Drawing_Random_Circles(Mat image, char* window_name, RNG rng)
{
	int lineType = 8;
	for (int i = 0; i < NUMBER; i++)
	{
		Point center;
		center.x = rng.uniform(x_1,x_2);
		center.y = rng.uniform(y_1,y_2);
		circle(image, center, rng.uniform(0,300), randomColor(rng), rng.uniform(-1,9), lineType);

		imshow(window_name, image);
		if (waitKey(DELAY) >= 0)
			return -1;
	}
	return 0;
}

//绘制文字
int Displaying_Random_Text(Mat image, char* window_name, RNG rng)
{
	int lineType = 8;
	for (int i = 1; i < NUMBER; i++)
	{
		Point org;
		org.x = rng.uniform(x_1, x_2);
		org.y = rng.uniform(y_1, y_2);

		putText(image, "Texting text rendering", org, rng.uniform(0, 8), rng.uniform(0, 100)*0.05 + 0.1,
			randomColor(rng), rng.uniform(1, 10), lineType);
		
		imshow(window_name, image);
		if (waitKey(DELAY) >= 0)
			return -1;
	}
	return 0;
}

//测试是否可以显示中文
int Displaying_Big_End(Mat image, char* window_name, RNG rng)
{
	char end_text[] = "我爱 OpenCV!";

	//取得文字大小
	Size textsize = getTextSize(end_text, FONT_HERSHEY_COMPLEX, 3, 5,0);
	Point org((window_width - textsize.width) / 2, (window_height - textsize.height) / 2);
	
	int lineType = 8;
	
	Mat image2;
	
	for (int i = 0; i < 255; i++)
	{
		//原图扣除所有颜色、饱和度(saturate)的处理
		image2 = image - Scalar::all(i);

		putText(image2, end_text, org, FONT_HERSHEY_COMPLEX + FONT_ITALIC, 3, Scalar(i, i, 255), 4, lineType);

		imshow(window_name, image2);
		if (waitKey(DELAY) >= 0)
			return -1;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值