YUV数据上叠加中文字符

整体思路:
1.取yuv数据的y分量转化opencv中的Mat(C语言为 IplImage)数据

			//叠加字符
			if(imgUV == NULL)
				imgUV=cvCreateImage(cvSize(w,h),IPL_DEPTH_8U,1);

			memcpy(imgUV->imageData,pYUV,w*h);

2.使用GDI+将中文字符绘制为bmp位图与Mat(C语言为 IplImage)灰度图比较,修改像素点

putTextZH(imgUV, "字符串", Point(w-300, h-100), cvScalar(255, 255, 255), 50, "黑体");在这里插入代码片
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 putTextZH(IplImage* dst, const char* str, Point org, CvScalar color, int fontSize, const char* fn, bool italic, bool underline)
{
	ASSERT(dst->imageData != 0 && (dst->nChannels == 1 || dst->nChannels == 3));

	int x, y, r, b;
	if (org.X > dst->width || org.Y > dst->height) 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_s(lf.lfFaceName, fn);

	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_s(buf, str);
	char *bufT[1 << 12];  // 这个用于分隔字符串后剩余的字符,可能会超出。处理多行
	{
		int nnh = 0;
		int cw, ch;

		const char* ln = strtok_s(buf, "\n",bufT);
		while (ln != 0)
		{
			GetStringSize(hDC, ln, &cw, &ch);
			strBaseW = max(strBaseW, cw);
			strBaseH = max(strBaseH, ch);

			ln = strtok_s(0, "\n",bufT);
			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->width ? dst->width - org.X - 1 : strBaseW - 1;
	b = org.Y + strBaseH > dst->height ? dst->height - 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);
	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_s(buf, str);
	const char* ln = strtok_s(buf, "\n",bufT);
	int outTextY = 0;
	while (ln != 0)
	{
		TextOutA(hDC, 0, outTextY, ln, strlen(ln));
		outTextY += singleRow;
		ln = strtok_s(0, "\n",bufT);
	}
	uchar* dstData = (uchar*)dst->imageData;
	int dstStep = dst->widthStep / sizeof(dstData[0]);
	unsigned char* pImg = (unsigned char*)dst->imageData + org.X * dst->nChannels + 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->nChannels; ++n){
				double vtxt = subStr[n] / 255.0;
				int cvv = vtxt * color.val[n] + (1 - vtxt) * subImg[n];
				subImg[n] = cvv > 255 ? 255 : (cvv < 0 ? 0 : cvv);
			}

			subStr += 3;
			subImg += dst->nChannels;
		}
	}

	SelectObject(hDC, hOldBmp);
	SelectObject(hDC, hOldFont);
	DeleteObject(hf);
	DeleteObject(hBmp);
	DeleteDC(hDC);
}

3.opencv中的Mat(C语言为 IplImage)数据转化为yuv数据

memcpy(pYUV,imgUV->imageData,w*h);

你可能感兴趣的:(图像处理)