帮同事实现了个批量加倾斜透明水印的功能,代码整理了下,供大家参考。
实现的功能:
1. 单个水印
通过putText函数或自定义的putTextHuskyC函数;
2. 批量水印
通过vconcat 纵向拼接及hconcat横向拼接;
3. 透明水印
通过addWeighted函数;
4. 批量倾斜透明水印
通过getRotationMatrix2D和warpAffine函数;
以下为具体实现:
因为cv::putText函数不能方便地绘制中文和宋体、楷体和Times New Roman等字体,采用以下代码可方便地进行文字在图像上居中绘制。
#include
#include
// 计算str字符串尺寸
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;
}
//color颜色,fontSize字号,fn字体
//italic斜体,underline下划线
void putTextHuskyC(cv::Mat& dst, const char* str, cv::Scalar color, int fontSize, const char* fn, bool italic, bool underline)
{
CV_Assert(dst.data != 0 && (dst.channels() == 1 || dst.channels() == 3));
int x, y, r, b;
x = 0;
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;
int orgX = dst.cols / 2 - strBaseW / 2;
int orgY = dst.rows / 2 - strBaseH / 2;
r = orgX + strBaseW > dst.cols ? dst.cols - orgX - 1 : strBaseW - 1;
b = orgY + strBaseH > dst.rows ? dst.rows - orgY - 1 : strBaseH - 1;
orgX = orgX < 0 ? 0 : orgX;
orgY = orgY < 0 ? 0 : orgY;
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);
SetTextColor(hDC, RGB(255, 255, 255));
SetBkColor(hDC, 0);
strcpy_s(buf, str);
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.data;
int dstStep = dst.step / sizeof(dstData[0]);
unsigned char* pImg = (unsigned char*)dst.data + orgX * dst.channels() + orgY * 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[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.channels();
}
}
SelectObject(hDC, hOldBmp);
SelectObject(hDC, hOldFont);
DeleteObject(hf);
DeleteObject(hBmp);
DeleteDC(hDC);
}
执行以下代码
cv::Mat singleWatermark = cv::Mat::zeros(cv::Size(320, 135), CV_8UC3);
std::string text = "watermark_2023_04_17";
putTextHuskyC(singleWatermark, text.c_str(), cv::Scalar(255, 255, 255), 24, "Times New Roman", false, false);
得到如下单个水印图
cv::Mat srcImage = cv::imread("ai_beauty.jpg");
if (srcImage.empty())
{
std::cout << "输入图像为空" << std::endl;
return;
}
// 重复小图成大图
int hRepeat = 3; // 横向重复次数
int vRepeat = 4; // 纵向重复次数
vector vImgs;
cv::Mat vResult;
for (int vi = 0; vi < vRepeat; vi++)
vImgs.push_back(singleWatermark);
vconcat(vImgs, vResult); //纵向拼接
vector hImgs;
cv::Mat hResult;
for (int hi = 0; hi < hRepeat; hi++)
hImgs.push_back(vResult);
hconcat(hImgs, hResult); //横向拼接
// 最终水印图
cv::Mat finalWatermark = hResult;
cv::Mat imageROI;
imageROI = srcImage(cv::Rect(0, 0, finalWatermark.cols, finalWatermark.rows));
cv::addWeighted(imageROI, 1.0, finalWatermark, 0.3, 0, imageROI);
cv::imshow("final watermark", srcImage);
cv::waitKey(0);
// 重复小图成大图
int hRepeat = 5; // 横向重复次数
int vRepeat = 8; // 纵向重复次数
vector vImgs;
cv::Mat vResult;
for (int vi = 0; vi < vRepeat; vi++)
vImgs.push_back(singleWatermark);
vconcat(vImgs, vResult); //纵向拼接
vector hImgs;
cv::Mat hResult;
for (int hi = 0; hi < hRepeat; hi++)
hImgs.push_back(vResult);
hconcat(hImgs, hResult); //横向拼接
// 旋转图像
double angle = 45; // 水印逆时针旋转角度
cv::Point2f center(hResult.cols / 2.0, hResult.rows / 2.0);
cv::Mat rotation_matix = getRotationMatrix2D(center, angle, 1.0);
cv::Mat rotated_image;
cv::warpAffine(hResult, rotated_image, rotation_matix, hResult.size());
// 最终水印图
cv::Mat finalWatermark = rotated_image(cv::Rect((hResult.cols - imageWidth) / 2, (hResult.rows - imageHeight) / 2, imageWidth, imageHeight));
// OpenCV加水印
cv::Mat imageROI;
imageROI = srcImage(cv::Rect(0, 0, finalWatermark.cols, finalWatermark.rows));
cv::addWeighted(imageROI, 1.0, finalWatermark, 0.3, 0, imageROI);
cv::imshow("final watermark", srcImage);
cv::waitKey(0);