Learn how to efficiently overlay text on images represented as cv::Mat objects in your computer vision projects using OpenCV in C++.
This guide provides a step-by-step approach to adding text to images using the OpenCV library in C++. We'll cover loading images, defining text properties, overlaying text, and optionally, creating transparent text effects using masks.
Load your image:
cv::Mat image = cv::imread("path/to/your/image.jpg");Define text, position, font, and color:
std::string text = "Hello, OpenCV!";
cv::Point textOrg(10, 50);
int fontFace = cv::FONT_HERSHEY_SIMPLEX;
double fontScale = 1;
cv::Scalar color(255, 0, 0); // Blue colorUse putText() to overlay text:
cv::putText(image, text, textOrg, fontFace, fontScale, color, 2);(Optional) Create a mask for transparency:
cv::Mat with the same size and type as your image.putText() on the mask with a white color to draw the text area.cv::Mat mask = cv::Mat::zeros(image.size(), image.type());
cv::putText(mask, text, textOrg, fontFace, fontScale, cv::Scalar(255, 255, 255), -1); // Filled text(Optional) Blend the text with the image using the mask:
copyTo() with the mask to copy the text onto the image.cv::Mat textImage;
image.copyTo(textImage, mask);Display or save the result:
cv::imshow("Image with Text", image); // Or textImage if you used a mask
cv::waitKey(0);
// cv::imwrite("path/to/save/image.jpg", image);This C++ code demonstrates how to add text to an image using the OpenCV library. It provides two methods: direct overlay and overlay with a transparency mask. The code loads an image, defines text properties, and then either draws the text directly onto the image or creates a mask for blending the text more seamlessly. Finally, it displays or saves the resulting images.
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
// 1. Load your image
cv::Mat image = cv::imread("path/to/your/image.jpg");
if (image.empty()) {
std::cerr << "Could not read the image!" << std::endl;
return -1;
}
// 2. Define text, position, font, and color
std::string text = "Hello, OpenCV!";
cv::Point textOrg(10, 50);
int fontFace = cv::FONT_HERSHEY_SIMPLEX;
double fontScale = 1;
cv::Scalar color(255, 0, 0); // Blue color
// 3. Use putText() to overlay text (directly on image)
cv::putText(image, text, textOrg, fontFace, fontScale, color, 2);
// 4. (Optional) Create a mask for transparency
cv::Mat mask = cv::Mat::zeros(image.size(), image.type());
cv::putText(mask, text, textOrg, fontFace, fontScale,
cv::Scalar(255, 255, 255), -1); // Filled text
// 5. (Optional) Blend the text with the image using the mask
cv::Mat textImage;
image.copyTo(textImage, mask);
// 6. Display or save the result
cv::imshow("Image with Text (Direct)", image);
cv::imshow("Image with Text (Mask)", textImage);
cv::waitKey(0);
// cv::imwrite("image_with_text.jpg", image);
// cv::imwrite("image_with_text_mask.jpg", textImage);
return 0;
}Explanation:
imread().putText() to draw the text directly onto the image.putText() with a white color and thickness = -1 (filled) to draw the text area on the mask.textImage).copyTo() with the mask to copy only the text area from the original image to textImage.imshow() or save them using imwrite().Remember to:
"path/to/your/image.jpg" with the actual path to your image file.General:
image.empty(). Handle cases where the image path is invalid.CV_8UC3 for color images, CV_8UC1 for grayscale). Ensure the mask type matches the image.Text Parameters:
cv::HersheyFonts enum for varied text appearances.putText() controls the line thickness. Use a negative value (e.g., -1) to create filled text.cv::LINE_AA for anti-aliased lines) as the last argument in putText().Transparency and Blending:
addWeighted() function to blend the text and image using alpha values (0.0 - 1.0).Alternatives and Extensions:
This code snippet demonstrates how to add text to images using the OpenCV library in C++.
Steps:
cv::imread() to load your desired image.cv::putText() to draw the text onto the image.cv::putText().copyTo() with the mask to blend the text onto the original image, creating a transparent effect.cv::imshow() or save it to a file with cv::imwrite().Key Points:
This guide explained how to add text to images using OpenCV in C++. You learned to load images, define text properties, overlay text directly, and create transparent text effects using masks. By following these steps and exploring the additional notes, you can effectively enhance your images with text for various applications.
How to alpha blend with transparency masks - OpenCV Q&A Forum | Sep 2, 2019 ... I know how to copy an image with a mask onto another using cv::Mat::copyTo() . ... cv::putText(mask_layer, text, p, fontface, fontscale ...
how to insert a small size image on to a big image - OpenCV Q&A ... | Jul 18, 2014 ... ,smiley[20]; Mat image; while(fr<=751) { sprintf(name,"images/img ... cv::Mat small_image; cv::Mat big_image; ... //Somehow fill ...
Increase the maximum amount of channels in cv::Mat - OpenCV ... | Nov 3, 2014 ... In my current work, I need to represent an input image (e.g. 200x200 ... cv::Mat variable and initialize it with values loaded from the text file.
Reading Image for Template Matching for Java - OpenCV Q&A Forum | Oct 22, 2014 ... I need someway to convert my images which will be coming from the DB to Mat type for comparison(template matching) ... More info: I have a mysql ...
cv_bridge/Tutorials ... | Apr 20, 2017 ... An example ROS node. Here is a node that listens to a ROS image message topic, converts the image into a cv::Mat, draws a circle on ...
Searching for the easiest way to add white frames to my pictures : r ... | Posted by u/BubblyQuality2618 - 218 votes and 86 comments