图像写入文字
putText( Mat& img, const string& text, Point org, int fontFace,double fontScale, Scalar color, int thickness=1, int lineType=8 );
参数解释:
putText(
Mat& img, // 待绘制的图像
const string& text, // 待绘制的文字
Point origin, // 文本框的左下角
int fontFace, // 字体 (如FONT_HERSHEY_PLAIN)
double fontScale, // 尺寸因子,值越大文字越大
Scalar color, // 线条的颜色(RGB)
int thickness = 1, // 线条宽度
int lineType = 8, // 线型(4邻域或8邻域,默认8邻域)
bool bottomLeftOrigin = false // true='origin at lower left'
);
举个栗子:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\types_c.h>
using namespace std;
using namespace cv;
int main()
{
Mat imageSource = imread("F:\\legend.jpg");
Mat imageGrey;
if (imageSource.empty())
{
cout << "could not load image..." << endl;
return -1;
}
namedWindow("显示原图", WND_PROP_AUTOSIZE);//As literal meaning
namedWindow("显示效果图", WND_PROP_AUTOSIZE);//As literal meaning
imshow("显示原图", imageSource);
putText(imageSource, "LegendWang", Point(20, 80), FONT_HERSHEY_PLAIN, 2, Scalar(0, 0, 255), 4, 8);
putText(imageSource, "LegendWang", Point(60, 100), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 25), 4, 8);
putText(imageSource, "LegendWang", Point(100, 200), FONT_HERSHEY_DUPLEX, 3, Scalar(25, 255, 25), 4, 8);
putText(imageSource, "LegendWang", Point(150, 500), FONT_HERSHEY_COMPLEX, 1, Scalar(255, 25, 25), 4, 8);
putText(imageSource, "LegendWang", Point(200, 800), FONT_HERSHEY_TRIPLEX, 4, Scalar(30, 255, 25), 4, 8);
putText(imageSource, "LegendWang", Point(250, 950), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(25, 25, 25), 4, 8);
putText(imageSource, "LegendWang", Point(20, 1000), FONT_HERSHEY_SCRIPT_SIMPLEX, 4, Scalar(160, 25, 25), 4, 8);
putText(imageSource, "LegendWang", Point(100, 1350), FONT_HERSHEY_SCRIPT_COMPLEX, 1, Scalar(80, 255, 25), 4, 8);
//在imageSource图片上,显示,位置在(20,80),字体类型为,字体大小为2,颜色为红色,字体厚度为4,线型默认为8.
imshow("显示效果图", imageSource);
imwrite("F:\\334.jpg", imageSource);
waitKey(0);//等待按键响应后退出,0改为10,就是10ms后自动退出。
return 0;
}
Result:
吐出来是这个样子的