首页 > 解决方案 > imshow 仅适用于外部范围

问题描述

我正在尝试显示代码每个阶段的输出,但 imshow 似乎不适用于内部函数

我用一个 imshow 行创建了一个空函数来测试它,它仍然没有显示给它的实际图像,而是显示一个充满 0 的小图像

另一个观察结果是,它在循环结束时将每个循环图像显示在一个名为“测试”和“原始图像”的单独窗口中,您有 n*2 个窗口(这两个名称重复),其中 n 是图像的数量

def test_(clr_img):
    cv2.imshow("test image", clr_img)

def loop_folder(folder_path, save_path, debug=True):
    """Applies the label_chars function to every image in a folder and saves at the save_path
    """
    json_name_list = []
    for file_name in listdir(folder_path):
        if file_name.endswith(".json"):
            json_name_list.append(file_name)

    for json_file_name in tqdm(json_name_list):
        json_path = path.join(folder_path, json_file_name)
        with open(json_path) as json_file_r:
            json_data = json.load(json_file_r)
            image_file_path = json_path.replace(".json", ".png")
            image = cv2.imread(image_file_path)

            if debug:
                img = image.copy()
                cv2.imshow("original image", image) # this one shows everytime
                test_(image) # this one sometimes shows 
            
        cv2.waitKey(0)
    cv2.destroyAllWindows()

我正在使用 opencv 4.3.0.36 和 python 3.7

标签: pythonopencv

解决方案


所以问题是opencv本身,它可以在其他PC上运行,所以我尝试重新安装opencv,现在它可以正常工作了


推荐阅读