cv2.imwrite(os.path.join(save_pic_path, auged_pic_path), auged_img) cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:783: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
时间: 2023-07-23 15:02:16 浏览: 88
根据错误信息,`cv2.imwrite`函数在保存图像时出现了断言错误。具体错误信息是:图像为空(`_img.empty()`)。
这个错误通常发生在尝试保存一个空的图像时。请确保你传递给`cv2.imwrite`函数的图像是有效的,且不为空。
你可以检查一下图像的路径是否正确,以及图像是否成功加载和处理。如果图像加载成功,并且在进行处理后,还可以检查一下图像的尺寸是否正确。
另外,如果你能提供更多的代码和错误堆栈信息,我可以给出更具体的解决方案。
相关问题
import os import numpy as np import nibabel as nib import imageio import cv2 def read_niifile(niifilepath): # 读取niifile文件 img = nib.load(niifilepath) # 提取niifile文件 img_fdata = img.get_fdata(dtype='float32') return img_fdata def save_fig(niifilepath, savepath, num, name): # 保存为图片 name = name.split('-')[1] filepath_seg = niifilepath + "segmentation\" + "segmentation-" + name filepath_vol = niifilepath + "volume\" + "volume-" + name savepath_seg = savepath + "segmentation\" savepath_vol = savepath + "volume\" if not os.path.exists(savepath_seg): os.makedirs(savepath_seg) if not os.path.exists(savepath_vol): os.makedirs(savepath_vol) fdata_vol = read_niifile(filepath_vol) fdata_seg = read_niifile(filepath_seg) (x, y, z) = fdata_seg.shape total = x * y for k in range(z): silce_seg = fdata_seg[:, :, k] # 三个位置表示三个不同角度的切片 if silce_seg.max() == 0: continue else: silce_seg = (silce_seg - silce_seg.min()) / (silce_seg.max() - silce_seg.min()) * 255 silce_seg = cv2.threshold(silce_seg, 1, 255, cv2.THRESH_BINARY)[1] if (np.sum(silce_seg == 255) / total) > 0.015: silce_vol = fdata_vol[:, :, k] silce_vol = (silce_vol - silce_vol.min()) / (silce_vol.max() - silce_vol.min()) * 255 imageio.imwrite(os.path.join(savepath_seg, '{}.png'.format(num)), silce_seg) imageio.imwrite(os.path.join(savepath_vol, '{}.png'.format(num)), silce_vol) num += 1 # 将切片信息保存为png格式 return num if name == 'main': path = r"C:\Users\Administrator\Desktop\LiTS2017" savepath = r"C:\Users\Administrator\Desktop\2D-LiTS2017" filenames = os.listdir(path + "segmentation") num = 0 for filename in filenames: num = save_fig(path, savepath, num, filename) 替换掉代码中的cv2模块,实现相同功能
可以使用PIL库替换掉cv2模块,实现相同的功能。具体替换方法如下:
1.导入PIL库
```python
from PIL import Image
```
2.将cv2.threshold替换为PIL.ImageOps.autocontrast
```python
silce_seg = (silce_seg - silce_seg.min()) / (silce_seg.max() - silce_seg.min()) * 255
silce_seg = Image.fromarray(silce_seg.astype('uint8'))
silce_seg = ImageOps.autocontrast(silce_seg, cutoff=1)
silce_seg = np.array(silce_seg)
```
3.将cv2.imwrite替换为PIL.Image.save
```python
imageio.imwrite(os.path.join(savepath_seg, '{}.png'.format(num)), silce_seg)
imageio.imwrite(os.path.join(savepath_vol, '{}.png'.format(num)), silce_vol)
```
替换后的代码如下:
```python
import os
import numpy as np
import nibabel as nib
import imageio
from PIL import Image, ImageOps
def read_niifile(niifilepath):
# 读取niifile文件
img = nib.load(niifilepath)
# 提取niifile文件
img_fdata = img.get_fdata(dtype='float32')
return img_fdata
def save_fig(niifilepath, savepath, num, name):
# 保存为图片
name = name.split('-')[1]
filepath_seg = niifilepath + "segmentation/" + "segmentation-" + name
filepath_vol = niifilepath + "volume/" + "volume-" + name
savepath_seg = savepath + "segmentation/"
savepath_vol = savepath + "volume/"
if not os.path.exists(savepath_seg):
os.makedirs(savepath_seg)
if not os.path.exists(savepath_vol):
os.makedirs(savepath_vol)
fdata_vol = read_niifile(filepath_vol)
fdata_seg = read_niifile(filepath_seg)
(x, y, z) = fdata_seg.shape
total = x * y
for k in range(z):
silce_seg = fdata_seg[:, :, k] # 三个位置表示三个不同角度的切片
if silce_seg.max() == 0:
continue
else:
silce_seg = (silce_seg - silce_seg.min()) / (silce_seg.max() - silce_seg.min()) * 255
silce_seg = Image.fromarray(silce_seg.astype('uint8'))
silce_seg = ImageOps.autocontrast(silce_seg, cutoff=1)
silce_seg = np.array(silce_seg)
if (np.sum(silce_seg == 255) / total) > 0.015:
silce_vol = fdata_vol[:, :, k]
silce_vol = (silce_vol - silce_vol.min()) / (silce_vol.max() - silce_vol.min()) * 255
imageio.imwrite(os.path.join(savepath_seg, '{}.png'.format(num)), silce_seg)
imageio.imwrite(os.path.join(savepath_vol, '{}.png'.format(num)), silce_vol)
num += 1
# 将切片信息保存为png格式
return num
if __name__ == '__main__':
path = r"C:\Users\Administrator\Desktop\LiTS2017"
savepath = r"C:\Users\Administrator\Desktop\2D-LiTS2017"
filenames = os.listdir(path + "segmentation")
num = 0
for filename in filenames:
num = save_fig(path, savepath, num, filename)
```
替换掉此代码里的import cv2模块,import os import numpy as np import nibabel as nib import imageio import cv2 def read_niifile(niifilepath): # 读取niifile文件 img = nib.load(niifilepath) # 提取niifile文件 img_fdata = img.get_fdata(dtype='float32') return img_fdata def save_fig(niifilepath, savepath, num, name): # 保存为图片 name = name.split('-')[1] filepath_seg = niifilepath + "segmentation\\" + "segmentation-" + name filepath_vol = niifilepath + "volume\\" + "volume-" +name savepath_seg = savepath + "segmentation\\" savepath_vol = savepath + "volume\\" if not os.path.exists(savepath_seg): os.makedirs(savepath_seg) if not os.path.exists(savepath_vol): os.makedirs(savepath_vol) fdata_vol = read_niifile(filepath_vol) fdata_seg = read_niifile(filepath_seg) (x, y, z) = fdata_seg.shape total = x * y for k in range(z): silce_seg = fdata_seg[:, :, k] # 三个位置表示三个不同角度的切片 if silce_seg.max() == 0: continue else: silce_seg = (silce_seg-silce_seg.min())/(silce_seg.max() - silce_seg.min())*255 silce_seg = cv2.threshold(silce_seg, 1, 255, cv2.THRESH_BINARY)[1] if (np.sum(silce_seg == 255) / total) > 0.015: silce_vol = fdata_vol[:, :, k] silce_vol = (silce_vol - silce_vol.min()) / (silce_vol.max() - silce_vol.min()) * 255 imageio.imwrite(os.path.join(savepath_seg, '{}.png'.format(num)), silce_seg) imageio.imwrite(os.path.join(savepath_vol, '{}.png'.format(num)), silce_vol) num += 1 # 将切片信息保存为png格式 return num if __name__ == '__main__': path= 'E:\\dataset\\LiTS17\\' savepath = 'E:\\dataset\\LiTS17\\2d\\' filenames = os.listdir(path + "segmentation") num = 0 for filename in filenames: num = save_fig(path, savepath, num, filename)
替换掉此代码里的import cv2模块,可以使用Pillow库中的Image模块进行替换。具体实现方式如下:
```python
import os
import numpy as np
import nibabel as nib
import imageio
from PIL import Image
def read_niifile(niifilepath): # 读取niifile文件
img = nib.load(niifilepath) # 提取niifile文件
img_fdata = img.get_fdata(dtype='float32')
return img_fdata
def save_fig(niifilepath, savepath, num, name): # 保存为图片
name = name.split('-')[1]
filepath_seg = niifilepath + "segmentation\\" + "segmentation-" + name
filepath_vol = niifilepath + "volume\\" + "volume-" +name
savepath_seg = savepath + "segmentation\\"
savepath_vol = savepath + "volume\\"
if not os.path.exists(savepath_seg):
os.makedirs(savepath_seg)
if not os.path.exists(savepath_vol):
os.makedirs(savepath_vol)
fdata_vol = read_niifile(filepath_vol)
fdata_seg = read_niifile(filepath_seg)
(x, y, z) = fdata_seg.shape
total = x * y
for k in range(z):
silce_seg = fdata_seg[:, :, k]
if silce_seg.max() == 0:
continue
else:
silce_seg = (silce_seg-silce_seg.min())/(silce_seg.max() - silce_seg.min())*255
silce_seg = cv2.threshold(silce_seg, 1, 255, cv2.THRESH_BINARY)[1]
if (np.sum(silce_seg == 255) / total) > 0.015:
silce_vol = fdata_vol[:, :, k]
silce_vol = (silce_vol - silce_vol.min()) / (silce_vol.max() - silce_vol.min()) * 255
# 保存为png格式
Image.fromarray(silce_seg.astype(np.uint8)).save(os.path.join(savepath_seg, '{}.png'.format(num)))
Image.fromarray(silce_vol.astype(np.uint8)).save(os.path.join(savepath_vol, '{}.png'.format(num)))
num += 1
return num
if __name__ == '__main__':
path= 'E:\\dataset\\LiTS17\\'
savepath = 'E:\\dataset\\LiTS17\\2d\\'
filenames = os.listdir(path + "segmentation")
num = 0
for filename in filenames:
num = save_fig(path, savepath, num, filename)
```
阅读全文