Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a text in an image in opencv c++ [duplicate]

Tags:

c++

opencv

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.

like image 491
Tatamatugas Avatar asked Sep 30 '17 06:09

Tatamatugas


People also ask

How do I show text in cv2?

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.

How are images stored in OpenCV?

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.

What is C++ OpenCV?

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.


1 Answers

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.

like image 175
ghchoi Avatar answered Sep 30 '22 00:09

ghchoi