话不多说,直接上代码
cpp
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/freetype.hpp>
using namespace std;
using namespace cv;
int main(void)
{
Mat image(1000, 1800, CV_8UC3, Scalar(200,162,33));
Ptr<freetype::FreeType2> ft2;
ft2 = freetype::createFreeType2();
ft2->loadFontData("./simhei.ttf", 0);
ft2->putText(image, "中文显示", Point(500, 500),150, CV_RGB(255, 206, 57),FILLED, LINE_AA, true);
imshow("提示", image);
waitKey(0);
}
调用freetype库,载入ttf字体包,解决。
ttf包只要是中文的都可以,我这里用的是宋体(simhei.ttf),在windows下拷贝过来的。
目录为:C:/Windows/fonts.
效果演示
CmakeList 参考
bash
# cmakelists: demo
cmake_minimum_required (VERSION 3.8)
project (warning VERSION 1.0.0)
find_package (OpenCV REQUIRED)
include_directories (${OpenCV_INCLUDE_DIRS})
add_executable (warning main.cpp)
target_link_libraries (warning ${OpenCV_LIBS})