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;
}
相关推荐
FL16238631292 小时前
[cmake]基于C++使用纯opencv部署ppocrv5v6的onnx模型
开发语言·c++·opencv
2401_885665197 小时前
基于OpenCV的模板匹配OCR实战:银行卡与身份证数字识别完整教程
人工智能·python·opencv·计算机视觉·ocr
winfredzhang16 小时前
用 MediaPipe 手势数字识别一键打开下载夹里的图片(Python + OpenCV 实战)
人工智能·python·opencv·google·mediapipe
sali-tec1 天前
C# 基于OpenCv的视觉工作流-章84-包胶有无检测
图像处理·人工智能·opencv·算法·计算机视觉
FL16238631291 天前
基于C#winform使用纯opencv部署ppocrv5和ppocrv6的onnx模型进行OCR文件检测识别
opencv·c#·ocr
江畔柳前堤1 天前
agent面试题
数据库·人工智能·opencv·数据挖掘·语音识别·agent
Java患者·2 天前
《Python 人脸识别入门实践:从人脸检测到人脸比对完整实现》
开发语言·python·opencv·目标检测·计算机视觉·目标跟踪·视觉检测
丨白色风车丨2 天前
OpenCV 实战入门:轮廓检测、模板匹配与命令行参数解析
人工智能·opencv·计算机视觉
Jerryhut2 天前
opencv对齐算法及其应用
人工智能·opencv·算法
盼小辉丶3 天前
OpenCV-Python实战(28)——OpenCV计算摄影从HDR图像融合到全景拼接
python·opencv·计算机视觉