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 != 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[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);
}

主函数

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();
}
时间: 2024-08-24 14:24:25

OpenCV实现图像上添加汉字 转的相关文章

opencv在图片上添加文字

/****************************************** func:cvText desc:put text on an image @param img The image pointer which we want to put text on @param text the text pointer @param x the x coordinate @param y the y coordinate @return null ****************

PHP 使用GD库生成验证码 在图像上绘制汉字

PHP 并不仅限于创建 HTML 输出, 它也可以创建和处理包括 GIF, PNG, JPEG, WBMP 以及 XPM 在内的多种格式的图像. 更加方便的是,PHP 可以直接将图像数据流输出到浏览器. 要想在 PHP 中使用图像处理功能,你需要连带 GD 库一起来编译 PHP. GD 库和 PHP 可能需要其他的库, 这取决于你要处理的图像格式. 你可以使用 PHP 中的图像函数来获取下列格式图像的大小: JPEG, GIF, PNG, SWF, TIFF 和 JPEG2000.如果联合 ex

PHP中应用GD2函数在图像上添加文字

<?php header("Content-type:text/html;charset=utf-8"); header("Content-type:image/gif"); $im=imagecreatefromjpeg("images/qie.jpg"); $textcolor=imagecolorallocate($im,56,73,136); $fnt="c:/windows/fonts/simhei.ttf";

OpenCV之响应鼠标(四):在图像上绘制出矩形并标出起点的坐标

涉及到两方面的内容:1. 用鼠标画出矩形.2.在图像上绘制出点的坐标 用鼠标绘制矩形,涉及到鼠标的操作,opencv中有鼠标事件的介绍.需要用到两个函数:回调函数CvMouseCallback和注册回调函数cvSetMouseCallback. 当回调函数被调用时,opencv会传入合适的值,当鼠标有动作时,有所反应,比如画线,描点. void CvMouseCallback(int event,int x,int y,int flags,void * param); event 为鼠标事件类型

一文带你学会使用YOLO及Opencv完成图像及视频流目标检测(上)|附源码

计算机视觉领域中,目标检测一直是工业应用上比较热门且成熟的应用领域,比如人脸识别.行人检测等,国内的旷视科技.商汤科技等公司在该领域占据行业领先地位.相对于图像分类任务而言,目标检测会更加复杂一些,不仅需要知道这是哪一类图像,而且要知道图像中所包含的内容有什么及其在图像中的位置,因此,其工业应用比较广泛.那么,今天将向读者介绍该领域中表现优异的一种算算法--"你只需要看一次"(you only look once,yolo),提出该算法的作者风趣幽默可爱,其个人主页及论文风格显示了其性

OpenCV常用图像操作和鼠标操作(双11版本)

更新日志: 1.添加了自适应窗口大小的功能: 2.添加了在图像上画矩形的功能: 3.添加了在大图上画矩形的功能: 4.部分函数名称更改: 5其他修改. 首先是头文件: /* ******* ccv.h ********** ********* opencv常用操作函数声明 ********** */ /* author: autumoon */ #ifndef _CCV_H_ #define _CCV_H_ #include <afxdlgs.h> //打开文件 #include "

使用Tslib在触摸屏上显示汉字【转】

转自:http://www.latelee.org/embedded-linux/use-tslib-to-display-chinese-character.html 终于到了在触摸屏上显示汉字了,真正写代码.测试的时间是1天,在此之前的一切准备.学习花费约2周到3周时间.而为了获取触摸屏上显示的图像,花费约2天.由于网络驱动已经接近于放弃状态,NFS用不了,只好用U盘来回复制(即使没有耐心也必须有耐心了).明明在内核中选择了支持NTFS格式的读写,但却不能将开发板上的文件复制到U盘,而用另一

Python OpenCV 实现图像滤波

一.实验过程 我使用的是python语言+openCV来实现图像滤波. 使用imread()函数读取图片,使用imshow()函数显示图片,waitKey()函数含义为按下任意键继续: 代码示例: img = cv2.imread("photo1.bmp") cv2.imshow("img",img) cv2.waitKey(0) 结果: 我们可以用pil来实现在图片上显示文字,首先先安装pillow 实例代码: #cv2和PIL中颜色的hex码的储存顺序不同,需转

OpenCV 为啥勾搭上 OpenGL

OpenCV 为啥勾搭上 OpenGL? Vinjn张静· 2 年前 如果读者留意 OpenCV 2.3 之后的版本,那么会发现 cv::ogl namespace,ogl 自然是 OpenGL了.一个三维计算机图形库为何出现在计算机视觉中,传统的 CV 开发者是否需要学习它,这些问题待我一一来回答. 问题一:为何引入 OpenGL? 在 2.3 之前 OpenCV 的渲染部分都是由 CPU 来实现的,不论是画线还是把图片显示到屏幕上.这有两个问题,速度慢,同时没法画三维物体.引入 OpenGL