OpenCV实现图像上添加汉字 转
2017-05-20 13:44
288 查看
<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 != 0) *w = size.cx; if(h != 0) *h = size.cy; } void paDrawString(Mat& dst, const char* str, Point org, Scalar color, int fontSize, 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 (lf.lfFaceName, "华文行楷" ); 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(buf, str); //处理多行 { int nnh = 0; int cw, ch; const char* ln = strtok(buf, "\n"); while(ln != 0) { GetStringSize(hDC, ln, &cw, &ch); strBaseW = max(strBaseW, cw); strBaseH = max(strBaseH, ch); ln = strtok(0, "\n"); 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(buf, str); const char* ln = strtok(buf, "\n"); int outTextY = 0; while(ln != 0) { TextOutA(hDC, 0, outTextY, ln, strlen(ln)); outTextY += singleRow; ln = strtok(0, "\n"); } 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 / 255.0; int cvv = vtxt * color.val + (1 - vtxt) * subImg ; subImg = cvv > 255 ? 255 : (cvv < 0 ? 0 : cvv); } subStr += 3; 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(10, 10), Scalar(255,0,0), 50, true, true); imshow("img", img); waitKey(); }
相关文章推荐
- OpenCV实现图像上添加汉字
- OpenCV之imgproc 模块. 图像处理(2)实现自己的线性滤波器 给图像添加边界 Sobel 导数 Laplace 算子 Canny 边缘检测
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
- OpenCV2实现在图像中写入汉字,支持单通道、3通道图像
- javacpp-opencv图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体、位置、大小、粗度、翻转、平滑等操作
- NVIDIA Jetson TX1 系列开发教程之十三:使用OpenCV在图像上添加汉字
- OpenCV实现在图像中写入汉字
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
- Episode Eight:opencv2.4.9实现向图像添加噪声和对图像滤波去噪
- javacpp-opencv图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体、位置、大小、粗度、翻转、平滑等操作
- OpenCV实现在图像中写入汉字
- OpenCV实现在图像中写入汉字
- OpenCV实现将三幅图像合并在一张图片
- C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示)
- 利用OpenCV实现图像的阈值分割
- opencv 旋转图像函数实现 等同于matlab里的rotate() (注:旋转后图像变大,超出部分填为黑色)
- 基于guided image filtering的图像去雾 opencv实现
- 曲线拟合的最小二乘法(基于OpenCV实现)的,拟合图像中离散点的拟合直线
- 使用OpenCV实现内存中图像数据的RGB-->HSV转换
- Opencv 图像叠加 添加水印