File "D:\py_works\test.py", line 12, in convertPNG uint16_img -= uint16_img.min() AttributeError: 'NoneType' object has no attribute 'min'
时间: 2023-05-20 12:01:46 浏览: 40
这个错误提示表明在 test.py 文件的第 12 行中,convertPNG 函数中的 uint16_img 对象是 NoneType 类型,因此无法调用 min() 方法。建议检查代码中是否有未赋值的变量或者是否有错误的函数调用。
相关问题
[ WARN:0@0.252] global loadsave.cpp:244 cv::findDecoder imread_('D:\学习专用\学习\硕士学习\深度\1.png'): can't open/read file: check file path/integrity Traceback (most recent call last): File "D:\py_works\test.py", line 28, in <module> convertPNG(pngfile, TARGET_FILE)#C:/Users/BAMBOO/Desktop/source pics/rgbd_6/color File "D:\py_works\test.py", line 12, in convertPNG uint16_img -= uint16_img.min() AttributeError: 'NoneType' object has no attribute 'min'
这是一个警告信息,提示无法打开或读取文件路径不正确。同时,还有一个追踪信息,显示在代码文件test.py的第28行调用了convertPNG函数,并传入了两个参数:pngfile和TARGET_FILE。
代码import os import numpy as np import nibabel as nib from PIL import Image # 创建保存路径 save_path = 'C:/Users/Administrator/Desktop/2D-LiTS2017' if not os.path.exists(save_path): os.makedirs(save_path) if not os.path.exists(os.path.join(save_path, 'image')): os.makedirs(os.path.join(save_path, 'image')) if not os.path.exists(os.path.join(save_path, 'label')): os.makedirs(os.path.join(save_path, 'label')) # 加载数据集 data_path = 'D:/BaiduNetdiskDownload/LiTS2017' img_path = os.path.join(data_path, 'Training Batch 1') label_path = os.path.join(data_path, 'Training Batch 2') # 转换图像 for file in sorted(os.listdir(img_path)): if file.endswith('.nii'): img_file = os.path.join(img_path, file) img = nib.load(img_file).get_fdata() img = np.transpose(img, (2, 0, 1)) # 转换为z, x, y for i in range(img.shape[0]): img_slice = img[i, :, :] img_slice = (img_slice - np.min(img_slice)) / (np.max(img_slice) - np.min(img_slice)) * 255 # 归一化到0-255 img_slice = img_slice.astype(np.uint8) img_slice = np.stack([img_slice]*3, axis=2) # 转换为三通道图像 img_name = file[:-4] + '' + str(i).zfill(3) + '.png' img_file_save = os.path.join(save_path, 'image', img_name) Image.fromarray(img_slice).save(img_file_save) # 转换标签 for file in sorted(os.listdir(label_path)): if file.endswith('.nii'): label_file = os.path.join(label_path, file) label = nib.load(label_file).get_fdata() label = np.transpose(label, (2, 0, 1)) # 转换为z, x, y for i in range(label.shape[0]): label_slice = label[i, :, :] label_slice[label_slice == 1] = 255 # 肝脏灰度值设为255 label_slice[label_slice == 2] = 128 # 肝脏肿瘤灰度值设为128 label_slice = label_slice.astype(np.uint8) label_name = file[:-4] + '' + str(i).zfill(3) + '.png' label_file_save = os.path.join(save_path, 'label', label_name) Image.fromarray(label_slice).save(label_file_save)出现scaled = scaled.astype(np.promote_types(scaled.dtype, dtype), copy=False) MemoryError错误,怎么修改?给出完整代码
这个错误通常意味着你的计算机没有足够的内存来处理数组。可能需要减少转换图像的尺寸或者使用更高效的算法。以下是一个可能的修改建议:
```python
import os
import numpy as np
import nibabel as nib
from PIL import Image
# 创建保存路径
save_path = 'C:/Users/Administrator/Desktop/2D-LiTS2017'
if not os.path.exists(save_path):
os.makedirs(save_path)
if not os.path.exists(os.path.join(save_path, 'image')):
os.makedirs(os.path.join(save_path, 'image'))
if not os.path.exists(os.path.join(save_path, 'label')):
os.makedirs(os.path.join(save_path, 'label'))
# 加载数据集
data_path = 'D:/BaiduNetdiskDownload/LiTS2017'
img_path = os.path.join(data_path, 'Training Batch 1')
label_path = os.path.join(data_path, 'Training Batch 2')
# 转换图像
for file in sorted(os.listdir(img_path)):
if file.endswith('.nii'):
img_file = os.path.join(img_path, file)
img = nib.load(img_file).get_fdata()
img = np.transpose(img, (2, 0, 1)) # 转换为z, x, y
for i in range(img.shape[0]):
img_slice = img[i, :, :]
img_slice = (img_slice - np.min(img_slice)) / (np.max(img_slice) - np.min(img_slice)) * 255 # 归一化到0-255
img_slice = img_slice.astype(np.uint8)
img_slice = np.stack([img_slice]*3, axis=2) # 转换为三通道图像
#减少图像大小
img_slice = np.array(Image.fromarray(img_slice).resize((512, 512), resample=Image.BICUBIC))
img_name = file[:-4] + '' + str(i).zfill(3) + '.png'
img_file_save = os.path.join(save_path, 'image', img_name)
Image.fromarray(img_slice).save(img_file_save)
# 转换标签
for file in sorted(os.listdir(label_path)):
if file.endswith('.nii'):
label_file = os.path.join(label_path, file)
label = nib.load(label_file).get_fdata()
label = np.transpose(label, (2, 0, 1)) # 转换为z, x, y
for i in range(label.shape[0]):
label_slice = label[i, :, :]
label_slice[label_slice == 1] = 255 # 肝脏灰度值设为255
label_slice[label_slice == 2] = 128 # 肝脏肿瘤灰度值设为128
label_slice = label_slice.astype(np.uint8)
label_name = file[:-4] + '' + str(i).zfill(3) + '.png'
label_file_save = os.path.join(save_path, 'label', label_name)
Image.fromarray(label_slice).save(label_file_save)
```
这里我们使用了 `PIL.Image` 中的 `resize()` 函数来减少图像大小。`resample=Image.BICUBIC` 用于指定插值方法,这里使用了 Bicubic 插值。这个方法可以保留图像的细节,并且可以减少内存使用。你也可以尝试使用其他插值方法。