當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python OpenCV moveWindow()用法及代碼示例

當我們使用imshow()函數顯示圖像時,輸出窗口將在計算機屏幕的中心或默認位置打開。即使有多個圖像窗口,所有窗口也會顯示在同一位置,我們必須手動移動窗口。如果我們想在特定位置顯示圖像窗口moveWindow(),OpenCV的函數就可以做到。

用法:cv2.moveWindow(window_Name,x,y)

參數:

  • window_name:要移動到特定位置的窗口的名稱
  • x:x坐標的值
  • y:y坐標值

返回:None

示例 1:具有特定位置的圖像

在此示例中,我們將在特定位置僅顯示一個窗口。

使用的圖像:

代碼:

Python3


# Import OpenCV library 
import cv2 
  
# Read an Image 
img = cv2.imread("Documents/geekslogo.png", 
                 cv2.IMREAD_COLOR) 
  
# Display image using imshow() function 
cv2.imshow("I2", img) 
  
# Move window to (10,50) position 
# using moveWindow() function 
cv2.moveWindow("I2", 10, 50) 
  
# Wait for user to press any key 
cv2.waitKey(0) 
  
# Close all opened windows 
cv2.destroyAllWindows() 

輸出:

示例 2:不同位置的多個圖像

在此示例中,我們將在特定位置顯示多個窗口。

使用的圖像:

Python3


# Import OpenCV library 
import cv2 
  
# Read four Images 
img1 = cv2.imread("Documents/geekslogo.png", cv2.IMREAD_COLOR) 
img2 = cv2.imread("Documents/geekslogo2.png", cv2.IMREAD_COLOR) 
img3 = cv2.imread("Documents/geekslogo3.png", cv2.IMREAD_COLOR) 
img4 = cv2.imread("Documents/geekslogo4.png", cv2.IMREAD_COLOR) 
  
  
# Display images using imshow() function 
cv2.imshow("I1", img1) 
cv2.imshow("I2", img2) 
cv2.imshow("I3", img3) 
cv2.imshow("I4", img4) 
  
# Move window to (10,50) position 
# using moveWindow() function 
cv2.moveWindow("I1", 10, 50) 
cv2.moveWindow("I2", 650, 50) 
cv2.moveWindow("I3", 10, 500) 
cv2.moveWindow("I4", 650, 500) 
  
# Wait for user to press any key 
cv2.waitKey(0) 
  
# Close all opened windows 
cv2.destroyAllWindows() 

輸出:



相關用法


注:本文由純淨天空篩選整理自patildhanu4111999大神的英文原創作品 Python OpenCV – moveWindow() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。