本文將討論如何使用pythonOpenCV startWindowThread()函數。
您想通過OpenCV代碼使用簡化的接口顯示圖像和視頻嗎?然後,您必須查看 OpenCV startWindowsThread() 函數,它可以讓您使用高級 GUI 窗口,即用於顯示來自 OpenCV 代碼的圖像和視頻的簡化接口。
用法:cv2.startWindowThread()
參數:None
返回值:它不返回任何內容。
逐步實施:
步驟1:首先,導入OpenCV庫:
這裏,我們導入cv2庫,cv2是OpenCV包,幫助我們分別調用imread()、startWindowThread()、namedWindow()和imshow()函數。
import cv2
第2步:現在,使用imread()函數讀取圖像:
在此步驟中,我們使用了 imread() 函數從指定文件加載圖像。
img = cv2.imread(path, flag)
- path:待讀取圖像的路徑。
- flag:讀取圖像的方式。該標誌的默認值為 cv2.IMREAD_COLOR。
步驟3:然後,調用startWindowThread()函數來使用高級GUI窗口:
在此步驟中,我們使用了 startWindowThread() 函數,該函數顯示用於顯示 OpenCV 代碼中的圖像和視頻的簡化接口。
cv2.startWindowThread()
步驟4:接下來,為您的 GUI 應用程序指定名稱和大小:
在此步驟中,我們使用了 namedWindow() 函數,該函數用於創建一個具有合適名稱和大小的窗口,以在屏幕上顯示圖像和視頻。
cv2.namedWindow(window_name,prop_value)
- window_name:窗口的名稱。
- prop_value:窗口屬性的新值,例如 cv2.WND_PROP_FULLSCREEN、cv2.WND_PROP_AUTOSIZE、cv2.WND_PROP_ASPECT_RATIO 等。
第5步:此外,在 GUI 應用程序上顯示圖像:
在此步驟中,我們使用了 imshow() 函數,該函數用於在窗口中顯示圖像。窗口自動適應圖像尺寸。
cv2.imshow(window_name, image)
- window_name:表示要在其中顯示圖像的窗口的名稱的字符串。
- image:這是要顯示的圖像。
第6步:最後,讓 python 無限時間休眠:
在此步驟中,我們使用了 waitKey() 函數,該函數允許用戶在指定時間或按下任意鍵之前顯示窗口。
cv2.waitKey(0)
例子:
在此示例中,我們使用了圖像“gfg_black.png”(link)。我們還使用 startWindowThread() 函數作為顯示 OpenCV 代碼中的圖像和視頻的簡化接口。
Python3
# Python program for OpenCV
# startWindowThread() function
# Import the library OpenCV
import cv2
# Read the image
img = cv2.imread("gfg_black.png")
# Use high GUI windows
cv2.startWindowThread()
# Name the GUI app
cv2.namedWindow("preview", cv2.WINDOW_NORMAL)
# Display the image on GUI app
cv2.imshow("preview", img)
# Make Python sleep for unlimited time
cv2.waitKey(0)
輸出:

相關用法
- Python OpenCV selectroi()用法及代碼示例
- Python OpenCV setTrackbarPos()用法及代碼示例
- Python OpenCV setWindowTitle()用法及代碼示例
- Python OpenCV setWindowProperty()用法及代碼示例
- Python OpenCV cv2.circle()用法及代碼示例
- Python OpenCV cv2.blur()用法及代碼示例
- Python OpenCV cv2.ellipse()用法及代碼示例
- Python OpenCV cv2.cvtColor()用法及代碼示例
- Python OpenCV cv2.copyMakeBorder()用法及代碼示例
- Python OpenCV cv2.imread()用法及代碼示例
- Python OpenCV cv2.imshow()用法及代碼示例
- Python OpenCV cv2.imwrite()用法及代碼示例
- Python OpenCV cv2.putText()用法及代碼示例
- Python OpenCV cv2.rectangle()用法及代碼示例
- Python OpenCV cv2.arrowedLine()用法及代碼示例
- Python OpenCV cv2.erode()用法及代碼示例
- Python OpenCV cv2.line()用法及代碼示例
- Python OpenCV cv2.flip()用法及代碼示例
- Python OpenCV cv2.transpose()用法及代碼示例
- Python OpenCV cv2.rotate()用法及代碼示例
- Python OpenCV cv2.polylines()用法及代碼示例
- Python OpenCV Canny()用法及代碼示例
- Python OpenCV destroyAllWindows()用法及代碼示例
- Python OpenCV Filter2D()用法及代碼示例
- Python OpenCV getgaussiankernel()用法及代碼示例
注:本文由純淨天空篩選整理自vin8rai大神的英文原創作品 Python OpenCV – startWindowThread() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。