目录
Pangolin是个非常好用的三维显示工具,但是唯独没有发现有提供文本显示的方法!!
是有的,是我唐突了
我以为:
效果图如下:
主要原理是借助OpenCV的文本显示,但是OpenCV不支持汉字,百度以下就能找到方法,这里不做说明。
具体步骤是首先通过cv::putText()获取二维文本坐标,再根据需要设定缩放系数,指定第三维度坐标。
代码如下:
input:要显示的字符串
return: 采样后字符的三维坐标集合
vector<cv::Mat> textToPoints(string text)
{
// string text = "box";
//int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX; //手写风格字体
int fontFace = 0;
double fontScale = 1; //字体缩放比
int thickness = 1;
int baseline = 0;
cv::Size textSize = cv::getTextSize(text, fontFace, fontScale, thickness, &baseline);
baseline += thickness;
cv::Mat img(textSize.height+5,100,CV_8UC1, cv::Scalar::all(0));
//pos the text
cv::Point textOrg(0, textSize.height);
cv::line(img,textOrg + cv::Point(0,thickness),textOrg + cv::Point(textSize.width,thickness),cv::Scalar(0,0,255));
cv::putText(img,text,textOrg,fontFace,fontScale,cv::Scalar::all(255),thickness,8);
cv::imshow("text",img);
// cv::waitKey(1);
vector<cv::Mat> txtPoints;
for(int i=0,iend=img.rows;i<iend;i++)
{
for(int j=0,jend=img.cols;j<jend;j++)
{
if((int)img.at<uchar>(i,j)==255)
{
cv::Mat pos=(cv::Mat_<float>(3,1)<<i,0,j);
txtPoints.push_back(pos);
}
}
}
return txtPoints;//你可以用Pangolin把点画出来
}
事实上:
class PANGOLIN_EXPORT GlFont
{
public:
// Singleton instance if requested.
static GlFont& I();
// Load GL Font data. Delay uploading as texture until first use.
GlFont(const unsigned char* ttf_buffer, float pixel_height, int tex_w=512, int tex_h=512);
GlFont(const std::string& filename, float pixel_height, int tex_w=512, int tex_h=512);
virtual ~GlFont();
// Generate renderable GlText object from this font.
GlText Text( const char* fmt, ... );
GlText Text( const std::string& str );
inline float Height() const {
return font_height_px;
}
两种构造方法,以第二种为例:
pangolin::GlFont *text_font = new pangolin::GlFont("/home/user/Third_lib/font/Anonymous-Pro-Bold.ttf", 15.0); //需要先去网上下载字体
text_font->Text("text").Draw(0.0, 0.0, 0.0);//参数为xyz坐标
效果如下:
而且会一直跟随,正对视角!!