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;
}
相关推荐
晨间晓16 小时前
图片里的高频和低频是什么?用华为云码道开发 OpenCV 傅里叶频谱分析工具
opencv·计算机视觉·华为云
晨间晓16 小时前
图片里的阴影能不能减弱?用华为云码道开发 OpenCV 光照校正工具
人工智能·opencv·华为云
晨间晓1 天前
图片里的细小噪点怎么平滑?用华为云码道开发 OpenCV 图像滤波对比工具
人工智能·opencv·华为云
偷吃米饭1 天前
图片特征怎么描述?用华为云码道开发 OpenCV ORB 特征提取工具
人工智能·opencv·华为云
晨间晓2 天前
图片里的关键点怎么找?用华为云码道开发 OpenCV FAST 特征点检测工具
人工智能·opencv·华为云
晨间晓2 天前
二值图里的噪点怎么去掉?用华为云码道开发 OpenCV 形态学操作工具
人工智能·opencv·华为云
晨间晓2 天前
图片里的角点在哪里?用华为云码道开发 OpenCV Harris 角点检测工具
人工智能·opencv·华为云
晨间晓2 天前
图片细节丰富不丰富?用华为云码道开发 OpenCV 纹理复杂度分析工具
人工智能·opencv·华为云
richard_yuu2 天前
OpenCV 实战第 15 篇:G-API 图计算,工业流水线的 CPU/GPU 混合执行
人工智能·opencv·计算机视觉
老唐7773 天前
OpenCV5实测:纯CPU跑YOLO26目标检测
图像处理·人工智能·opencv·yolo·目标检测·计算机视觉