1. 程式人生 > 其它 >遙感語義分割的資料清洗

遙感語義分割的資料清洗

  在將遙感影象進行切割後,我們得到了label集與image集,但此時資料集的圖片數目太多,因為我僅僅需要實現梯田的提取,所以存在大量的無用的圖片,因此在label集中根據影象的值對資料集進行資料清洗,再根據label集刪除image集中的無用的圖片,具體程式碼如下:

刪除label中值全為零的圖片:

import cv2
import os
import glob

path = "D:\Download\Pytorch-UNet-master\data\labelset"#在此輸入要清洗的label資料集的路徑
paths = glob.glob(os.path.join(path, '*.png'))

for file in paths:
	img = cv2.imread(file, 0)
	if cv2.countNonZero(img) == 0:
		os.remove(file)

按label檔案刪除image檔案:

import os

inputlabelPath = 'D:/Download/Pytorch-UNet-master/data/labelset/'#輸入label集的路徑
inputimagePath = 'D:/Download/Pytorch-UNet-master/data/dataset/'#輸入image集的路徑

filelist = os.listdir(inputimagePath)
for i in filelist:
	item_path = inputlabelPath + i
	image_path = inputimagePath + i
	if not os.path.exists(item_path):
		os.remove(image_path)

重新命名(也可以不用重新命名,只是我有強迫症)

import os
Path = 'D:/Download/Pytorch-UNet-master/data/labelset/'#分別放進去執行一下就可以了
#Path = 'D:/Download/Pytorch-UNet-master/data/dataset/'

filelist = os.listdir(Path)
n=0
for i in filelist:
	oldname = Path + os.sep + filelist[n]
	newname = Path + os.sep + 'a' + str(n) + '.JPG'
	os.rename(oldname, newname)
	print(oldname, '======>', newname)
	n = n + 1

資料清洗完成,可以開始後續的深度學習模型的訓練了