[英]cv2.imshow() function does not show the image if user input is taken, but works otherwise
我正在編寫代碼以在 500x500 白色圖像上生成用戶定義的網格。 程序應要求用戶輸入網格大小並相應地繪制垂直和水平線,最后顯示圖像。
我面臨的問題是,每當我調用“getGridSize()”函數時,圖像都不會顯示。 但是當我手動為變量“num”賦值時,圖像顯示有效
我也嘗試過使用 cv2.waitKey(0) 和 cv2.destroyAllWindows() 但徒勞無功。
import cv2
import numpy as np
width = 500 # Dimensions of the blank, white image
def main():
## getGridSize()
num = 4
generateWhiteImg()
lineImg = whiteImg.copy() # Create a copy of the white image to draw upon
for i in range(0, num):
spacing = int(width / (num + 1)) # Specify the width between each line, for uniform placement
point = (i + 1) * spacing # Change the coordinate of the lines to be drawn by fixed amounts
lineImg = cv2.line(lineImg, (0, point), (width, point), (0, 0, 0), 3) # Draw horizontal lines
lineImg = cv2.line(lineImg, (point, 0), (point, width), (0, 0, 0), 3) # Draw vertical lines
cv2.imshow('LINES', lineImg)
## Function to get grid size from user
def getGridSize():
global num # Stores the size of grid to be drawn
num = input("Enter the size of blank grid: ")
num = int(num) # To convert string to integer
## Function to generate a blank, white image
def generateWhiteImg():
global whiteImg
img = np.zeros((width, width, 3), np.uint8) # Generate a zeros list, i.e., a black image
img[:width] = (255, 255, 255) # Convert black image to white
cv2.imwrite('whiteImg.png', img) # Save the white image
whiteImg = cv2.imread('whiteImg.png') # Load the same from directory
if __name__ == "__main__":
main()
沒有錯誤消息。 我希望代碼按原樣工作。 我認為問題出在 getGridSize() 函數內
更新 1:我為此使用 python 3.5.4
當你用 OpenCV 做 GUI 的事情時,你總是需要在之后調用cv2.waitKey()
方法。 它確保顯示窗口並確定窗口將保持打開狀態的時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.