问题
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()
回答1:
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.
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)
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']
来源:https://stackoverflow.com/questions/34068092/opencv-cv2-imshow-automatically-add-padding-to-small-image