【问题标题】:How to show image in true size in a full screen opencv window?如何在全屏opencv窗口中以真实尺寸显示图像?
【发布时间】:2021-02-06 07:34:35
【问题描述】:

我正在尝试创建一个全屏 opencv 窗口(大小为 1920x1080),该窗口应在 opencv 窗口的中间位置显示图像(620x365)。不幸的是,图像也被扩展到全屏。怎么可能做到这一点?需要设置哪些属性?我使用的是 opencv 2.4.12 版。

import cv2

imgwindow = 'show my image'
image = cv2.imread('./myimage.jpg')
cv2.namedWindow(imgwindow, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(imgwindow, cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_CAP_PROP_FORMAT)
cv2.imshow(imgwindow,image)
cv2.waitKey(0)
cv2.destroyAllWindows()

当我使用以下示例时,我得到一个全屏 opencv 窗口和一个真实大小的图像,但在窗口的左上角而不是中间位置。

import cv2
import win32gui
import win32con
import ctypes

user32 = ctypes.windll.user32
winW, winH = [user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)]

imgwindow = 'show my image'
image = cv2.imread('./myimage.jpg')
cv2.namedWindow(imgwindow, cv2.WINDOW_AUTOSIZE)
cv2.cv.CV_WINDOW_FULLSCREEN)
cv2.imshow(imgwindow,image)
simHndl = win32gui.GetForegroundWindow()
win32gui.MoveWindow(simHndl, 0,0, winW, winH, win32con.TRUE)

cv2.waitKey(0)
cv2.destroyAllWindows()

这是当窗口包含真实图像图片并位于中间时的示例图片:

这是当窗口全屏但图像也扩展到全屏时的示例图片:

这就是我正在寻找的情况:全屏窗口和真实大小的图像放置在中间:

【问题讨论】:

    标签: python-2.7 opencv imshow


    【解决方案1】:

    如果您只使用 cv2.imshow 而不首先使用 cv2.namedWindow 声明它,OpenCV 会以原始大小加载图像。

    因此,通过跳过 cv2.namedWindow,您可以实现以原始大小加载图像的目标。从那里您可以使用 cv2.moveWindow 移动窗口。您所需要的只是计算它的加载位置。下面代码中给出的公式:

    import cv2
    import ctypes
    
    # Get the window size and calculate the center
    user32 = ctypes.windll.user32
    win_x, win_y = [user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)] 
    win_cnt_x, win_cnt_y = [user32.GetSystemMetrics(0)/2, user32.GetSystemMetrics(1)/2] 
    
    # load image
    imgwindow = 'show my image'
    image = cv2.imread('./myimage.jpg')
    
    # Get the image size information
    off_height, off_width = image.shape[:2]
    off_height /= 2
    off_width /= 2
    
    # Show image and move it to center location
    image = cv2.resize(image,(win_x, win_y))
    cv2.imshow(imgwindow,image)
    
    cv2.moveWindow(imgwindow,0,0)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    【讨论】:

    • 感谢您的回答。但是,这不会显示图像位于中间位置的全尺寸窗口。结果是一个与中间位置的图像大小相同的窗口。请参阅我添加到您的帖子中的屏幕打印。
    • 对不起,我误解了你,试试新编辑的代码,看看是不是你想要的。
    • 不,这不是我想要的。现在我得到一个全屏窗口和一个全屏展开图像。
    • 也许你可以说明你在寻找什么......
    • 查看我的编辑。我添加了 3 张图片。第三张图片是我正在寻找的情况。
    【解决方案2】:

    我也有这个要求。 imshow 的行为决定了您在 openCV 中可以做什么。

    案例 1:加载图像并立即显示 - 您在窗口中获得正确大小的图像,其标题栏与图像大小相同

    案例 2:创建了一个命名的全屏窗口并加载图像 - 你得到一个自动缩放到全屏的图像(不知道为什么,肯定有充分的理由)

    案例 3:创建一个命名的全屏窗口并将其大小调整为图像,然后显示图像 - 与没有标题栏的案例 1 相同

    我的“解决方案”是创建一个与全屏大小相同的图像并“添加”居中的较小图像(很简单) 以下是我选择这样做的一个片段:

    # screenx and screeny are size of screen, 1280 by 720 for example
    # image is the image to center on the screen, sized imagex by imagey
    
    # create a black image as big as the screen
    bg_img = np.zeros((screeny, screenx, 3), np.uint8)
    bg_img[:, 0:screeny] = (0, 0, 0)
    # add a white border to define the region
    cv2.rectangle(bg_img, (0, 0), (screenx-2, screeny-2), color=(255, 255, 255), thickness=1)
    # calculate the offsets required to center the image
    x_offset = (screenx - imagex)//2
    y_offset = (screeny - imagey)//2
    # insert the samller image into the larger image, centered
    bg_image[y_offset:y_offset+image.shape[0], x_offset:x_offset+image.shape[1]] = image
    

    PS - 我在 Win10 中发现缩放和布局的显示设置会显着影响屏幕分辨率...请注意这一点。在 100% 时,您可能有 2560x1440,但在 150% 时,您会得到 1707x960。

    【讨论】:

    • 顺便说一句,您也可以使用 pygame 来执行此操作。它适用于 openCV。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 2014-11-19
    • 2013-05-05
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多