05-05 04:54
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[OpenCV] putText 함수를 이용한 Text 출력 본문

Programming/OpenCV

[OpenCV] putText 함수를 이용한 Text 출력

cinema4dr12 2015. 3. 17. 01:12

[cv::putText 함수의 Prototype]

//! renders text string in the image
CV_EXPORTS_W void putText( Mat& img, const string& text, Point org,
                         int fontFace, double fontScale, Scalar color,
                         int thickness=1, int lineType=8,
                         bool bottomLeftOrigin=false );


[Source Code]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "stdafx.h"
#include "opencv2/opencv.hpp"
 
using namespace cv;
 
int _tmain(int argc, _TCHAR* argv[])
{
    /// Image
    cv::Mat myImage = imread( "../landscapes-267a.jpg" );
 
    /// Text
    string myText = "Testing Text Rendering";
    
    /// Text Location
    cv::Point myPoint;
    myPoint.x = 10;
    myPoint.y = 40;
 
    /// Font Face
    int myFontFace = 2;
 
    /// Font Scale
    double myFontScale = 1.2;
 
    cv::putText( myImage, myText, myPoint, myFontFace, myFontScale, Scalar::all(255) );
 
    /// Create Windows
    namedWindow( "Text Rendering"1 );
 
    /// Show stuff
    imshow( "Text Rendering", myImage );
 
    /// Wait until user press some key
    waitKey();
    return 0;
}
cs


만약 Font의 Color를 원하는 R, G, B로 변경하고자 하는 경우에는 다음과 같이 코드를 입력합니다.


1
cv::putText( myImage, myText, myPoint, myFontFace, myFontScale, Scalar(255255255) );
cs


Font와 관련된 CV 설정은 "core_c.h" 헤더 파일에 정의되어 있습니다:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* basic font types */
#define CV_FONT_HERSHEY_SIMPLEX         0
#define CV_FONT_HERSHEY_PLAIN           1
#define CV_FONT_HERSHEY_DUPLEX          2
#define CV_FONT_HERSHEY_COMPLEX         3
#define CV_FONT_HERSHEY_TRIPLEX         4
#define CV_FONT_HERSHEY_COMPLEX_SMALL   5
#define CV_FONT_HERSHEY_SCRIPT_SIMPLEX  6
#define CV_FONT_HERSHEY_SCRIPT_COMPLEX  7
 
/* font flags */
#define CV_FONT_ITALIC                 16
 
#define CV_FONT_VECTOR0    CV_FONT_HERSHEY_SIMPLEX
 
 
/* Font structure */
typedef struct CvFont
{
  const char* nameFont;   //Qt:nameFont
  CvScalar color;       //Qt:ColorFont -> cvScalar(blue_component, green_component, red\_component[, alpha_component])
    int         font_face;    //Qt: bool italic         /* =CV_FONT_* */
    const int*  ascii;      /* font data and metrics */
    const int*  greek;
    const int*  cyrillic;
    float       hscale, vscale;
    float       shear;      /* slope coefficient: 0 - normal, >0 - italic */
    int         thickness;    //Qt: weight               /* letters thickness */
    float       dx;       /* horizontal interval between letters */
    int         line_type;    //Qt: PointSize
}
CvFont;
cs


[Source에 사용된 이미지]


[Result]


Comments