opencv 显示中文字体
OpenCV显示中文汉字,未使用CvxText和FreeType库
采用windows的GDI显示系统的TrueType字体,没有封装,就两个函数,分成了h和cpp文件,可以自己编辑文件名和函数名,亦可以直接将cpp的代码复制到你需要的程序中。
1. puttext.h文件
#ifndef PUTTEXT_H_
#define PUTTEXT_H_#include <windows.h>#include <string>#include <opencv2/opencv.hpp>using namespace cv;void GetStringSize(HDC hDC, const char* str, int* w, int* h);void putTextZH(Mat &dst, const char* str, Point org, Scalar color, int fontSize,const char *fn = "Arial", bool italic = false, bool underline = false);#endif
2. puttext.cpp 文件
#include "puttext.h"
void GetStringSize(HDC hDC, const char* str, int* w, int* h){SIZE size;GetTextExtentPoint32A(hDC, str, strlen(str), &size);if (w != 0) *w = size.cx;if (h != 0) *h = size.cy;}void putTextZH(Mat &dst, const char* str, Point org, Scalar color, int fontSize, const char* fn, bool italic, bool underline){CV_Assert(dst.data != 0 && (dst.channels() == 1 || dst.channels() == 3));int x, y, r, b;if (org.x > dst.cols || org.y > dst.rows) return;x = org.x < 0 ? -org.x : 0;y = org.y < 0 ? -org.y : 0;LOGFONTA lf;lf.lfHeight = -fontSize;lf.lfWidth = 0;lf.lfEscapement = 0;lf.lfOrientation = 0;lf.lfWeight = 5;lf.lfItalic = italic;lf.lfUnderline = underline; //下划线lf.lfStrikeOut = 0;lf.lfCharSet = DEFAULT_CHARSET;lf.lfOutPrecision = 0;lf.lfClipPrecision = 0;lf.lfQuality = PROOF_QUALITY;lf.lfPitchAndFamily = 0;strcpy_s(lf.lfFaceName, fn);HFONT hf = CreateFontIndirectA(&lf);HDC hDC = CreateCompatibleDC(0);HFONT hOldFont = (HFONT)SelectObject(hDC, hf);int strBaseW = 0, strBaseH = 0;int singleRow = 0;char buf[1 << 12];strcpy_s(buf, str);char *bufT[1 << 12];// 这个用于分隔字符串后剩余的字符,可能会超出。//处理多行{int nnh = 0;int cw, ch;const char* ln = strtok_s(buf, "\n", bufT);while (ln != 0){GetStringSize(hDC, ln, &cw, &ch);strBaseW = max(strBaseW, cw);strBaseH = max(strBaseH, ch);ln = strtok_s(0, "\n", bufT);nnh++;}singleRow = strBaseH;strBaseH *= nnh;}if (org.x + strBaseW < 0 || org.y + strBaseH < 0){SelectObject(hDC, hOldFont);DeleteObject(hf);DeleteObject(hDC);return;}r = org.x + strBaseW > dst.cols ? dst.cols - org.x - 1 : strBaseW - 1;b = org.y + strBaseH > dst.rows ? dst.rows - org.y - 1 : strBaseH - 1;org.x = org.x < 0 ? 0 : org.x;org.y = org.y < 0 ? 0 : org.y;BITMAPINFO bmp = { 0 };BITMAPINFOHEADER& bih = bmp.bmiHeader;int strDrawLineStep = strBaseW * 3 % 4 == 0 ? strBaseW * 3 : (strBaseW * 3 + 4 - ((strBaseW * 3) % 4));bih.biSize = sizeof(BITMAPINFOHEADER);bih.biWidth = strBaseW;bih.biHeight = strBaseH;bih.biPlanes = 1;bih.biBitCount = 24;bih.biCompression = BI_RGB;bih.biSizeImage = strBaseH * strDrawLineStep;bih.biClrUsed = 0;bih.biClrImportant = 0;void* pDibData = 0;HBITMAP hBmp = CreateDIBSection(hDC, &bmp, DIB_RGB_COLORS, &pDibData, 0, 0);CV_Assert(pDibData != 0);HBITMAP hOldBmp = (HBITMAP)SelectObject(hDC, hBmp);//color.val[2], color.val[1], color.val[0]SetTextColor(hDC, RGB(255, 255, 255));SetBkColor(hDC, 0);//SetStretchBltMode(hDC, COLORONCOLOR);strcpy_s(buf, str);const char* ln = strtok_s(buf, "\n", bufT);int outTextY = 0;while (ln != 0){TextOutA(hDC, 0, outTextY, ln, strlen(ln));outTextY += singleRow;ln = strtok_s(0, "\n", bufT);}uchar* dstData = (uchar*)dst.data;int dstStep = dst.step / sizeof(dstData[0]);unsigned char* pImg = (unsigned char*)dst.data + org.x * dst.channels() + org.y * dstStep;unsigned char* pStr = (unsigned char*)pDibData + x * 3;for (int tty = y; tty <= b; ++tty){unsigned char* subImg = pImg + (tty - y) * dstStep;unsigned char* subStr = pStr + (strBaseH - tty - 1) * strDrawLineStep;for (int ttx = x; ttx <= r; ++ttx){for (int n = 0; n < dst.channels(); ++n) {double vtxt = subStr[n] / 255.0;int cvv = vtxt * color.val[n] + (1 - vtxt) * subImg[n];subImg[n] = cvv > 255 ? 255 : (cvv < 0 ? 0 : cvv);}subStr += 3;subImg += dst.channels();}}SelectObject(hDC, hOldBmp);SelectObject(hDC, hOldFont);DeleteObject(hf);DeleteObject(hBmp);DeleteDC(hDC);}
3.调用实例:
#include "opencv2/opencv.hpp"#include "putText.h"using namespace std;using namespace cv;int main(){Mat img = imread("1000.jpg");putTextZH(img, "Arial字体换...\n行显示!", Point(50, 50), Scalar(0, 0, 255), 30, "Arial");putTextZH(img, "微软雅黑字体换...\n行,斜体,下划线,显示!", Point(50, 100), Scalar(0, 255, 0), 30, "微软雅黑", true, true);putTextZH(img, "楷体字体换...\n行,斜体,下划线,显示!", Point(50, 200), Scalar(128, 255, 0), 30, "楷体", true, true);putTextZH(img, "梁朝伟", Point(500, 500), Scalar(0, 0, 255), 30, "楷体", false, false);imshow("test", img);waitKey();return 0;}
4.显示结果
https://blog.csdn.net/caomin1hao/article/details/83057626
https://blog.csdn.net/caomin1hao/article/details/83057626
opencv 显示中文字体相关推荐
- OpenCV显示中文字体
01.引言 OpenCV-Python 是一个用来图像处理的 Python 库,但其一般不能在图片上显示中文,否则会乱码.但有些情况下我们必须要显示中文,因此,我们可以使用 PIL 库来写中文,再转换 ...
- OpenCV显示中文汉字,未使用CvxText和FreeType库
OpenCV显示中文汉字,未使用CvxText和FreeType库 采用windows的GDI显示系统的TrueType字体,没有封装,就两个函数,分成了h和cpp文件,可以自己编辑文件名和函数名,亦 ...
- Ubuntu下使用OpenCV显示中文
由于Opencv默认不显示中文,所以我们需要通过需要通过一些库来设置OpenCV支持中文显示 源码下载链接:Ubuntu下Opencv显示中文 代码说明 项目需要ft2build.h,它是freety ...
- 9、10、11、12、13_添加标注 (Annotations)、添加网格线(Grid Lines)、显示中文字体、保存图形(saving Figures)、高质量矢量图输出
9.添加标注 (Annotations) 10.添加网格线(Grid Lines) 11.显示中文字体 12.保存图形(saving Figures) 13.高质量矢量图输出 9.添加标注 (Anno ...
- matplotlib 无法显示中文字体的解决方法
matplotlib 无法显示中文字体的解决方法 参考文章: (1)matplotlib 无法显示中文字体的解决方法 (2)https://www.cnblogs.com/lingLongBaby/p ...
- python显示汉字_python如何显示中文字体
python如何显示中文字体? 在这里,你可以选择2种不同的解决方法 方法一:定义声明好编码格式 首先你要做的,是在打开写入文件时,声明encoding编码put_in = open(becopyed ...
- 【MacBook python画图显示中文字体】
mac系统hewindows系统在画图时显示中文字体的方式不一样,mac用python画图时需要加上 plt.rcParams['font.sans-serif'] = ['Arial Unicode ...
- python中文字体下载_Python在Matplotlib图中显示中文字体的操作方法
1. 说明 本篇主要针对在Ubuntu系统中,matplotlib显示不了中文的问题,尤其是在无法安装系统字体的情况下,解决Python绘图时中文显示的问题. 2. 在系统中安装字体 $ fc-lis ...
- Ubuntu下让matplotlib显示中文字体
文章目录 安装中文字体 显示matplotlib库的字体文件夹 删除matplotlib 的缓存文件(可选) matplotlib中设置字体,显示! 参考文章: https://zodiac911.g ...
最新文章
- 《OpenGL编程指南》一第2章 着色器基础
- 测试ModelAttribute注解
- 【Linux】一步一步学Linux——ssh-keyscan命令(179)
- 【ZOJ - 3329】One Person Game(带循环的概率dp,数学期望,高斯消元,数学)
- 数据库基础知识——流程控制结构
- 值得思考,机器学习模型做出的决策是你想要的吗?
- PAT 1014 Waiting in Line
- oracle sql 匹配 一位,关于在SQL中查找匹配间隔:在SQL中查找匹配间隔-Oracle
- 我开发的kvm虚拟化虚拟机批量生产脚本
- java.lang.IllegalArgumentException: parameter must be a descendant of this view
- Discuz! X3.0/X3.1/X3.2通用 Apache伪静态规则
- 计算机培训实践反思模板,基于问题解决式课堂教学模式的反思
- c语言10h,bios 10h中断是什么意思啊?
- iOS苹果开发者账号申请教程
- 简单人物画像_超级简单人物素描画图片精选
- win10怎么还原成win7系统
- jaffe 数据库百度网盘下载
- Flutter Card、ListTitle
- 软件测试工程师发展方向,主要有哪些?
- 14、美女福利图片API接口,免费好用