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

e37o9pze  于 2023-05-01  发布在  其他
关注(0)|答案(2)|浏览(242)
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:0@0.145] 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:0@0.148] 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:0@0.151] 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:0@0.154] 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:0@0.158] 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格式
没有问题目录

6kkfgxo0

6kkfgxo01#

问题出在你的图片路径上,它也出现在你的错误消息中:
无法读取D:\drcnn-train\training\4处的图像预处理完成。D我不确定4在你的情况下是一个目录还是一个没有指定扩展名的图像名称。
你可以尝试使用glob代替例如:

import glob 

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

这将列出以结尾的所有图像。然后你就可以在image_paths上循环并继续你的代码。

envsm3lx

envsm3lx2#

首先,确保路径中存在文件。通过打印你试图作为文件打开的字符串的连接来验证它。将打印值与文件资源管理器中的实际文件进行比较。
同时仔细检查资源管理器中文件的扩展名。
如果问题仍然存在,请确保您已更改文件的访问权限,并且您的脚本可以访问该文件(检查文件的属性)。

相关问题