您的位置:首页 > 运维架构

opencv为图像画框并显示文字

2015-10-14 13:47 1036 查看
代码示例:

#include "opencv2/opencv.hpp"
int main()
{
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.5f, 1.5f, 0, 2, CV_AA);//设置显示的字体
IplImage *pImg=cvLoadImage("1.jpg");
CvPoint P1,P2;
P1.x=50;
P1.y=50;
P2.x=200;
P2.y=200;
char *strID;
strID="MyObject";
cvRectangle(pImg,P1 ,P2, CV_RGB(0, 255, 0), 2); //绿色画框
cvPutText(pImg,strID, cvPoint(P1.x, P1.y-10), &font, CV_RGB(255, 0, 0));//红色字体注释
cvNamedWindow("MyImg");
cvShowImage("MyImg",pImg);
cvWaitKey(0);
cvReleaseImage(&pImg);
return 1;
}
代码结果:



重要的两个函数的原型:

1./* Draws a rectangle given two opposite corners of the rectangle (pt1 & pt2),

   if thickness<0 (e.g. thickness == CV_FILLED), the filled box is drawn */

CVAPI(void)  cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2,

                          CvScalar color, int thickness CV_DEFAULT(1),

                          int line_type CV_DEFAULT(8),

                          int shift CV_DEFAULT(0));

2./* Renders text stroke with specified font and color at specified location.

   CvFont should be initialized with cvInitFont */

CVAPI(void)  cvPutText( CvArr* img, const char* text, CvPoint org,

                        const CvFont* font, CvScalar color );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: