<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. boost编译随笔

    boost下载地址 编译 生成bjam.exe 1.下载boost源码,可以直接使用上面给出的1.60.0版本 2.解压下载到的boost文件,例如解压到 x:\boost_1_60_0 3.使用Vi ...

  2. log4cpp退出时内存泄露的修复方案

    1.缘由 一直对log4cpp非常有好感,就在自己的项目中集成了log4cpp1.1.1版本,并围绕着它建立了一系列的封装函数方便外部调用.写完了一个测试代码后,忽然想看看自己写的程序有没有内存泄露问 ...

  3. 使用performance进行网页性能监控

    由于项目需要, 需要对网页的一些性能进行监控, 接触到了performance, window.performance 提供了一组精确的数据,经过简单的计算就能得出一些网页性能数据, 将这些数据存储为 ...

  4. 音乐之声——midi制作原理

    实际发出声音需要4项必备的条件 1 发生的装置 Sequencer     把sequencer想成CD播放机 (plays) 2 要演奏的乐曲 Sequence     sequence就好像是单曲 ...

  5. CentOS 7 使用iptables防火墙

    # 停止firewalld服务 systemctl stop firewalld systemctl mask firewalld # 安装iptables-services yum install ...

  6. python+flask:实现POST接口功能

    1.首先需要安装python和flask,这个是必须的嘛. 2.我们这里实现的是一个POST功能的简单接口. from flask import Flask, request, jsonify imp ...

  7. 《Android进阶之光》--Android新特性

    Android 5.0新特性 1)全新的Material Design设计风格 2)支持多种设备 3)全新的通知中心设计--按照优先级显示 4)支持64位ART虚拟机 5)多任务视窗Overview ...

  8. 3.数码相框-通过freetype库实现矢量显示

    本章主要内容如下: 1)矢量字体原理 2)使用freetype库实现矢量字体显示 1. 矢量字体原理 将汉字的笔划边缘用直线段描述成封闭的曲线,并将线段各端点的坐标经压缩存储,如下图所示: 由于每个汉 ...

  9. 2017第八届蓝桥杯 K倍区间

    标题: k倍区间 给定一个长度为N的数列,A1, A2, - AN,如果其中一段连续的子序列Ai, Ai+1, - Aj(i <= j)之和是K的倍数,我们就称这个区间[i, j]是K倍区间. ...

  10. nyoj281 整数中的1(二) 数位DP

    和整数中的1一毛一样.就是输入时改了一下罢了. AC代码: #include<cstdio> const int maxn = 35; int w[maxn], h[maxn]; void ...