Change Detection Google-CD Dataset Processing

1. Google-CD
contains 20 pairs of images from Google Earth Map. The image resolution is 1006×1168 ~ 4936×5224 pixels. We crop the images into small patches of size 256×256 with no overlap, and randomly divide them into three parts: 2504/313/313 for training/validation/testing, respectively.
2. Dataset download link:
Baidu Netdisk: https://pan.baidu.com/s/19GNeHoZo1VfFli3N0jgxLA?pwd=4d6m
Extraction code: 4d6m
3. Dataset processing
three folders (T1, T2, label) in TIF format , TIF format and PNG format, the pictures inside are cut into small pieces with a size of 256×256 and no overlap, each folder gets a total of 3130, and the generated picture format is PNG. Put in three new folders (A, B, label) respectively, and the names of the pictures corresponding to the same name in the three folders are also the same after cutting.
Be careful to change the file path.

import os
import random
from PIL import Image

def split_image(image_path, output_folder):

    # 打开图片
    img = Image.open(image_path)

    # 转换成PNG格式
    img = img.convert('RGB')

    # 计算分割后的小块数量
    width, height = img.size
    x_blocks = width // 256
    y_blocks = height // 256
    num_blocks = x_blocks * y_blocks

    # 分割图片
    for i in range(x_blocks):
        for j in range(y_blocks):
            box = (i*256, j*256, (i+1)*256, (j+1)*256)
            block = img.crop(box)

            # 保存分割后的小块
            filename = image_path.split('/')[-1][:-4] + '_' + str(i) + '_' + str(j) + '.png'
            block.save(os.path.join(output_folder, filename))

    return num_blocks

# 分割T1文件夹里的图片,并保存到1文件夹
T1_path = 'T1/'
output_path = 'A/'
if not os.path.exists(output_path):
    os.makedirs(output_path)

num_blocks = 0
for filename in os.listdir(T1_path):
    if filename.endswith('.tif'):
        image_path = os.path.join(T1_path, filename)
        num_blocks += split_image(image_path, output_path)
    elif filename.endswith('.tiff'):
        image_path = os.path.join(T1_path, filename)
        num_blocks += split_image(image_path, output_path)
    elif filename.endswith('.png'):
        image_path = os.path.join(T1_path, filename)
        num_blocks += split_image(image_path, output_path)

print(f'T1分割后得到{num_blocks}个小块')

# 分割T2文件夹里的图片,并保存到2文件夹
T2_path = 'T2/'
output_path = 'B/'
if not os.path.exists(output_path):
    os.makedirs(output_path)

num_blocks = 0
for filename in os.listdir(T2_path):
    if filename.endswith('.tif'):
        image_path = os.path.join(T2_path, filename)
        num_blocks += split_image(image_path, output_path)
    elif filename.endswith('.tiff'):
        image_path = os.path.join(T2_path, filename)
        num_blocks += split_image(image_path, output_path)
    elif filename.endswith('.png'):
        image_path = os.path.join(T2_path, filename)
        num_blocks += split_image(image_path, output_path)

print(f'T2分割后得到{num_blocks}个小块')

# 分割label文件夹里的图片,并保存到3文件夹
label_path = 'label/'
output_path = 'label/'
if not os.path.exists(output_path):
    os.makedirs(output_path)

num_blocks = 0
for filename in os.listdir(label_path):
    if filename.endswith('.tif'):
        image_path = os.path.join(label_path, filename)
        num_blocks += split_image(image_path, output_path)
    elif filename.endswith('.tiff'):
        image_path = os.path.join(label_path, filename)
        num_blocks += split_image(image_path, output_path)
    elif filename.endswith('.png'):
        image_path = os.path.join(label_path, filename)
        num_blocks += split_image(image_path, output_path)

print(f'label分割后得到{num_blocks}个小块')

4. Divide the training set, verification set, and test set 8:1:1
Copy the A folder to generate train, test, val folders

import os
import random
from PIL import Image

# 创建train、test、val文件夹
train_path = 'train/'
test_path = 'test/'
val_path = 'val/'
for path in [train_path, test_path, val_path]:
    if not os.path.exists(path):
        os.makedirs(path)

# 把A文件夹图片按8:1:1比例随机放入train、test、val文件夹
A_path = 'A1/'
image_paths = [os.path.join(A_path, filename) for filename in os.listdir(A_path) if filename.endswith('.png')]
random.shuffle(image_paths)
num_images = len(image_paths)
train_num = int(num_images * 0.8)
test_num = int(num_images * 0.1)
val_num = num_images - train_num - test_num
for i, path in enumerate(image_paths):
    if i < train_num:
        output_folder = train_path
    elif i < train_num + test_num:
        output_folder = test_path
    else:
        output_folder = val_path
    output_path = os.path.join(output_folder, path.split('/')[-1])
    os.rename(path, output_path) # 移动文件到新文件夹

5. Generate a list file
Read the folder picture name and generate a .txt file

import os

folder_path = '/test/'
with open('test.txt', 'w') as f:
    for filename in os.listdir(folder_path):
        if filename.endswith('.jpg') or filename.endswith('.png'):
            f.write(filename + '\n')

6. Label RGB NO
error:

ValueError: size shape must match input shape. Input is 2D, size is 4

(255,255,3) to (255,255)

import os
import cv2
import numpy as np

# 输入文件夹路径
input_folder_path =  r'F:\label'

# 输出文件夹路径
output_folder_path =  r'F:\label1'

# 遍历所有图片并转换到灰度图像
for filename in os.listdir(input_folder_path):
    if filename.endswith(".png"):
        # 读入原始RGB图像
        img = cv2.imread(os.path.join(input_folder_path, filename))

        # 转换到灰度图像
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

        # 保存到指定路径
        cv2.imwrite(os.path.join(output_folder_path, filename), gray)

Guess you like

Origin blog.csdn.net/qq_40994007/article/details/131326400