Sunday, 10 April 2016

OpenCV C++ Code for putting Text on an Image

This Opencv C++ Tutorial is about putting Text on an Image

In Opencv we can put Text on an Image by using putText() function.
Syntax:
C++: void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )
Parameters:
img – Image.
text – Text string to be drawn.
org – Bottom-left corner of the text string in the image.
font – CvFont structure initialized using InitFont().
fontFace – Font type. One of FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX, where each of the font ID’s can be combined with FONT_ITALIC to get the slanted letters.
fontScale – Font scale factor that is multiplied by the font-specific base size.
color – Text color.
thickness – Thickness of the lines used to draw a text.
lineType – Line type. See the line for details.
bottomLeftOrigin – When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

//Opencv c++ code for Overlaying a Text on an Image
//Opencv c++ code for Putting Text on an Image
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
 Mat image;
 image=imread("C:\\Users\\arjun\\Desktop\\opencv-logo.png",1);

 if(!image.data)
 { printf("Error loading image \n"); return -1;}

    putText(image, "opencv-hub", Point(5,100), FONT_HERSHEY_DUPLEX, 1, Scalar(0,143,143), 2);

 namedWindow("Image Window image",CV_WINDOW_AUTOSIZE);
 imshow("Image Window image",image);

 waitKey(0);
 return(0);
}


Input Image:-
OpenCV-logo

Output Image:-
OpenCV C++ Code putText

Note:-Here we have put the text "opencv"  (seen in purple color) over the image.

No comments:

Post a Comment