预处理期间无法打开/读取文件

问题描述 投票:0回答:2
import os
import cv2
import numpy as np 

def preprocess_image(image):
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    image = cv2.bitwise_not(image)
    kernel = np.ones((30, 30), np.uint8)
    tophat = cv2.morphologyEx(image, cv2.MORPH_TOPHAT, kernel)

    g_kernel = cv2.getGaborKernel((350, 350), 8.0, np.pi/4, 10.0, 0.5, 0, ktype=cv2.CV_32F)
    h, w = g_kernel.shape[:2]
    g_kernel = cv2.resize(tophat, (3*w, 3*h), interpolation=cv2.INTER_CUBIC)
    image = cv2.bitwise_not(g_kernel)
    
    for i in range(len(image)):
        for j in range(len(image)):
            if image[i][j] > 244:
                image[i][j] = 0
    
    kernel = np.ones((2, 2), np.uint8)
    opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
    
    return opening

# Set paths for the input images directory and the output images directory
input_dir = os.path.abspath("training\\")
save_dir = "preprocessed_images/"

# Create the output directory if it does not already exist
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

# Loop over each image in the input directory
for filename in os.listdir(input_dir):
    # Load the image
    image_path = os.path.join(input_dir, filename)
    image = cv2.imread(image_path)
    
    if image is None:
        print(f"Could not read image at {image_path}")
        continue
    
    # Preprocess the image
    preprocessed_image = preprocess_image(image)
    
    # Save the preprocessed image to the output directory
    save_path = os.path.join(save_dir, filename)
    cv2.imwrite(save_path, preprocessed_image)
    
print("Preprocessing complete.")

D:\drcnn-train>python p_processing.py
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('D:\drcnn-train\training\0'): can't open/read file: check file path/integrity
Could not read image at D:\drcnn-train\training\0
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('D:\drcnn-train\training\1'): can't open/read file: check file path/integrity
Could not read image at D:\drcnn-train\training\1
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('D:\drcnn-train\training\2'): can't open/read file: check file path/integrity
Could not read image at D:\drcnn-train\training\2
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('D:\drcnn-train\training\3'): can't open/read file: check file path/integrity
Could not read image at D:\drcnn-train\training\3
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('D:\drcnn-train\training\4'): can't open/read file: check file path/integrity
Could not read image at D:\drcnn-train\training\4
Preprocessing complete.

我在代码中做错了什么,它没有读取图像。图片为PNG格式

没有问题目录

python tensorflow opencv deep-learning data-preprocessing
2个回答
0
投票

问题出在您的图像路径上,它也在您的错误消息中:

无法在

D:\drcnn-train\training\4
预处理完成时读取图像。 D 我不确定 4 是您的目录还是未指定扩展名的图像名称。

您可以尝试使用 glob,例如:

import glob 

image_paths = glob.glob(f"{input_dir}/*.jpg)

这将列出 input_dir 中所有以 .jpg 结尾的图像 然后你可以遍历 image_paths 并继续你的代码。


0
投票

首先确保路径中存在文件。通过打印您尝试作为文件打开的字符串的连接来验证它。将打印值与文件资源管理器中的实际文件进行比较。

还要从资源管理器中仔细检查文件的扩展名。

如果问题仍然存在,请确保您已更改文件的访问权限并且您的脚本可以访问它(检查文件的属性)。

© www.soinside.com 2019 - 2024. All rights reserved.