基本閱讀和顯示影象

import cv2

image_path= #put your image path here

#use imread() function to read image data to variable img.
img = cv2.imread(image_path) 

#display image data in a new window with title 'I am an image display window'
cv2.imshow('I am an image display window',img) 

#wait until user hits any key on keyboard
cv2.waitKey(0) 

#close any windows opened by opencv
cv2.destroyAllWindows() 

要控制螢幕上顯示視窗的大小,請在 cv2.imshow 命令之前新增以下命令:

window_width=800 #size of the display window on the screen
window_height=600

#open an empty window with a title.
#The flag cv2.WINDOW_NORMAL allows the window to be scaleable.
cv2.namedWindow('I am an image display window', cv2.WINDOW_NORMAL)

#scale the image display window to desired size
cv2.resizeWindow('I am an image display window', window_width, window_height)

有關詳細資訊,請參閱 openCV 文件