I am currently working on my assignment and where I will load an image and display the same image with text in it. The problem is I don't know how to use the putText function.
This is the code I have right now:
cvInitFont(CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0.0, 1, 8);
cvPutText(img, "You are drinking a lot of water. You may want to cut back.", cvPoint(20, 20), CV_FONT_HERSHEY_SIMPLEX, cvScalar(255, 0, 0));
Please help me. Thanks in advance.
Step 1: Import cv2 Step 2: Define the parameters for the puttext( ) function. Step 3: Pass the parameters in to the puttext() function. Step 4: Display the image.
The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.
OpenCV is an open source C++ library for image processing and computer vision, originally developed by Intel, later supported by Willow Garage and and is now maintained by Itseez. It is free for both commercial and non-commercial use. Therefore you can use the OpenCV library even for your commercial applications.
Try something like this:
cv::Mat img(512, 512, CV_8UC3, cv::Scalar(0));
cv::putText(img, //target image
"Hello, OpenCV!", //text
cv::Point(10, img.rows / 2), //top-left position
cv::FONT_HERSHEY_DUPLEX,
1.0,
CV_RGB(118, 185, 0), //font color
2);
cv::imshow("Hello!", img);
cv::waitKey();
On a 512 * 512 black image, the code writes "Hello, OpenCV!"
This code is compatible with both OpenCV 2.x and 3.x.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With