Favicon

Overlay text on the image

Peponi2/11/20252m

C#
NugetPackageOpenCvSharp4

1. Introduction

text on image

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);
}
HersheyFontsValueDescription
HersheySimplex0normal size sans-serif font
HersheyPlain1small size sans-serif font
HersheyDuplex2normal size sans-serif font (more complex than HERSHEY_SIMPLEX)
HersheyComplex3normal size serif font
HersheyTriplex4normal size serif font (more complex than HERSHEY_COMPLEX)
HersheyComplexSmall5smaller version of HERSHEY_COMPLEX
HersheyScriptSimplex6hand-writing style font
HersheyScriptComplex7more complex variant of HERSHEY_SCRIPT_SIMPLEX
Italic16flag for italic font
LineTypesValueDescription
Link888-connected line.
Link444-connected line.
AntiAlias16Anti-aliased line.

4. 참조 자료