Write on an image using openCV in C++
Last Updated :
04 Feb, 2021
In this article, we will discuss how to write over an image using OpenCV C++. Function putText() from OpenCV C++ library will be used to write text on an image.
Program 1:
Below program shows how to write text over a blank background image:
C++
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
int main( int argc, char ** argv)
{
Mat image(500, 500, CV_8UC3,
Scalar(255, 255, 255));
if (!image.data) {
cout << "Could not open or"
<< " find the image"
<< endl;
return 0;
}
Point org(30, 100);
putText(image, "Text On Image" , org,
FONT_HERSHEY_SCRIPT_COMPLEX, 2.1,
Scalar(0, 0, 255), 2, LINE_AA);
imshow( "Output" , image);
waitKey(0);
return 0;
}
|
Output:
Program 2:
Below program shows how to write text over a loaded image:
C++
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
int main( int argc, char ** argv)
{
Mat image = imread( "C:/Users/harsh/Downloads/geeks.png" ,
IMREAD_COLOR);
if (!image.data) {
cout << "Could not open or"
<< " find the image" << std::endl;
return 0;
}
Point org(1, 30);
putText(image, "Geeks For Geeks" , org,
FONT_HERSHEY_SCRIPT_COMPLEX, 1.0,
Scalar(0, 255, 0), 2, LINE_AA);
imshow( "Output" , image);
waitKey(0);
return 0;
}
|
Output:
Share your thoughts in the comments
Please Login to comment...