PutText() 输出文本
- OpenCV5 将支持中文字符的输出, 当前版本OpenCV4原生不支持, 可以使用Contrib包+FreeType方式实现, 不过比较麻烦.
- 为了省事, 也可以通过将Mat转成bitmap,然后使用GDI方式输出中文字符.
示例代码
/// <summary>
/// OpenCV暂时不能支持中文字符输出,显示为??
/// </summary>
private void putTextTest()
{
string pngFile = @"D:\my_workspace\opencv\images\draw.png";
var mat = Cv2.ImRead(pngFile, ImreadModes.Color);
double fontScale = 1;
int thinkness = 2;
mat.PutText("Learn opencv中文", new OpenCvSharp.Point(100, 100), HersheyFonts.HersheyComplex,
fontScale, Scalar.Red, thinkness, LineTypes.AntiAlias, false);
fontScale = 2;
mat.PutText("Learn opencv中文", new OpenCvSharp.Point(100, 150), HersheyFonts.HersheyComplex,
fontScale, Scalar.Red, thinkness, LineTypes.AntiAlias, false);
Cv2.ImShow("Learn opencv", mat);
Cv2.WaitKey();
}
/// <summary>
/// 使用GDI方式输出中文字符
/// </summary>
private void putChineseTest()
{
string pngFile = @"D:\my_workspace\opencv\images\draw.png";
var mat = Cv2.ImRead(pngFile, ImreadModes.Color);
var bmp = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
Graphics graphics = Graphics.FromImage(bmp);
graphics.DrawString("Learn opencv中文", new Font("宋体", 20), new SolidBrush(Color.Red), 100, 200);
var mat2 = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
Cv2.ImShow("Learn opencv中文", mat2);
Cv2.WaitKey();
}
程序效果截图:
参考
- windows下编译OpenCV带opencv_contrib和freetype https://blog.csdn.net/Jay_Xio/article/details/115724624
- 听说putText()支持中文了?! https://zhuanlan.zhihu.com/p/615815756