全屏且无边框的 OpenCV 窗口

发布于 2025-01-08 19:37:19 字数 330 浏览 0 评论 0原文

在 OpenCV 中显示图像时:

cvSetWindowProperty("displayCVWindow", CV_WND_PROP_FULLSCREEN, 
CV_WINDOW_FULLSCREEN);

如果有人注意到,全屏窗口周围有一个小边框。有办法摆脱这个吗?

显示全屏模式下窗口边框的屏幕截图。 注意:屏幕截图被裁剪为仅显示左上角

完整时显示窗口边框的屏幕截图**注意**:屏幕截图被裁剪为仅显示左上角

In OpenCV when displaying an image with:

cvSetWindowProperty("displayCVWindow", CV_WND_PROP_FULLSCREEN, 
CV_WINDOW_FULLSCREEN);

There is a small border around the full screened window if anyone ever noticed. Is there a way to get a rid of this?

Screenshot showing border of window when in full screen mode. Note: the screenshot was cropped to show only top-left corner

Screenshot showing border of window when in full screen mode. **Note**: the screenshot was cropped to show only top-left corner

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苄①跕圉湢 2025-01-15 19:37:19

OpenCV 不提供此功能

如果您想让图像处于全屏模式或在没有窗口/边框的情况下浮动,您将有 2 个选择:

如果您决定破解窗口,您可以尝试此代码并替换SetWindowLong()呼吁:

SetWindowLong(win_handle, GWL_STYLE, 0;

如果这不起作用,您将不得不更深入地了解 Windows 上的窗口创建。

OpenCV does not provide this capability.

If you want to have the image in fullscreen mode or floating around without window/borders you will have 2 choices:

If you decide to hack the window, you may try this code and replace the SetWindowLong() call for:

SetWindowLong(win_handle, GWL_STYLE, 0;

If that doesn't work, you'll have to dig a little deeper into window creation on Windows.

Oo萌小芽oO 2025-01-15 19:37:19

问题实际上不在于边框的存在,而是窗口的背景由于某种原因而显示出来。据我了解,OpenCV的namedWindow实际上创建了两个窗口,一个在另一个窗口内。 “白线”实际上是父窗口的灰色背景。我使用的修复方法是将背景颜色更改为我通过 Windows API 显示的 Mat 的颜色。

这是我用来修复它的代码:

cv::namedWindow("mainWin", WINDOW_NORMAL);//create new window
cv::setWindowProperty("mainWin",CV_WND_PROP_FULLSCREEN,CV_WINDOW_FULLSCREEN);//set fullscreen property
HWND hwnd = FindWindow(0, L"mainWin");//get window through Windows API
SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND, (LONG) CreateSolidBrush(RGB(0, 0, 0)));//set window background to black; you can change the colour in the RGB()

The problem is actually not the presence of a border, but the window's background showing through for some reason. From what I understand, OpenCV's namedWindow actually creates a two windows, one inside the other. The "white lines" are actually the grey background of the parent window. The fix I used was to change the background colour to the colour of the Mat I was displaying through the Windows API.

Here's the code I used to fix it:

cv::namedWindow("mainWin", WINDOW_NORMAL);//create new window
cv::setWindowProperty("mainWin",CV_WND_PROP_FULLSCREEN,CV_WINDOW_FULLSCREEN);//set fullscreen property
HWND hwnd = FindWindow(0, L"mainWin");//get window through Windows API
SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND, (LONG) CreateSolidBrush(RGB(0, 0, 0)));//set window background to black; you can change the colour in the RGB()
滿滿的愛 2025-01-15 19:37:19

我遇到了这个问题,发现它很烦人,所以我做了一个小函数,它只是创建一个你喜欢的任何颜色的“框架”并将图像放在那里。不幸的是,由于我找不到可靠的跨平台方法来获取显示尺寸,因此您必须自己传递它们。这是代码:

import cv2
import numpy as np


_FULL_FRAMES = {}


def show_fullscreen(image, background_colour = None, window_name='window', display_number = 0, display_sizes=None):
    """
    Draw a fullscreen image.

    :param image: The image to show.
        If integer, it will be assumed to be in range [0..255]
        If float, it will be assumed to be in range [0, 1]
    :param background_colour: The background colour, as a BGR tuple.
    :param window_name: Name of the window (can be used to draw multiple fullscreen windows)
    :param display_number: Which monitor to display to.
    :param display_sizes: Size of displays (needed only if adding a background colour)
    """
    if image.dtype=='float':
        image = (image*255.999).astype(np.uint8)
    else:
        image = image.astype(np.uint8, copy=False)
    if image.ndim==2:
        image = image[:, :, None]

    assert display_number in (0, 1), 'Only 2 displays supported for now.'
    if window_name not in _FULL_FRAMES:
        cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
        if display_number == 1:
            assert display_sizes is not None
            first_display_size = display_sizes[0]
            cv2.moveWindow(window_name, *first_display_size)
        cv2.setWindowProperty(window_name,cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
        if background_colour is not None:
            background_colour = np.array(background_colour)
            if background_colour.dtype=='int':
                background_colour = background_colour.astype(np.uint8)
            else:
                background_colour = (background_colour*255.999).astype(np.uint8)
            assert display_sizes is not None, "Unfortunately, if you want to specify background color you need to specify display sizes."
            pic_display_size = display_sizes[display_number]
            aspect_ratio = pic_display_size[1]/float(pic_display_size[0])  # (hori/vert)
            frame_size_x = int(max(image.shape[0]/aspect_ratio, image.shape[1]))
            frame_size_y = int(max(image.shape[1]*aspect_ratio, image.shape[0]))
            _FULL_FRAMES[window_name] = np.zeros((frame_size_y, frame_size_x, 3), dtype=np.uint8) + background_colour
        else:
            _FULL_FRAMES[window_name] = None

    if _FULL_FRAMES[window_name] is not None:
        frame = _FULL_FRAMES[window_name]
        start_y, start_x = (frame.shape[0] - image.shape[0])//2, (frame.shape[1] - image.shape[1])//2
        frame[start_y: start_y+image.shape[0], start_x:start_x+image.shape[1]] = image
        display_img = frame
    else:
        display_img = image

    cv2.imshow(window_name, display_img)
    cv2.waitKey(1)


if __name__ == '__main__':
    for t in np.linspace(0, 10, 1000):
        im = np.sin(-4*t+np.sin(t/4.)*sum(xi**2 for xi in np.meshgrid(*[np.linspace(-20, 20, 480)]*2)))*.5+.5
        show_fullscreen(im, background_colour=(0, 0, 0), display_sizes=[(1440, 900), (1920, 1080)], display_number=0)  #
        # show_fullscreen(im, background_colour=None, display_number=0)

I ran into this and found it annoying, so I make a small function which just creates a "frame" of whatever colour you like and puts the image in there. Unfortunately, since I counldn't find a reliable cross-platform way to get display-sizes, you have to pass them yourself. Here's the code:

import cv2
import numpy as np


_FULL_FRAMES = {}


def show_fullscreen(image, background_colour = None, window_name='window', display_number = 0, display_sizes=None):
    """
    Draw a fullscreen image.

    :param image: The image to show.
        If integer, it will be assumed to be in range [0..255]
        If float, it will be assumed to be in range [0, 1]
    :param background_colour: The background colour, as a BGR tuple.
    :param window_name: Name of the window (can be used to draw multiple fullscreen windows)
    :param display_number: Which monitor to display to.
    :param display_sizes: Size of displays (needed only if adding a background colour)
    """
    if image.dtype=='float':
        image = (image*255.999).astype(np.uint8)
    else:
        image = image.astype(np.uint8, copy=False)
    if image.ndim==2:
        image = image[:, :, None]

    assert display_number in (0, 1), 'Only 2 displays supported for now.'
    if window_name not in _FULL_FRAMES:
        cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
        if display_number == 1:
            assert display_sizes is not None
            first_display_size = display_sizes[0]
            cv2.moveWindow(window_name, *first_display_size)
        cv2.setWindowProperty(window_name,cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
        if background_colour is not None:
            background_colour = np.array(background_colour)
            if background_colour.dtype=='int':
                background_colour = background_colour.astype(np.uint8)
            else:
                background_colour = (background_colour*255.999).astype(np.uint8)
            assert display_sizes is not None, "Unfortunately, if you want to specify background color you need to specify display sizes."
            pic_display_size = display_sizes[display_number]
            aspect_ratio = pic_display_size[1]/float(pic_display_size[0])  # (hori/vert)
            frame_size_x = int(max(image.shape[0]/aspect_ratio, image.shape[1]))
            frame_size_y = int(max(image.shape[1]*aspect_ratio, image.shape[0]))
            _FULL_FRAMES[window_name] = np.zeros((frame_size_y, frame_size_x, 3), dtype=np.uint8) + background_colour
        else:
            _FULL_FRAMES[window_name] = None

    if _FULL_FRAMES[window_name] is not None:
        frame = _FULL_FRAMES[window_name]
        start_y, start_x = (frame.shape[0] - image.shape[0])//2, (frame.shape[1] - image.shape[1])//2
        frame[start_y: start_y+image.shape[0], start_x:start_x+image.shape[1]] = image
        display_img = frame
    else:
        display_img = image

    cv2.imshow(window_name, display_img)
    cv2.waitKey(1)


if __name__ == '__main__':
    for t in np.linspace(0, 10, 1000):
        im = np.sin(-4*t+np.sin(t/4.)*sum(xi**2 for xi in np.meshgrid(*[np.linspace(-20, 20, 480)]*2)))*.5+.5
        show_fullscreen(im, background_colour=(0, 0, 0), display_sizes=[(1440, 900), (1920, 1080)], display_number=0)  #
        # show_fullscreen(im, background_colour=None, display_number=0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文