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 color
Use 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.