OpenCV中CvFont的定义和操作

标签: opencv教育 分类: 术业专攻 
自己程序用用到的一些字体操作和设置,贴出来备个份,方便查阅。

主要是cvGetTextSize(text, &font, &text_size, &baseline);

       cvPoint(2,text_size.height+baseline)

这样写出的字才对齐得更好些。

   CvPoint pt;
   pt.x = point.x - text_size.width/2; //这样的设置为字体(左右)居中对齐
   pt.y = point.y + (text_size.height+baseline)/2; //这样的设置为字体(上下)居中对齐



  if(i==0) pt.y = (AVIFRAME_COLORBAR_HEIGHT-AVIFRAME_BAR_HEIGHT)+10; //设置字体输出的具体位置
  else if(i==portion) pt.y=AVIFRAME_COLORBAR_HEIGHT-5;


//Stage x
CvFont font;
cvInitFont(&font,CV_FONT_HERSHEY_DUPLEX ,1.0f,1.0f,0,1,CV_AA);

char text[20];
sprintf(text,"Stage%d",nIndex);
CvSize text_size;
int baseline;
cvGetTextSize(text, &font, &text_size, &baseline);
cvPutText(aviFrame,text,cvPoint(20,text_size.height+baseline),&font, CV_RGB(255,0,0));



//makeTilte
CvFont font;
cvInitFont(&font,CV_FONT_HERSHEY_TRIPLEX,0.35f,0.7f,0,1,CV_AA);
char* text="Major Strain[%]";
CvSize text_size;
int baseline;
cvGetTextSize(text, &font, &text_size, &baseline);
cvPutText(colorbar,text,cvPoint(2,text_size.height+baseline),&font,CV_RGB(0,0,0));



//写刻度值
double gap=(maxLimit-minLimit)/double(portion);
CvPoint pt;
char  buffer[30];
double value;
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX ,0.5f,0.5f,0,1,CV_AA);
for(i=0;i<=portion;i++)
{
  value = (maxLimit- i*gap)*100;
  sprintf(buffer,"%4.2f",value);
  pt.x=AVIFRAME_BAR_WIDTH+10;
  if(i==0) pt.y = (AVIFRAME_COLORBAR_HEIGHT-AVIFRAME_BAR_HEIGHT)+10;
  else if(i==portion) pt.y=AVIFRAME_COLORBAR_HEIGHT-5;
  else pt.y=i*AVIFRAME_BAR_HEIGHT/portion+(AVIFRAME_COLORBAR_HEIGHT-AVIFRAME_BAR_HEIGHT);
  cvPutText(colorbar,buffer,pt,&font, CV_RGB(0,0,0));
}



void CYnPlot::MakeLabelImage(IplImage** pImage,const char* text,double scale)
{
CvFont font;
cvInitFont(&font,CV_FONT_HERSHEY_TRIPLEX ,0.5f*scale,0.5f*scale,0,1,CV_AA);

CvSize text_size;
cvGetTextSize(text, &font, &text_size, NULL);

if (*pImage) cvReleaseImage(pImage);
*pImage = cvCreateImage(cvSize(text_size.width+5,text_size.height+10),8,3);
cvSet(*pImage,CV_RGB(255,255,255));
cvPutText(*pImage,text,cvPoint(2,text_size.height+3),&font, CV_RGB(0,0,0));
}



void CYnPlot::MakeTitleImage(IplImage** pImage,const char* text)
{
CvFont font;
cvInitFont(&font,CV_FONT_HERSHEY_TRIPLEX ,0.7f,0.7f,0,1,CV_AA);

CvSize text_size;
cvGetTextSize(text, &font, &text_size, NULL);

if (*pImage) cvReleaseImage(pImage);
*pImage = cvCreateImage(cvSize(text_size.width+5,text_size.height+10),8,3);
cvSet(*pImage,CV_RGB(255,255,255));
cvPutText(*pImage,text,cvPoint(2,text_size.height+3),&font, CV_RGB(0,0,0));
}



   CvFont font;
   CvSize text_size;
   cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX ,0.35f,0.35f,0,1,CV_AA);



   CvFont font;
   double hscale = 0.2f*_pointSize;
   cvInitFont(&font,CV_FONT_HERSHEY_TRIPLEX ,hscale,hscale,0,1,CV_AA);
  
   CvSize text_size;
   int baseline;  
   cvGetTextSize(text, &font, &text_size,&baseline);
   CvPoint pt;
   pt.x = point.x - text_size.width/2;
   pt.y = point.y + (text_size.height+baseline)/2;
   cvPutText(m_pPlotImage,text,pt,&font, color);



   CvFont font;
   double hscale = 0.2f*_pointSize;
   cvInitFont(&font,CV_FONT_HERSHEY_TRIPLEX ,hscale,hscale,0,1,CV_AA);
  
   CvSize text_size;
   int baseline;  
   cvGetTextSize(text, &font, &text_size,&baseline);
   CvPoint pt;
   pt.x = point.x - text_size.width/2;
   pt.y = point.y + (text_size.height+baseline)/2;
   cvPutText(m_pPlotImage,text,pt,&font, color);

你可能感兴趣的:(教育)