【问题标题】:Why is cv2 showing a blank grey square?为什么 cv2 显示一个空白的灰色方块?
【发布时间】:2021-10-08 11:44:02
【问题描述】:

我有一个使用 cv2 库的 python 程序,它选择一个随机的美国州,从文件夹中提取该州的图像并显示它。它工作正常,所以我保存了它,突然间,它没有显示图像,而是显示一个空白的灰色方块并崩溃。为什么会这样,我该如何解决?下面是示例图像: sample image second sample image 这是代码:

import cv2
import random
import time

#makes a list of the states
def states():
    states = ['alabama','alaska','arizona','arkansas','california',
    'colorado','connecticut','deleware','florida','georgia','hawaii',
    'idaho','illinois','indiana','iowa','kansas','kentucky','louisiana',
    'maine','maryland','massachussets','michigan','minnesota',
    'mississipi','missouri','montana','nebraska','nevada',
    'new_hampshire','new_jersey','new_mexico','new_york',
    'north_carolina','north_dakota','ohio','oklahoma','oregon',
    'pennsylvania','rhode_island','south_carolina','south_dakota',
    'tennessee','texas','utah','vermont','virginia','washington',
    'west_virginia','wisconsin','wyoming']
    while True:
        #choose a random state from the states list
        state = random.choice(states)
        #take a picture from the states folder, and display it
        img = cv2.imread('picture_files/states/' + state + '.png') 
        cv2.imshow('guess the state!', img)
        #checks if you typed the right state,
        #and gives an appropriate response
        guess = input("guess the state! (type stop to stop!)\n")
        if guess.lower() == state:
            print("Correct!")
            time.sleep(2)
            print("Lets do it again!")
        elif guess.lower() == "stop":
            break
        else:
            print("Nope! It was " + state + ". Keep trying!")
            time.sleep(2)

if __name__ == '__main__':
    states()

【问题讨论】:

  • 我不明白你提到的“崩溃”。它没有响应吗?还是只是无限循环?
  • Rizquuula,当我运行程序时它显示正方形,我的光标显示加载符号,然后它说“python没有响应”,控制台显示“程序退出,代码:-805306369”
  • 以下链接可能对您有所帮助stackoverflow.com/questions/22274789/…
  • 尝试在您的cv2.imshow() 下方添加cv2.waitKey(0)。它会显示您的图像,但会再次开始not responding。如果您需要同时运行这两个进程,请使用线程。 realpython.com/intro-to-python-threading

标签: python cv2


【解决方案1】:

我之前遇到过这样的错误,opencv 无法打开,并且已经通过几种方法成功解决。

  1. 在函数末尾添加cv2.waitKey(0),然后添加 cv2.destroyAllWindows() 在你的电话下面states() 像这样:

    if __name__ == '__main__':
        states()
        cv2.destroyAllWindows()
    
  2. 安装opencv-contrib-python

    尝试使用命令pip3 install opencv-contrib-python==4.4.0.46 安装它。

【讨论】:

    【解决方案2】:

    基本上,您缺少一些 cv2.waitKey() 函数来显示图像(另请参阅 here)。

    这是一个可能的解决方案示例。

    def pick_a_state():
        states = ['a','b'] # replace with your list
        return random.choice(states)
    
    def show_state(state):
        img = cv2.imread(state + '.png', cv2.IMREAD_UNCHANGED)
        cv2.imshow('guess the state!', img)
        cv2.waitKey(1000)
    
    def get_the_answer(state):
        guess = raw_input("guess the state! (type stop to stop!)\n")
        if guess.lower() == state:
            print(state)
            print("Correct!")
            time.sleep(2)
            print("Lets do it again!")
            return 1
        elif guess.lower() == "stop":
            return 0
        else:
            print("Nope! It was " + state + ". Keep trying!")
            time.sleep(2)
            return 1
    
    if __name__ == '__main__':
        while True:
            state = pick_a_state()
            show_state(state)
            if get_the_answer(state) == 0:
                cv2.destroyAllWindows()
                break
    

    请分析代码以了解其工作原理。希望能帮上忙。

    【讨论】: