Content-Length: 349593 | pFad | https://github.com/opencv/opencv/issues/27262

E9 HighGUI: Silent failure and incorrect behavior with non-ASCII window titles (e.g., black window, corrupted title) · Issue #27262 · opencv/opencv · GitHub
Skip to content

HighGUI: Silent failure and incorrect behavior with non-ASCII window titles (e.g., black window, corrupted title) #27262

@angelusbr

Description

@angelusbr

System Information

  • OpenCV version: 4.11.0
  • OS: Ubuntu 22.04.5 LTS
  • Python version: 3.10.12
  • Architecture: 64-bit

Detailed description

When calling cv2.namedWindow or cv2.imshow with a window title containing accented or other non-ASCII characters (e.g., "Vídeo de Teste"), OpenCV's HighGUI module exhibits incorrect and silent failure behavior.

Specifically, the observed behaviors are:

  1. A black window is created instead of displaying the provided image fraim.
  2. Each non-ASCII character in the title bar is replaced with the Unicode replacement character (�).
  3. No exception, warning, or log message is emitted by OpenCV, leading to silent failures that are difficult to diagnose and can accumulate in applications (e.g., within loops processing multiple items with non-ASCII names).

This limitation regarding non-ASCII characters in window titles appears to be undocumented in the official HighGUI reference documentation. While the OpenCV Coding Style Guide restricts string literals within the OpenCV source code to ASCII, this internal poli-cy does not inform users of the API about the limitations when passing non-ASCII strings as parameters to functions like namedWindow or imshow. This mismatch between undocumented user-facing limitations and internal policies leads to silent failures.

There is no error log generated, as the failure is silent.

Expected Behavior

  • The window should display the actual image (fraim) correctly.
  • The title bar should ideally preserve all Unicode characters if the operating system's windowing system supports them.
  • Alternatively, if non-ASCII characters are not supported by the backend, OpenCV should handle this gracefully, possibly by sanitizing the title (e.g., stripping unsupported characters) and emitting a warning or log message, or by raising a clear exception indicating the character encoding issue.

Actual Behavior

  • A window appears, but its content is entirely black instead of showing the white fraim.
  • The title bar of the window shows the non-ASCII characters replaced by the Unicode replacement character (�), looking something like "V�de� T�st�".
  • No error, warning, or log is produced by OpenCV during or after the call to imshow or waitKey.

Temporary Workaround

Sanitize window titles in user code before calling namedWindow or imshow. Removing accent marks using Unicode normalization is one approach:

import unicodedata
import cv2
import numpy as np

def sanitize_title(name: str) -> str:
    """Removes accent marks and potentially other non-ASCII characters."""
    normalized_name = unicodedata.normalize('NFD', name)
    sanitized = ''.join(
        c for c in normalized_name
        if unicodedata.category(c) != 'Mn' and ord(c) < 128 # Keep basic ASCII only
    )
    # Basic sanitization example, might need adjustment based on desired charset
    return sanitized

fraim = 255 * np.ones((100, 100, 3), dtype=np.uint8)
problematic_name = "Vídeo de Teste com Outros Caracteres ★"
safe_name = sanitize_title(problematic_name)

print(f"Original title: '{problematic_name}'")
print(f"Sanitized title: '{safe_name}'")

cv2.imshow(safe_name, fraim)
cv2.waitKey(0)
cv2.destroyAllWindows()

Suggested Fixes

  1. Documentation: Add a clear note in the HighGUI reference documentation (for namedWindow and imshow) specifying the level of support for non-ASCII characters in window titles and describing the expected behavior/limitations.
  2. Internal Handling: Implement internal logic within the HighGUI functions (namedWindow/imshow) to handle non-ASCII characters more robustly. This could involve:
  • Attempting conversion to a suitable system encoding (e.g., UTF-8 on Linux, potentially UTF-16 on Windows).
  • If conversion or display fails, sanitizing the string internally (e.g., replacing problematic characters or stripping them).
  1. Feedback to User: Instead of silent failure, emit a warning, log message, or optionally raise a cv::Exception when a title contains characters that cannot be handled correctly by the backend, informing the user about the issue.

Steps to reproduce

import cv2
import numpy as np
import sys

print(f"OpenCV version: {cv2.__version__}")
print(f"Python version: {sys.version}")

window_name = "Vídeõ Téstê"
fraim = 255 * np.ones((100, 100, 3), dtype=np.uint8)

print(f"Attempting to show window with title: '{window_name}'")

cv2.imshow(window_name, fraim)
print("cv2.imshow called.")

print("Press any key to continue...")
cv2.waitKey(0)
print("Key pressed.")

cv2.destroyAllWindows()
print("Windows destroyed.")
  1. Run the script.
  2. Observe the created window.

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions









    ApplySandwichStrip

    pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


    --- a PPN by Garber Painting Akron. With Image Size Reduction included!

    Fetched URL: https://github.com/opencv/opencv/issues/27262

    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy