<span style="font-size:18px;">void GetStringSize(HDC hDC, const char* str, int* w, int* h)
{
SIZE size;
GetTextExtentPoint32A(hDC, str, strlen(str), &size);
if(w != ) *w = size.cx;
if(h != ) *h = size.cy;
} void paDrawString(Mat& dst, const char* str, Point org, Scalar color, int fontSize, bool italic, bool underline)
{
CV_Assert(dst.data != && (dst.channels() == || dst.channels() == )); int x, y, r, b;
if(org.x > dst.cols || org.y > dst.rows) return;
x = org.x < ? -org.x : ;
y = org.y < ? -org.y : ; LOGFONTA lf;
lf.lfHeight = - fontSize ;
lf.lfWidth = ;
lf.lfEscapement = ;
lf.lfOrientation = ;
lf.lfWeight = ;
lf.lfItalic = italic ; //斜体
lf.lfUnderline = underline ; //下划线
lf.lfStrikeOut = ;
lf.lfCharSet = DEFAULT_CHARSET ;
lf.lfOutPrecision = ;
lf.lfClipPrecision = ;
lf.lfQuality = PROOF_QUALITY ;
lf.lfPitchAndFamily = ;
strcpy (lf.lfFaceName, "华文行楷" ); HFONT hf = CreateFontIndirectA(&lf);
HDC hDC = CreateCompatibleDC();
HFONT hOldFont = (HFONT)SelectObject(hDC, hf); int strBaseW = , strBaseH = ;
int singleRow = ;
char buf[ << ];
strcpy(buf, str); //处理多行
{
int nnh = ;
int cw, ch;
const char* ln = strtok(buf, "\n");
while(ln != )
{
GetStringSize(hDC, ln, &cw, &ch);
strBaseW = max(strBaseW, cw);
strBaseH = max(strBaseH, ch); ln = strtok(, "\n");
nnh++;
}
singleRow = strBaseH;
strBaseH *= nnh;
} if(org.x + strBaseW < || org.y + strBaseH < )
{
SelectObject(hDC, hOldFont);
DeleteObject(hf);
DeleteObject(hDC);
return;
} r = org.x + strBaseW > dst.cols? dst.cols - org.x - : strBaseW - ;
b = org.y + strBaseH > dst.rows ? dst.rows - org.y - : strBaseH - ;
org.x = org.x < ? : org.x;
org.y = org.y < ? : org.y; BITMAPINFO bmp = {};
BITMAPINFOHEADER& bih = bmp.bmiHeader;
int strDrawLineStep = strBaseW * % == ? strBaseW * : (strBaseW * + - ((strBaseW * ) % )); bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biWidth=strBaseW;
bih.biHeight=strBaseH;
bih.biPlanes=;
bih.biBitCount=;
bih.biCompression=BI_RGB;
bih.biSizeImage=strBaseH * strDrawLineStep;
bih.biClrUsed=;
bih.biClrImportant=; void* pDibData = ;
HBITMAP hBmp = CreateDIBSection(hDC, &bmp, DIB_RGB_COLORS, &pDibData, , ); CV_Assert(pDibData != );
HBITMAP hOldBmp = (HBITMAP)SelectObject(hDC, hBmp); //color.val[2], color.val[1], color.val[0]
SetTextColor(hDC, RGB(, , ));
SetBkColor(hDC, );
//SetStretchBltMode(hDC, COLORONCOLOR); strcpy(buf, str);
const char* ln = strtok(buf, "\n");
int outTextY = ;
while(ln != )
{
TextOutA(hDC, , outTextY, ln, strlen(ln));
outTextY += singleRow;
ln = strtok(, "\n");
}
uchar* dstData = (uchar*)dst.data;
int dstStep = dst.step/sizeof(dstData[]);
unsigned char* pImg = (unsigned char*)dst.data + org.x * dst.channels() + org.y * dstStep;
unsigned char* pStr = (unsigned char*)pDibData + x * ;
for(int tty = y; tty <= b; ++tty)
{
unsigned char* subImg = pImg + (tty - y) * dstStep;
unsigned char* subStr = pStr + (strBaseH - tty - ) * strDrawLineStep;
for (int ttx = x; ttx <= r; ++ttx)
{
for (int n = ; n < dst.channels(); ++n){
double vtxt = subStr[n] / 255.0;
int cvv = vtxt * color.val[n] + ( - vtxt) * subImg[n];
subImg[n] = cvv > ? : (cvv < ? : cvv);
} subStr += ;
subImg += dst.channels();
}
} SelectObject(hDC, hOldBmp);
SelectObject(hDC, hOldFont);
DeleteObject(hf);
DeleteObject(hBmp);
DeleteDC(hDC);
}

主函数

void main()
{
Mat img = imread("cat.jpg");
if(!img.data)
{
cout<<"load image error"<<endl;
return;
}
paDrawString(img, "测试汉字哈...\n换行了哟\n真的可以啊!!!~",Point(, ), Scalar(,,), , true, true);
imshow("img", img);
waitKey();
}

OpenCV实现图像上添加汉字 转的更多相关文章

  1. 用python, PIL在图像上添加文字(可以控制,调节为水印等)

    最近想在图像上,添加想要的文字,首先想到的是matplotlib,但是这个更加倾向于画图(柱状图,折线图之类) opencv这个库肯定也行,但是为了和我现有程序连接在一起,我选择了PIL 其中字体的设 ...

  2. 【深度学习】使用opencv在视频上添加文字和标记框

    深度学习识别出视频的物体之后,需要在视频上画框标记出来. 接下来介绍如何使用python在视频上画框和文字 #!/usr/bin/env python # -*- coding:utf-8 -*- i ...

  3. PHP中应用GD2函数在图像上添加文字

    <?php header("Content-type:text/html;charset=utf-8"); header("Content-type:image/g ...

  4. C# 如何获取屏幕的截图,以及如何在图像上添加文字

    关键代码为 Screen sc = Screen.PrimaryScreen; Rectangle rct = sc.Bounds; Image img = new Bitmap(rct.Width, ...

  5. 如何在matalb图像上添加公式符号

    方法: legend({'$\sigma(t)$'},'interpreter','latex') 效果如下:

  6. 利用OpenCV给图像添加中文标注

    利用OpenCV给图像添加中文标注 : 参考:http://blog.sina.com.cn/s/blog_6bbd2dd101012dbh.html  和https://blog.csdn.net/ ...

  7. OpenCV之响应鼠标(四):在图像上绘制出矩形并标出起点的坐标

    涉及到两方面的内容:1. 用鼠标画出矩形.2.在图像上绘制出点的坐标 用鼠标绘制矩形,涉及到鼠标的操作,opencv中有鼠标事件的介绍.需要用到两个函数:回调函数CvMouseCallback和注册回 ...

  8. 一文带你学会使用YOLO及Opencv完成图像及视频流目标检测(上)|附源码

    计算机视觉领域中,目标检测一直是工业应用上比较热门且成熟的应用领域,比如人脸识别.行人检测等,国内的旷视科技.商汤科技等公司在该领域占据行业领先地位.相对于图像分类任务而言,目标检测会更加复杂一些,不 ...

  9. OpenCV学习笔记(4)——图像上的算术运算

    学习图像上的算术运算,加法,减法,位运算等 1.图像加法 使用cv2.add()将两幅图像进行加法运算,也可以用numpy运算,直接img+img1.两幅图像的大小和类型必须一致,或者第二个图像可以是 ...

随机推荐

  1. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  2. Hadoop源码分类概要整理

    最近突然觉得, 很多掌握的都还是很浅的原理,需要更深入细粒度去了解整个分布式系统的运转机制.于是..开始作死而又作死而又作死的源码之旅. Hadoop包的功能总共有下列几类: tool:提供一些命令行 ...

  3. MySQL创建用户与授权

    一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...

  4. MySQL创建带有编码的数据库

    mysql> create database chao default character set utf8 collate utf8_general_ci;

  5. 2017年StackOverflow上最好的20个Python问题

    1.Python的 .. (点号 点号) 是什么语法? 答案地址:https://stackoverflow.com/questions/43487811/what-is-python-dot-dot ...

  6. 浅谈session,cookie,sessionStorage,localStorage的区别及应用场景

    浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟服务端进行数据交互. 一.cookie和session cookie和session都是用来跟踪浏览器 ...

  7. Sublime Text编辑远程Linux服务器上的文件

    sublime有个叫sftp的插件,可以通过它直接打开远程机器上的文件进行编辑,并在保存后直接同步到远程linux服务器上. 用Package Control安装插件 按下Ctrl+Shift+P调出 ...

  8. python爬虫提取冰与火之歌五季的种子

    # -*- encoding:utf-8 -*- import requests import re import sys reload(sys) sys.setdefaultencoding(&qu ...

  9. 批标准化(Batch Norm)

    BN作用: 加速收敛 控制过拟合,可以少用或不用Dropout和正则 降低网络对初始化权重不敏感 允许使用较大的学习率 一.如何加速收敛? 通过归一化输入值/隐藏单元值,以获得类似的范围值,可加速学习 ...

  10. hdu 2553 N皇后

    这题要打表,不然超时. AC代码 #include<cstdio> #include<cstring> int n,cnt; int vis[3][20]; int ans[1 ...