OpenCV实现图像上添加汉字 转

时间:2023-03-09 00:33:51
OpenCV实现图像上添加汉字 转
<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();
}