opencv cv2.imshow自动将填充添加到小图像 [英] opencv cv2.imshow automatically add padding to small image
问题描述
我尝试使用cv2.imshow()显示图像,但是我发现结果自动用空白填充.我使用的代码如下:
I try to use cv2.imshow() to display images, however I find that the result is padded with blank automatically. The code I use is as following:
import cv2
if __name__ == '__main__':
img = cv2.imread('test2.png')
cv2.imshow('dafasf', img)
key = cv2.waitKey(0)
if key == 27: # ESC
cv2.destroyAllWindows()
推荐答案
默认情况下,imshow
创建具有WINDOW_AUTOSIZE
属性的窗口对象.这意味着窗口大小由 OpenCV 编译时使用的某些前端(GUI)决定.
By default imshow
creates window object with WINDOW_AUTOSIZE
attribute. It means that window size is determined by some frontend (GUI) which OpenCV was compiled with.
您的图片实际上没有更改,只是显示问题.
Your image are not actually changed, it is just displaying issue.
如果要手动控制窗口属性,可以尝试在调用imshow
之前使用属性创建namedWindow
. e.像这样的东西:
If you want to manually control window attributes, you can try to create namedWindow
with your attributes before imshow
call, i. e. something like this:
cv2.namedWindow("window name", cv2.WINDOW_NORMAL)
cv2.imshow("window name", img)
根据文档,窗口显示有3个属性(WINDOW_NORMAL
,WINDOW_AUTOSIZE
和WINDOW_OPENGL
),但是通过以下简单代码段,您可以确定实际支持的值(在我的版本中,我看到WINDOW_FREERATIO
,WINDOW_FULLSCREEN
,WINDOW_AUTOSIZE
,WINDOW_NORMAL
,WINDOW_KEEPRATIO
和WINDOW_OPENGL
):
According to documentation there are 3 attributes for window displaying (WINDOW_NORMAL
, WINDOW_AUTOSIZE
and WINDOW_OPENGL
), but with the following simple snippet you can determine your actually supported values (in my version I see WINDOW_FREERATIO
, WINDOW_FULLSCREEN
, WINDOW_AUTOSIZE
, WINDOW_NORMAL
, WINDOW_KEEPRATIO
and WINDOW_OPENGL
):
[i for i in list(cv2.__dict__.keys()) if i[:6] == 'WINDOW']
这篇关于opencv cv2.imshow自动将填充添加到小图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!