Personal use record: batch production of dsm deep learning labels from point clouds to start training

 

CGdomYRJ-114(CK17-70)_BH_42.tif
CGdomYRJ-114(CK17-70)_BH_23.tif
CGdomYRJ-114(CK17-70)_BH_22.tif

 32-bit to 24-bit

import os
from PIL import Image
import cv2

png_path = './dataset/sat_train/'
save_path = './dataset/sat_train2/'

#遍历将图像中的位数32bit-->24bit
for image_name in sorted(os.listdir(png_path)):
    image_path = os.path.join(png_path,image_name)
    # img = Image.open(image_path)
    print('image_name:',image_name)
    img_24bit = Image.open(image_path).convert('RGB')
    dst = save_path+image_name
    img_24bit.save(dst)

print('convert_finished')

Not 6060

import os
import cv2

import shutil
# Cutting the input image to h*w blocks
inPath = "./dataset/sat_train2/"
outPath = "./dataset/sat_train3/"
 
for f in os.listdir(inPath):
 
 
    path = inPath + f.strip()
    img = cv2.imread(path) 
   
    height = img.shape[0]
    width = img.shape[1]
    if height!=6061 or width!=6060:
        shutil.move(inPath+f, outPath+f)
        print(f)

CGdomYRJ-114(CK17-70)_BG_16.tif
CGdomYRJ-114(CK17-70)_AE_18.tif
CGdomYRJ-114(CK0-17)_G_07.tif
CGdomYRJ-114(CK17-70)_BC_27.tif
CGdomYRJ-114(CK17-70)_BH_24.tif
CGdomYRJ-114(CK17-70)_BH_21.tif
CGdomYRJ-114(CK17-70)_AI_21.tif
CGdomYRJ-114(CK17-70)_AE_14.tif
CGdomYRJ-114(CK17-70)_BH_20.tif
CGdomYRJ-114(CK17-70)_BH_41.tif
CGdomYRJ-114(CK17-70)_BK_19.tif
CGdomYRJ-114(CK17-70)_AC_11.tif
CGdomYRJ-114(CK17-70)_BE_19.tif
CGdomYRJ-114(CK17-70)_BN_19.tif
CGdomYRJ-114(CK17-70)_AP_32.tif
CGdomYRJ-114(CK17-70)_AF_23.tif
CGdomYRJ-114(CK17-70)_BF_20.tif
CGdomYRJ-114(CK17-70)_BJ_19.tif
CGdomYRJ-114(CK17-70)_BG_50.tif
CGdomYRJ-114(CK17-70)_BD_22.tif
CGdomYRJ-114(CK17-70)_AR_32.tif
CGdomYRJ-114(CK17-70)_BI_20.tif
CGdomYRJ-114(CK17-70)_BG_23.tif
CGdomYRJ-114(CK17-70)_AD_10.tif
CGdomYRJ-114(CK17-70)_BF_23.tif
CGdomYRJ-114(CK17-70)_BG_25.tif
CGdomYRJ-114(CK0-17)_P_06.tif
CGdomYRJ-114(CK17-70)_AF_22.tif
CGdomYRJ-114(CK17-70)_AC_10.tif
CGdomYRJ-114(CK17-70)_BL_19.tif
CGdomYRJ-114(CK17-70)_BF_53.tif
CGdomYRJ-114(CK17-70)_AS_33.tif
CGdomYRJ-114(CK17-70)_AD_14.tif
CGdomYRJ-114(CK17-70)_BF_26.tif
CGdomYRJ-114(CK17-70)_BG_22.tif
CGdomYRJ-114(CK17-70)_BG_27.tif
CGdomYRJ-114(CK17-70)_AE_19.tif
CGdomYRJ-114(CK17-70)_BD_25.tif
CGdomYRJ-114(CK0-17)_P_05.tif
CGdomYRJ-114(CK17-70)_BH_25.tif
CGdomYRJ-114(CK17-70)_BE_16.tif
CGdomYRJ-114(CK0-17)_M_07.tif
CGdomYRJ-114(CK17-70)_BC_26.tif
CGdomYRJ-114(CK17-70)_BD_26.tif
CGdomYRJ-114(CK17-70)_BF_16.tif
CGdomYRJ-114(CK17-70)_BE_29.tif
CGdomYRJ-114(CK17-70)_AI_22.tif
CGdomYRJ-114(CK17-70)_BC_28.tif
CGdomYRJ-114(CK0-17)_Q_06.tif
CGdomYRJ-114(CK17-70)_AD_15.tif
CGdomYRJ-114(CK17-70)_BN_18.tif
CGdomYRJ-114(CK17-70)_BD_23.tif
CGdomYRJ-114(CK17-70)_BG_42.tif
CGdomYRJ-114(CK17-70)_BG_45.tif
CGdomYRJ-114(CK17-70)_BF_18.tif
CGdomYRJ-114(CK17-70)_BC_30.tif
CGdomYRJ-114(CK17-70)_BG_29.tif
CGdomYRJ-114(CK17-70)_BF_52.tif
CGdomYRJ-114(CK17-70)_BJ_18.tif
CGdomYRJ-114(CK17-70)_BN_17.tif
CGdomYRJ-114(CK17-70)_BG_28.tif
CGdomYRJ-114(CK17-70)_BM_19.tif
CGdomYRJ-114(CK17-70)_BG_18.tif
CGdomYRJ-114(CK17-70)_BD_21.tif
CGdomYRJ-114(CK17-70)_BE_15.tif
CGdomYRJ-114(CK17-70)_BG_15.tif
CGdomYRJ-114(CK17-70)_BH_43.tif
CGdomYRJ-114(CK17-70)_BG_49.tif
CGdomYRJ-114(CK17-70)_BF_22.tif
CGdomYRJ-114(CK17-70)_BF_43.tif
CGdomYRJ-114(CK17-70)_BC_29.tif
CGdomYRJ-114(CK0-17)_O_04.tif
CGdomYRJ-114(CK17-70)_BM_18.tif
CGdomYRJ-114(CK17-70)_AG_18.tif
CGdomYRJ-114(CK17-70)_AB_07.tif

delete inconsistencies

import os
import cv2

# source = 'dataset/sat_train/'
dem_tar ="./dataset/sat_train/"
mask_tar ="./dataset/mask_train/"

mask_names = filter(lambda x: x.find('tif')!=-1, os.listdir(mask_tar))
dem_names = filter(lambda x: x.find('tif')!=-1, os.listdir(dem_tar))
for f in mask_names:
    dem_name = dem_tar + f.strip()
    if not os.path.exists(dem_name):
        os.remove(mask_tar + f.strip())
        print('mask' +f.strip())
for f in dem_names:
    mask_name = mask_tar + f.strip()
    if not os.path.exists(mask_name):
        os.remove(dem_tar + f.strip())
        print('dem'+f.strip())

Making Labels 512



import os
from osgeo import gdal
 
inPath = "./dataset/sat_train/"
outPath = "./dataset/train/"
 
 
for f in os.listdir(inPath):
# 读取要切的原图
    imgPath=inPath+f.strip()
    in_ds = gdal.Open(imgPath)
    print(f.strip()+"open tif file succeed")
    width = in_ds.RasterXSize  # 获取数据宽度
    height = in_ds.RasterYSize  # 获取数据高度
    outbandsize = in_ds.RasterCount # 获取数据波段数
    # im_geotrans = in_ds.GetGeoTransform() # 获取仿射矩阵信息
    # im_proj = in_ds.GetProjection()  # 获取投影信息
    datatype = in_ds.GetRasterBand(1).DataType
    im_data = in_ds.ReadAsArray()  # 获取数据
 
    # 读取原图中的每个波段
    in_band1 = in_ds.GetRasterBand(1)

 
    # 定义切图的起始点坐标
    offset_x = 0
    offset_y = 0
 
    # 定义切图的大小(矩形框)
    block_xsize = 512 # 行
    block_ysize = 512 # 列
 
 
    k = 0
    for j in range(width // block_xsize):
        for i in range(height // block_xsize):
            out_band1 = in_band1.ReadAsArray(i * block_xsize, j * block_xsize, block_xsize, block_ysize)
            out_band2 = in_band1.ReadAsArray(i * block_xsize, j * block_xsize, block_xsize, block_ysize)
            out_band3 = in_band1.ReadAsArray(i * block_xsize, j * block_xsize, block_xsize, block_ysize)

            # print(out_band3)
            
            # 获取Tif的驱动,为创建切出来的图文件做准备
            gtif_driver = gdal.GetDriverByName("GTiff")
 
 
            # 创建切出来的要存的文件(3代表3个不都按,最后一个参数为数据类型,跟原文件一致)
            filename = outPath + f.strip()[:-4] + '({},{})@{:04d}_sat.tif'.format(j, i, k)  # 文件名称
            out_ds = gtif_driver.Create(filename, block_xsize, block_ysize, outbandsize, datatype)
            k += 1
            # print("create new tif file succeed")
 
            # 写入目标文件
            out_ds.GetRasterBand(1).WriteArray(out_band1)
            out_ds.GetRasterBand(2).WriteArray(out_band2)
            out_ds.GetRasterBand(3).WriteArray(out_band3)

 
            # 将缓存写入磁盘
            out_ds.FlushCache()
            del out_ds
    print("FlushCache succeed")
print("End!")

# import os
# from osgeo import gdal
 
inPath = "./dataset/mask_train/"
outPath = "./dataset/train/"
 
 
for f in os.listdir(inPath):
# 读取要切的原图
    imgPath=inPath+f.strip()
    in_ds = gdal.Open(imgPath)
    print(f.strip()+"open tif file succeed")
    width = in_ds.RasterXSize  # 获取数据宽度
    height = in_ds.RasterYSize  # 获取数据高度
    outbandsize = in_ds.RasterCount # 获取数据波段数
    # im_geotrans = in_ds.GetGeoTransform() # 获取仿射矩阵信息
    # im_proj = in_ds.GetProjection()  # 获取投影信息
    datatype = in_ds.GetRasterBand(1).DataType
    im_data = in_ds.ReadAsArray()  # 获取数据
 
    # 读取原图中的每个波段
    in_band1 = in_ds.GetRasterBand(1)
    in_band2 = in_ds.GetRasterBand(2)
    in_band3 = in_ds.GetRasterBand(3)

 
    # 定义切图的起始点坐标
    offset_x = 0
    offset_y = 0
 
    # 定义切图的大小(矩形框)
    block_xsize = 512 # 行
    block_ysize = 512 # 列
 
 
    k = 0
    for j in range(width // block_xsize):
        for i in range(height // block_xsize):
            out_band1 = in_band1.ReadAsArray(i * block_xsize, j * block_xsize, block_xsize, block_ysize)
            out_band2 = in_band2.ReadAsArray(i * block_xsize, j * block_xsize, block_xsize, block_ysize)
            out_band3 = in_band3.ReadAsArray(i * block_xsize, j * block_xsize, block_xsize, block_ysize)

            # print(out_band3)
            
            # 获取Tif的驱动,为创建切出来的图文件做准备
            gtif_driver = gdal.GetDriverByName("GTiff")
 
 
            # 创建切出来的要存的文件(3代表3个不都按,最后一个参数为数据类型,跟原文件一致)
            filename = outPath + f.strip()[:-4] + '({},{})@{:04d}_mask.png'.format(j, i, k)  # 文件名称
            out_ds = gtif_driver.Create(filename, block_xsize, block_ysize, outbandsize, datatype)
            k += 1
            # print("create new tif file succeed")
 
            # 写入目标文件
            out_ds.GetRasterBand(1).WriteArray(out_band1)
            out_ds.GetRasterBand(2).WriteArray(out_band2)
            out_ds.GetRasterBand(3).WriteArray(out_band3)

            # 将缓存写入磁盘
            out_ds.FlushCache()
            del out_ds
    print("FlushCache succeed")
print("End!")

# import os
# from osgeo import gdal
import cv2
outPath = "./dataset/train/"
 
mask_names = filter(lambda x: x.find('mask')!=-1, os.listdir(outPath))
# sat_names = filter(lambda x: x.find('sat')!=-1, os.listdir(tar))
#trainlist = list(map(lambda x: x[:-8], imagelist))
for f in mask_names:
    path = outPath + f.strip()
    if not os.path.exists(path):
        continue;    
    img = cv2.imread(path,0)             
    if cv2.countNonZero(img) == 0:
       print(f+'Image is black')
       path2=f[:-9]
       os.remove(path)
       os.remove(outPath +path2 + "_sat.tif")


self.firstconv = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False)

Guess you like

Origin blog.csdn.net/weixin_61235989/article/details/129953512