所以我试图在图像上使用putText()来编写单个字符以适应25x25的框但文本太小而无法渲染,它看起来就像我选择文本的任何颜色的blob 。有没有办法用OpenCV创建小的,可读的文本覆盖到图像上?
答案 0 :(得分:0)
以下是使用putText()
同时加载Photoshop或GIMP中创建的文件中的字符的示例。
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
using namespace cv;
using namespace std;
int
main(int argc,char*argv[])
{
// Make a 3 channel image
cv::Mat main(200,300,CV_8UC3);
// Fill entire image with magenta
main = cv::Scalar(255,0,255);
// Load a character "M" from a file and overlay
Mat txt = cv::imread("M.png",-CV_LOAD_IMAGE_ANYDEPTH);
txt.copyTo(main(cv::Rect(80,120,txt.cols,txt.rows)));
// Now use puttext() to do a white S
int fontFace = FONT_HERSHEY_COMPLEX_SMALL;
double fontScale=1.5;
string text="S";
putText(main,"S",Point(60,100),fontFace,fontScale,Scalar(255,255,255));
// Save to disk
imwrite("result.png",main);
}
这里是M.png
文件:
结果如下:
我还注意到反锯齿字体(在下图中的右侧)看起来有点容易阅读:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
using namespace cv;
using namespace std;
int
main(int argc,char*argv[])
{
// Make a 3 channel image
cv::Mat main(280,800,CV_8UC3);
// Fill entire image with magenta
main = cv::Scalar(255,0,255);
double fontScale=1.5;
int thickness=1;
int x=10,y=40;
putText(main,"Simplex",Point(x,y),CV_FONT_HERSHEY_SIMPLEX,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Simplex AA",Point(x+400,y),CV_FONT_HERSHEY_SIMPLEX,fontScale,Scalar(255,255,255),thickness,CV_AA);
y+=40;
putText(main,"Plain",Point(x,y),CV_FONT_HERSHEY_PLAIN,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Plain AA",Point(x+400,y),CV_FONT_HERSHEY_PLAIN,fontScale,Scalar(255,255,255),thickness,CV_AA);
y+=40;
putText(main,"Duplex",Point(x,y),CV_FONT_HERSHEY_DUPLEX,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Duplex AA",Point(x+400,y),CV_FONT_HERSHEY_DUPLEX,fontScale,Scalar(255,255,255),thickness,CV_AA);
y+=40;
putText(main,"Complex",Point(x,y),CV_FONT_HERSHEY_COMPLEX,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Complex AA",Point(x+400,y),CV_FONT_HERSHEY_COMPLEX,fontScale,Scalar(255,255,255),thickness,CV_AA);
y+=40;
putText(main,"Triplex",Point(x,y),CV_FONT_HERSHEY_TRIPLEX,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Triplex AA",Point(x+400,y),CV_FONT_HERSHEY_TRIPLEX,fontScale,Scalar(255,255,255),thickness,CV_AA);
y+=40;
putText(main,"Script",Point(x,y),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Script AA",Point(x+400,y),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale,Scalar(255,255,255),thickness,CV_AA);
// Save to disk
imwrite("result.png",main);
}