OpenCv 图片上添加汉字

转自:http://blog.csdn.net/xuefengdage/article/details/49447897

采用freetype库,写小字会失真,网上找到这个,利用HDC直接写,效果很好,做个摘抄。

  1 <span style="font-size:18px;">void GetStringSize(HDC hDC, const char* str, int* w, int* h)
  2 {
  3     SIZE size;
  4     GetTextExtentPoint32A(hDC, str, strlen(str), &size);
  5     if(w != 0) *w = size.cx;
  6     if(h != 0) *h = size.cy;
  7 }
  8 
  9 void paDrawString(Mat& dst, const char* str, Point org, Scalar color, int fontSize, bool italic, bool underline)
 10 {
 11     CV_Assert(dst.data != 0 && (dst.channels() == 1 || dst.channels() == 3));
 12 
 13     int x, y, r, b;
 14     if(org.x > dst.cols || org.y > dst.rows) return;
 15     x = org.x < 0 ? -org.x : 0;
 16     y = org.y < 0 ? -org.y : 0;
 17 
 18     LOGFONTA lf;
 19     lf.lfHeight         = - fontSize ;
 20     lf.lfWidth          = 0 ;
 21     lf.lfEscapement     = 0 ;
 22     lf.lfOrientation    = 0 ;
 23     lf.lfWeight         = 5;
 24     lf.lfItalic         = italic ;    //斜体
 25     lf.lfUnderline      = underline ;    //下划线
 26     lf.lfStrikeOut       = 0 ;
 27     lf.lfCharSet        = DEFAULT_CHARSET ;
 28     lf.lfOutPrecision    = 0 ;
 29     lf.lfClipPrecision    = 0 ;
 30     lf.lfQuality         = PROOF_QUALITY ;
 31     lf.lfPitchAndFamily  = 0 ;
 32     strcpy (lf.lfFaceName, "华文行楷" );
 33 
 34     HFONT hf = CreateFontIndirectA(&lf);
 35     HDC hDC = CreateCompatibleDC(0);
 36     HFONT hOldFont = (HFONT)SelectObject(hDC, hf);
 37 
 38     int strBaseW = 0, strBaseH = 0;
 39     int singleRow = 0;
 40     char buf[1 << 12];
 41     strcpy(buf, str);
 42 
 43     //处理多行
 44     {
 45         int nnh = 0;
 46         int cw, ch;
 47         const char* ln = strtok(buf, "\n");
 48         while(ln != 0)
 49         {
 50             GetStringSize(hDC, ln, &cw, &ch);
 51             strBaseW = max(strBaseW, cw);
 52             strBaseH = max(strBaseH, ch);
 53 
 54             ln = strtok(0, "\n");
 55             nnh++;
 56         }
 57         singleRow = strBaseH;
 58         strBaseH *= nnh;
 59     }
 60 
 61     if(org.x + strBaseW < 0 || org.y + strBaseH < 0)
 62     {
 63         SelectObject(hDC, hOldFont);
 64         DeleteObject(hf);
 65         DeleteObject(hDC);
 66         return;
 67     }
 68 
 69     r = org.x + strBaseW > dst.cols? dst.cols - org.x - 1 : strBaseW - 1;    
 70     b = org.y + strBaseH > dst.rows ? dst.rows - org.y - 1 : strBaseH - 1;
 71     org.x = org.x < 0 ? 0 : org.x;
 72     org.y = org.y < 0 ? 0 : org.y;
 73 
 74     BITMAPINFO bmp = {0};
 75     BITMAPINFOHEADER& bih = bmp.bmiHeader;
 76     int strDrawLineStep = strBaseW * 3 % 4 == 0 ? strBaseW * 3 : (strBaseW * 3 + 4 - ((strBaseW * 3) % 4));
 77 
 78     bih.biSize=sizeof(BITMAPINFOHEADER);
 79     bih.biWidth=strBaseW;
 80     bih.biHeight=strBaseH;
 81     bih.biPlanes=1;
 82     bih.biBitCount=24;
 83     bih.biCompression=BI_RGB;
 84     bih.biSizeImage=strBaseH * strDrawLineStep;
 85     bih.biClrUsed=0;
 86     bih.biClrImportant=0;
 87 
 88     void* pDibData = 0;
 89     HBITMAP hBmp = CreateDIBSection(hDC, &bmp, DIB_RGB_COLORS, &pDibData, 0, 0);
 90 
 91     CV_Assert(pDibData != 0);
 92     HBITMAP hOldBmp = (HBITMAP)SelectObject(hDC, hBmp);
 93 
 94     //color.val[2], color.val[1], color.val[0]
 95     SetTextColor(hDC, RGB(255, 255, 255));
 96     SetBkColor(hDC, 0);
 97     //SetStretchBltMode(hDC, COLORONCOLOR);
 98 
 99     strcpy(buf, str);
100     const char* ln = strtok(buf, "\n");
101     int outTextY = 0;
102     while(ln != 0)
103     {
104         TextOutA(hDC, 0, outTextY, ln, strlen(ln));
105         outTextY += singleRow;
106         ln = strtok(0, "\n");
107     }
108     uchar* dstData = (uchar*)dst.data;
109     int dstStep = dst.step/sizeof(dstData[0]);
110     unsigned char* pImg = (unsigned char*)dst.data + org.x * dst.channels() + org.y * dstStep;
111     unsigned char* pStr = (unsigned char*)pDibData + x * 3;
112     for(int tty = y; tty <= b; ++tty)
113     {
114         unsigned char* subImg = pImg + (tty - y) * dstStep;
115         unsigned char* subStr = pStr + (strBaseH - tty - 1) * strDrawLineStep;
116         for (int ttx = x; ttx <= r; ++ttx)
117         {
118             for (int n = 0; n < dst.channels(); ++n){
119                 double vtxt = subStr[n] / 255.0;
120                 int cvv =  vtxt * color.val[n] + (1 - vtxt) * subImg[n];
121                 subImg[n] = cvv > 255 ? 255 : (cvv < 0 ? 0 : cvv);
122             }
123 
124             subStr += 3;
125             subImg += dst.channels();
126         }
127     }
128 
129     SelectObject(hDC, hOldBmp);
130     SelectObject(hDC, hOldFont);
131     DeleteObject(hf);
132     DeleteObject(hBmp);
133     DeleteDC(hDC);
134 }
View Code

 

转载于:https://www.cnblogs.com/gaohai/p/8044035.html

在Qt C++中,可以使用OpenCV库来添加中文文字到图片上。下面是一个简单的示例代码: 首先,确保已经正确地安装了OpenCV和Qt C++库,并在项目中进行了正确的配置。 接下来,我们需要使用OpenCV中的putText()函数来在图片上绘制文字。该函数具有以下参数:图像,要绘制的文字,文字的起始坐标,字体类型,字体大小,颜色和文本粗细等。 ``` #include <opencv2/opencv.hpp> #include <QtCore/QCoreApplication> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); cv::Mat image = cv::imread("your_image.jpg"); // 读取图片 if (image.empty()) { qDebug() << "Failed to load image"; return -1; } cv::Scalar color(0, 0, 255); // 设置文字颜色为红色 std::string chinese_text = "你好,世界!"; // 要绘制的中文文字 cv::putText(image, chinese_text, cv::Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1.0, color, 2); // 在图片上绘制中文文字 cv::imshow("Image with Chinese Text", image); // 显示带有中文文字的图片 cv::waitKey(0); return a.exec(); } ``` 在上面的示例代码中,我们首先读取了一张图片"your_image.jpg"。然后,定义了红色文字(BGR颜色格式)。接下来,将要绘制的中文文字赋值给字符串变量"chinese_text"。然后,使用putText()函数在图片上指定的位置(50, 50)绘制中文文字。 注意,在Windows环境下,如果默认的字体不支持中文显示,需要手动设置字体库为支持中文的字体。 最后,通过imshow()函数显示带有中文文字的图片,并通过waitKey()函数等待按键操作。 以上就是在Qt C++中使用OpenCV库来在图片上写中文的简单示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值