Overlay text on the image
Peponi │ 2/11/2025 │ 2m
C#
NugetPackageOpenCvSharp4
Overlay text on the image
2/11/2025
2m
Peponi
C#
NugetPackageOpenCvSharp4
1. Introduction
Cv2
.PutText()
를 이용해 이미지 또는 비디오에 글자를 출력할 수 있다. 좌측 상단 또는 좌측 하단을 원점으로 하여 원하는 위치에 글자를 출력하며 폰트, 크기 등을 취향에 맞게 선택할 수 있다.
2. Example
using OpenCvSharp;
namespace RenderingText;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var load = new Button()
{
Text = "LOAD",
Dock = DockStyle.Fill,
};
load.Click += Load_Click;
this.Controls.Add(load);
}
private void Load_Click(object? sender, EventArgs e)
{
OpenFileDialog dialog = new()
{
Filter = "All files|*.*",
InitialDirectory = $@"C:\",
CheckPathExists = true,
CheckFileExists = true
};
if (dialog.ShowDialog() == DialogResult.OK)
{
// Image load
using var image = Cv2.ImRead(dialog.FileName);
Cv2.PutText(image, dialog.SafeFileName, new(10, 70), HersheyFonts.HersheySimplex, 3, Scalar.MediumSeaGreen);
Cv2.ImShow(dialog.FileName, image);
}
}
}
3. Cv2.PutText
/// <summary>
/// renders text string in the image
/// </summary>
/// <param name="img">Image.</param>
/// <param name="text">Text string to be drawn.</param>
/// <param name="org">Bottom-left corner of the text string in the image.</param>
/// <param name="fontFace">Font type, see #HersheyFonts.</param>
/// <param name="fontScale">Font scale factor that is multiplied by the font-specific base size.</param>
/// <param name="color">Text color.</param>
/// <param name="thickness">Thickness of the lines used to draw a text.</param>
/// <param name="lineType">Line type. See #LineTypes</param>
/// <param name="bottomLeftOrigin">When true, the image data origin is at the bottom-left corner.
/// Otherwise, it is at the top-left corner.</param>
public static void PutText(InputOutputArray img, string text, Point org,
HersheyFonts fontFace, double fontScale, Scalar color,
int thickness = 1, LineTypes lineType = LineTypes.Link8, bool bottomLeftOrigin = false)
{
if (img == null)
throw new ArgumentNullException(nameof(img));
if (string.IsNullOrEmpty(text))
throw new ArgumentNullException(text);
img.ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.imgproc_putText(img.CvPtr, text, org, (int) fontFace, fontScale, color,
thickness, (int) lineType, bottomLeftOrigin ? 1 : 0));
img.Fix();
GC.KeepAlive(img);
}
HersheyFonts | Value | Description |
---|---|---|
HersheySimplex | 0 | normal size sans-serif font |
HersheyPlain | 1 | small size sans-serif font |
HersheyDuplex | 2 | normal size sans-serif font (more complex than HERSHEY_SIMPLEX) |
HersheyComplex | 3 | normal size serif font |
HersheyTriplex | 4 | normal size serif font (more complex than HERSHEY_COMPLEX) |
HersheyComplexSmall | 5 | smaller version of HERSHEY_COMPLEX |
HersheyScriptSimplex | 6 | hand-writing style font |
HersheyScriptComplex | 7 | more complex variant of HERSHEY_SCRIPT_SIMPLEX |
Italic | 16 | flag for italic font |
LineTypes | Value | Description |
---|---|---|
Link8 | 8 | 8-connected line. |
Link4 | 4 | 4-connected line. |
AntiAlias | 16 | Anti-aliased line. |