Opencv C++写中文(来自Gemini)

基于与Google Gemini交互获取的Opencv在图片上写汉字的实现

复制代码
sudo apt-get install libfreetype6-dev
sudo apt-get install fonts-wqy-zenhei

CMakeLists.txt

复制代码
cmake_minimum_required(VERSION 3.10) # Or a more recent version

project(OpenCVChineseText)

set(CMAKE_CXX_STANDARD 17) # Or your preferred C++ standard
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# --- Find OpenCV ---
find_package(OpenCV REQUIRED)
if(OpenCV_FOUND)
    message(STATUS "Found OpenCV: ${OpenCV_DIR}")
    include_directories(${OpenCV_INCLUDE_DIRS})
else()
    message(FATAL_ERROR "OpenCV not found. Please set OpenCV_DIR or ensure it's in your PATH.")
endif()

# --- Find FreeType ---
find_package(Freetype REQUIRED)
if(Freetype_FOUND)
    message(STATUS "Found FreeType: ${FREETYPE_DIR}")
    include_directories(${FREETYPE_INCLUDE_DIRS})
else()
    message(FATAL_ERROR "FreeType not found. Please ensure it's installed.")
endif()

# --- Add Executable ---
add_executable(opencv_chinese_text main.cpp) # Replace main.cpp with your source file(s)

# --- Link Libraries ---
target_link_libraries(opencv_chinese_text
    ${OpenCV_LIBS}
    ${FREETYPE_LIBRARIES}
)

# --- Optional: Install target ---
install(TARGETS opencv_chinese_text DESTINATION bin)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/fonts DESTINATION share/opencv_chinese_text) # If you have a fonts folder

main.cpp

cpp 复制代码
#include <iostream>
#include <string>
#include <vector>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <opencv2/opencv.hpp>
#include <locale>
#include <codecvt>

int main()
{
    cv::Mat image = cv::Mat::zeros(200, 500, CV_8UC3);
    std::string chineseText = "你好,世界";
    int fontSize = 48;
    int x = 50, y = 100;
    cv::Scalar textColor(255, 255, 255); // White color for text

    FT_Library library;
    if (FT_Init_FreeType(&library))
    {
        std::cerr << "Error initializing FreeType library" << std::endl;
        return -1;
    }

    FT_Face face;
    if (FT_New_Face(library, "/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", 0, &face))
    {
        std::cerr << "Error loading font file" << std::endl;
        FT_Done_FreeType(library);
        return -1;
    }

    FT_Set_Pixel_Sizes(face, 0, fontSize);

    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
    std::wstring wide_text = converter.from_bytes(chineseText);

    for (wchar_t wc : wide_text)
    {
        FT_UInt glyph_index = FT_Get_Char_Index(face, wc);
        if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER))
        {
            std::cerr << "Error loading glyph" << std::endl;
            continue;
        }

        FT_Bitmap &bitmap = face->glyph->bitmap;

        for (int i = 0; i < bitmap.rows; ++i)
        {
            for (int j = 0; j < bitmap.width; ++j)
            {
                unsigned char pixel_value = bitmap.buffer[i * bitmap.pitch + j];
                if (pixel_value > 0)
                {
                    cv::Point draw_point(x + face->glyph->bitmap_left + j, y - face->glyph->bitmap_top + i);
                    if (draw_point.x >= 0 && draw_point.x < image.cols && draw_point.y >= 0 && draw_point.y < image.rows)
                    {
                        image.at<cv::Vec3b>(draw_point)[0] = cv::saturate_cast<uchar>(textColor[0] * (pixel_value / 255.0));
                        image.at<cv::Vec3b>(draw_point)[1] = cv::saturate_cast<uchar>(textColor[1] * (pixel_value / 255.0));
                        image.at<cv::Vec3b>(draw_point)[2] = cv::saturate_cast<uchar>(textColor[2] * (pixel_value / 255.0));
                    }
                }
            }
        }

        x += face->glyph->advance.x >> 6;
    }

    FT_Done_Face(face);
    FT_Done_FreeType(library);

    cv::putText(image, "This is English", cv::Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(255, 255, 255));
    cv::imshow("Image with Text", image);
    cv::waitKey(0);
    cv::destroyAllWindows();

    return 0;
}
相关推荐
jndingxin12 小时前
OpenCV CUDA模块设备层-----反向二值化阈值处理函数thresh_binary_inv_func()
人工智能·opencv·计算机视觉
jndingxin13 小时前
OpenCV CUDA模块设备层-----在 GPU 上执行类似于 std::copy 的操作函数warpCopy()
人工智能·opencv·计算机视觉
晓131313 小时前
OpenCV篇——项目(二)OCR文档扫描
人工智能·python·opencv·pycharm·ocr
jndingxin13 小时前
OpenCV CUDA模块设备层-----在GPU 上高效地执行两个 uint 类型值的最大值比较函数vmax2()
人工智能·opencv·计算机视觉
luofeiju20 小时前
RGB下的色彩变换:用线性代数解构色彩世界
图像处理·人工智能·opencv·线性代数
Echo``10 天前
12.OpenCV—基础入门
开发语言·c++·人工智能·qt·opencv·计算机视觉
jndingxin10 天前
OpenCV CUDA模块设备层-----线程块内初始化连续内存区域 的设备端工具函数blockYota()
人工智能·opencv·计算机视觉
AI technophile11 天前
OpenCV计算机视觉实战(12)——图像金字塔与特征缩放
人工智能·opencv·计算机视觉
无证驾驶梁嗖嗖11 天前
[特殊字符] OpenCV opencv_world 模块作用及编译实践完整指南
opencv
从零开始学习人工智能11 天前
相机标定与3D重建技术通俗讲解
opencv·算法