在使用以下内容显示图像时在OpenCV中
cvSetWindowProperty("displayCVWindow", CV_WND_PROP_FULLSCREEN,
CV_WINDOW_FULLSCREEN);
如果有人注意到,在完整的屏幕窗口周围有一个小边框。有没有办法摆脱这个?
在全屏模式下显示窗口边框的屏幕截图。 注意:屏幕截图被裁剪为仅显示左上角
答案 0 :(得分:2)
OpenCV不提供此功能。
如果您想让图像处于全屏模式或浮动而没有窗口/边框,您将有两个选择:
如果您决定破解该窗口,可以try this code并替换SetWindowLong()
来电:
SetWindowLong(win_handle, GWL_STYLE, 0;
如果这不起作用,你将不得不深入挖掘Windows上的窗口创建。
答案 1 :(得分:1)
问题实际上不是边框的存在,而是窗口的背景因某种原因而显示出来。根据我的理解,OpenCV的名为Windows实际上创建了两个窗口,一个在另一个窗口内。 "白线"实际上是父窗口的灰色背景。我使用的修复方法是将背景颜色更改为我通过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()
答案 2 :(得分:0)
我遇到了这个问题,发现它很烦人,所以我做了一个小功能,它只是创建一个任意颜色的“框架”,然后将图像放在其中。不幸的是,由于我无法找到一种可靠的跨平台方式来获取显示尺寸,因此您必须自己传递它们。这是代码:
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)