我想在图片上边写点中文,看到网上的文章都要自己编译到库里边?
实际上如果编译了opencv_contrib freetype2本来就编译进去了,网上看到很多要编译到opencv的过程,没这个必要,只要用下边的代码就可以了:
#include <opencv2/freetype.hpp>
void test(){
cv::Ptr<cv::freetype::FreeType2> ft2;
ft2 = cv::freetype::createFreeType2();
ft2->loadFontData( "/tmp/fz.ttf", 0 );
String text = "baseline";
int fontHeight = 60;
int thickness = -1;
int linestyle = 8;
int baseline=0;
Size textSize = ft2->getTextSize( text, fontHeight, thickness, &baseline );
Mat img(600, 800, CV_8UC3, Scalar::all(0));
Point textOrg( (img.cols - textSize.width) / 2, (img.rows + textSize.height) / 2 );
ft2->putText( img, text, textOrg, fontHeight, Scalar::all(255), baseline, linestyle, true );
imshow( "text", img );
waitKey();
}