画像を読み込む関数,ノイズを付加する関数,画像をsaveする関数を定義し,処理したところ
IndexError: index 0 is out of bounds for axis 0 with size 0 というerrorがでてきました.
testimageに何もないというメッセージというのはわかりますが,原因がわかりません.
#画像を読み込む def load_images(inputhpath,imagesize,type_color): imglist=[] exclude_prefixes=('_','.') for root,dirs,files in os.walk(inputpath): dirs[:]=[dir for dir in dirs if not dir.startswith(exclude_prefixes)] files[:]=[file for file in files if not file.startswith(exclude_prefixes)] for fn in sorted(files): bn,ext=os.path.splitext(fn) if ext not in[".bmp",".BMP",".jpg",".JPG",".jpeg",".JPEG",".png",".PNG"]: continue filename=os.path.join(root,fn) if type_color=='Color': testimage=cv2.imread(filename,cv2.IMREAD_COLOR) height,width=testimage.shape[:2] testimage=cv2.resize(testimage,(imagesize,imagesize),interpolation=cv2.INTER_AREA) testimage=np.asarray(testimage,dtype=np.float64) testimage=testimage[:,:,::,-1] elif type_color=='Gray': testimage=cv2.imread(filename,cv2.IMREAD_GRAYSCALE) height,width=testimage.shape[:2] testimage=cv2.resize(testimage,(imagesize,imagesize),interpolation=cv2.INTER_AREA) testimage=np.asarray([testimage],dtype=np.float64) testimage=np.asarray(testimage,dtype=np.float64).reshape((imagesize,imagesize,1)) imglist.append(testimage) imgsdata=np.asarray(imglist,dtype=np.float32) return imgsdata,sorted(files) inputpath='drive/My Drive/denoise' #ノイズを付加する def generate_noise(imagelist): imagelist_out=[] for i in range(0,len(imagelist)): image_temp=imagelist[i]+np.random.normal(loc=0.0,scale=50.0,size=imagelist[i].shape) image_temp=np.clip(image_temp,0,255) imagelist_out.append(image_temp) imgsdata=np.asarray(imagelist_out,dtype=np.float32) return imgsdata imagelist='drive/My Drive/denoise/train' #画像を保存 def save_images(savepath,filnamelist,imagelist): for i,fn in enumerate(savepath): filename=os.path.join(savepath,fn) testimage=imagelist[i] testimage=testimage[:,:,::-1] cv2.imwrite(filename,testimage) savepath='drive/My Drive/denoise' #処理の実行 image_train,image_train_filenames=load_images("drive/My Drive/denoise/train",256,'Gray') image_test,image_test_filenames=load_images("drive/My Drivet/denoise/test",256,'Gray') image_train=generate_noise(image_train) image_test=generate_noise(image_test) save_images("drive/My Drive/denoise/train_noise",image_train_filenames,image_train) save_images("drive/My Drive/denoise/test_noise",image_test_filenames,image_test) --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-26-077254fccf41> in <module>() 5 image_test=generate_noise(image_test) 6 ----> 7 save_images("drive/My Drive/denoise/train_noise",image_train_filenames,image_train) 8 save_images("drive/My Drive/denoise/test_noise",image_test_filenames,image_test) <ipython-input-25-1c159fb19400> in save_images(savepath, filnamelist, imagelist) 2 for i,fn in enumerate(savepath): 3 filename=os.path.join(savepath,fn) ----> 4 testimage=imagelist[i] 5 testimage=testimage[:,:,::-1] 6 cv2.imwrite(filename,testimage) IndexError: index 0 is out of bounds for axis 0 with size 0
あなたの回答
tips
プレビュー