High-resolution image data sets building extraction production

  1. Directory Structure

    /dataset/
     xxxx.tif  # 原始图像
     image-3000
         0.tif # 切割后的tif
         1.tif
         ........
         0.json # 生成的json文件也放在该文件夹下
         1.json
         .......
         0_json # 调用labelme_json_to_dataset 0.json 生成的0_json文件夹
             img.png # 原始图片的png格式
             info.yaml
             label.png # 标签图片
             label_names.txt 
             label_viz.png
         1_json
         .......
         0.png  # 将json文件夹中的label.png 提取出来
         1.png 
         .......
         label_0.tif # 将上边的png标签文件转换为tif格式

  2. Collecting data, high-resolution image

    1. UAV data, aeronautical data, etc.
  3. Cut image, the pixel size is how much?

    1. This average pixel data set size (40000 * 50000) tif format, LZW compression method

    2. To consider the computer's graphics card, try not to be the target building without cutting issues such as the use of principal and interest 3000 3000 *

      # data:2020-01-04
      # user:dean
      # desc:图像切割脚本
      import tifffile as tiff  # 也可使用pillow或opencv 但若图片过大时可能会出问题
      import os
      image = r"I:\人工智能数据\DOM\裴庄村51-dom\裴庄村51-dom.tif"
      target_dir = r"I:\人工智能数据\DOM\裴庄村51-dom\image-3000"  # 切割后图片存储位置
      width = 1500*2   # 切割图像大小
      height = 1500*2  # 切割图像大小
      img = tiff.imread(image)  # 导入图片
      print("导入图片完成",img.shape) # 原始图片大小
      pic_width = img.shape[1]
      pic_height = img.shape[0]
      row_num = pic_width//width  # 纵向切割数量
      col_num = pic_height // height  # 横向切割数量
      print("开始进行切割,可切割总数为{}".format(col_num*row_num))
      for j in range(col_num):
          for i in range(row_num):
              num = j * row_num + i
              print("正在进行第{}张切割".format(num + 1))
              row = i * width
              row_end = row + width
              col = j * height
              col_end = col + height
              # print(col,col_end,row,row_end)
              cropped = img[col:col_end,row:row_end]
              name = "{}.tif".format(num)
              image_path = os.path.join(target_dir,name)
              tiff.imsave(image_path, cropped)
  4. Annotation tool labelme

    1. Use label tagging each picture

      pip install labelme  # 安装labelme
    2. After tagging each image file will generate the corresponding name.json

      labelme_json_to_dataset xxx.json 

      # data:2020-01-04
      # user:dean
      # desc:批量将json文件转为 label
      import os
      dir = r"I:\人工智能数据\DOM\裴庄村51-dom\image-3000"
      files = [os.path.join(dir,file) for file in os.listdir(dir) if file.endswith(".json")]
      for file in files:
          cmd = "labelme_json_to_dataset {}".format(file)
          print(cmd)
          os.system(cmd)
    3. All json / label.png to extract a unified folder

      # data:2020-01-04
      # user:dean
      # desc:将label文件夹中的laebl提取出来
      import tifffile as tiff
      from  PIL import Image
      import os
      target_dir = r"I:\人工智能数据\DOM\裴庄村51-dom\image-3000"  # json_label 所在的文件夹
      files = [os.path.join(target_dir,file)  for file in os.listdir(target_dir)]
      for i in files:
          if os.path.isdir(i):
              lables = os.listdir(i)
              for file in lables:
                  if file == "label.png":
                      imgae = Image.open(os.path.join(i, "label.png"))
                      name = "{}.png".format(i.split("_")[0])
                      imgae.save(os.path.join(target_dir,name))
                      print("第{}个文件夹".format(i))
                      break;
    4. Convert all label.png as tif format

      # coding:utf-8
      # file: change_format.py
      # author: Dean
      # contact: [email protected]
      # time: 2020/1/4 20:41
      # desc:图片格式转换
      from PIL import Image
      import os
      target_dir = "I:\人工智能数据\DOM\裴庄村51-dom\image-3000"  # png 图片所在文件夹
      files = [os.path.join(target_dir,file) for file in  os.listdir(target_dir)]
      print(files)
      for file in files:
          if file.endswith(".png"):
              name = os.path.basename(file)
              new_name = "label_{}.tif".format(name.split(".")[0])
              image = Image.open(file)
              path = os.path.join(target_dir, new_name)
              image.save(path)
              print(path)
    5. End (The corresponding data can be extracted)

Guess you like

Origin www.cnblogs.com/Dean0731/p/12150524.html