from __future__ import print_function
import os
import cv2
import skimage.io as io
def testGenerator(label_path, num_image=30, target_size=(256, 256), flag_multi_class=False, as_gray=True):
for i in range(num_image):
img = io.imread(os.path.join(label_path, "%d.png" % i), as_gray=as_gray)
img = cv2.resize(img, target_size)
resize_path = 'D:/cv/2.unet-code/data/membrane/train/resize'
cv2.imwrite(os.path.join(resize_path, "%d.png" % i), img, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
label_path = "D:/cv/2.unet-code/data/membrane/train/label"
testGenerator(label_path, num_image=30, target_size=(256, 256), flag_multi_class=False, as_gray=True)
在此过程中遇到的问题
1、要增加os库 cv2库 scikit-image 库,加载的时候尽量用镜像下载,下载速率快
2、 注意下面这两个函数中的获取文件时的方式os.path.join(label_path, “%d.png” % i),特别好用!!!
注意cv2.imwrite(fname,img,num)函数中的num,num代表的是图像的质量,记得要强制转换为整数类型
img = io.imread(os.path.join(label_path, "%d.png" % i), as_gray=as_gray) cv2.imwrite(os.path.join(resize_path, "%d.png" % i), img, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
3、显示找不到路径的几个原因:
(1)看看是否是英文路径
(2)路径是否正确。(注意读取的路径和保存的路径,可以将代码中的路径复制粘贴到计算机中,看看路径是否设置正确)
(3)将“\”改为“/”或者用“\\”代替“/”
注意:因为我是python小白,很多东西可能表述不清楚,欢迎大家批评指正。



