变化检测数据集制作详细版

news/2025/2/19 14:57:45/

本文记录在进行变化检测数据集制作过程中所使用的代码

首先需要准备相同地区不同时间的两幅影像,裁减成合适大小,如256*256。相同区域命名相同放在两个文件夹下。

接着使用labelme对变化区域进行标注,这里不再进行labelme安装及标注的赘述。

在标注完成后,会在A、B两个文件夹下生成对应的json文件,A文件夹的json文件为对影像A的标注,B同上。

代码块包含四个部分:

1.json to png

2.同名png合并(处理AB都发生变化的标签)

3.独有png提取(处理只在A或B发生变化的标签)

4.png对应jpg提取(根据生成的标签检索AB)

1、将A的json文件放在Ajson文件夹,B同上,进行json转png

# json转png   开发时间:2023/5/13 21:19#------------------------------------------------------#
# 文件夹A\B所有影像
# 文件夹Ajson\Bjson所有json文件
#------------------------------------------------------#import json
import os
import numpy as np
import cv2def json_to_png(json_file_path, png_file_path):with open(json_file_path, 'r') as f:data = json.load(f)# 将json文件中的标注信息读取出来shapes = data['shapes']label_names = [shape['label'] for shape in shapes]# 获取每个标签对应的颜色值distinct_label_names = list(set(label_names))# 标签对应色彩字典label_name_to_color = {'1': (255, 255, 255)}# 创建空白的图片,并将每个像素点的值初始化为0img_height = data['imageHeight']img_width = data['imageWidth']img = np.zeros((img_height, img_width), dtype=np.uint8)# 为每个标注区域填充对应的颜色for shape in shapes:label_name = shape['label']color = label_name_to_color[label_name]points = shape['points']pts = np.array(points, np.int32)cv2.fillPoly(img, [pts], color=color)# 将生成的png图片保存到文件cv2.imwrite(png_file_path, img)if __name__ == '__main__':json_file_path = 'Ajson'json_fileList = os.listdir(json_file_path)for json_file in json_fileList:a = json_filefile_path_name = json_file_path + '/' + json_file# 截取文件名,用来命名图片name = os.path.splitext(file_path_name)[0]index = name + '.png'json_to_png(file_path_name, index)print(file_path_name, 'to', index)json_file_path_b = 'Bjson'json_fileList = os.listdir(json_file_path_b)for json_file in json_fileList:a = json_filefile_path_name = json_file_path_b + '/' + json_file# 截取文件名,用来命名图片name = os.path.splitext(file_path_name)[0]index = name + '.png'json_to_png(file_path_name, index)print(file_path_name, 'to', index)

2、按名称将A、B相同标签png的变化部分进行合并,使一对A、B的变化标签成为一张png

# 合并两个文件夹下相同名称的两张png标签
#
# 开发时间:2023/5/18 16:38
import os
from PIL import Imagedef merge(path1, path2, path3):img1 = Image.open(path1)img2 = Image.open(path2)width, height = img1.sizenew_img = Image.new('L', (width, height), 0)for x in range(width):for y in range(height):pixel1 = img1.getpixel((x, y))pixel2 = img2.getpixel((x, y))if pixel1 == 255 or pixel2 == 255:new_img.putpixel((x, y), 255)else:new_img.putpixel((x, y), 0)file_name = os.path.split(path1)[1]path_new = os.path.join(path3, file_name)# 保存新的图片new_img.save(path_new)# 提取需要合并的同名文件
A_file_path = 'Ajson'
A_fileList = os.listdir(A_file_path)B_file_path = 'Bjson'
B_fileList = os.listdir(B_file_path)result = 'result'Alist = []
for file_a in A_fileList:Alist.append(file_a)Blist = []
for file_b in B_fileList:Blist.append(file_b)common_set = set(Alist).intersection(set(Blist))common_list = list(common_set)if len(common_list) == 0:print("这两个列表没有相同的元素")
else:print("这两个列表有相同的元素:")print(len(common_list))for item in common_list:print(item)print(len(common_list))for item in common_list:path_a = os.path.join(A_file_path, item)path_b = os.path.join(B_file_path, item)path_result = resultif not os.path.exists(path_result):os.makedirs(path_result)merge(path_a, path_b, path_result)
print('merge successfully!!!')

3、检测AB文件夹下的文件的交集并集情况,把AB中独有的图片放进result

# 检测AB文件夹下的文件的交集并集情况,把AB中独有的图片放进result
#
# 开发时间:2023/5/19 10:09
import os
import shutilfolder1 = "Ajson"
folder2 = "Bjson"
result = 'result'
if not os.path.exists(result):os.makedirs(result)files1 = set(os.listdir(folder1))
files2 = set(os.listdir(folder2))common_files = files1.intersection(files2)# 获取不同的文件名
# different_files = files1.difference(files2).union(files2.difference(files1))
# A中独有的标签
different_filesA = files1.difference(files2)
# B中独有的标签
different_filesB = files2.difference(files1)# 将AB独有的标签放进result
for a in different_filesA:pngA = os.path.join(folder1, a)pngA_new = os.path.join(result, a)shutil.copy(pngA, pngA_new)for b in different_filesB:pngB = os.path.join(folder2, b)pngB_new = os.path.join(result, b)shutil.copy(pngB, pngB_new)# 输出结果
print("相同的文件名:", common_files)
print("相同的文件数量:", len(common_files))print("A独有的文件名:", different_filesA)
print("A独有的文件数量:", len(different_filesA))print("B独有的文件名:", different_filesB)
print("B独有的文件数量:", len(different_filesB))print('detection successfully!!!')

4、按照标签统计结果检索AB中对应的变化标签(此步骤如不需要可以不进行)

# 提取出result中的png对应于AB文件夹的jpg文件
#
# 开发时间:2023/5/25 8:27import os
import shutilfolder1 = "A_rea"
folder2 = "B_rea"
result = 'result'
# 原始ab文件夹
allA = 'A'
allB = 'B'if not os.path.exists(folder1):os.makedirs(folder1)
if not os.path.exists(folder2):os.makedirs(folder2)# 获取result文件夹中的所有文件名
files_result = set(os.listdir(result))# 修改后缀
name = []
for item in files_result:name.append(item.split('.')[0])for i in name:imgName = i + '.jpg'pathA = os.path.join(folder1, imgName)pathB = os.path.join(folder2, imgName)oldA = os.path.join(allA, imgName)oldB = os.path.join(allB, imgName)shutil.copy(oldA, pathA)shutil.copy(oldB, pathB)# 输出结果print("正在处理:", oldA)print("文件数量:", len(name))
print('Same_A_B successfully!!!')


http://www.ppmy.cn/news/1163838.html

相关文章

【工具】利用ffmpeg将网页中的.m3u8视频文件转化为.mp4格式

目录 0.环境 1.背景 2.前提 3.详细描述 1)在网站上找到你想下载的视频的.m3u8链接 2)打开命令行,用ffmpeg命令进行转化 3)过程&结果截图 0.环境 windows64 ffmpeg 1.背景 网页上有个.m3u8格式的视频文件,…

web:[极客大挑战 2019]HardSQL

题目 打开页面显示为 查看源代码没有发现其他的提示信息,随便尝试一下 错误 题目名为hardsql,先来尝试有无sql注入存在 尝试输入单引号输入 显示页面存在注入 这里按照常规思路继续使用order by函数和union select函数进行查询,但是页面没有…

C/C++网络编程基础知识超详细讲解上部分(系统性学习day11)

目录 前言 一、网络的含义与构成 含义: 构成: 二、网络的体系结构 1>OSI七层模型 2>TCP/IP协议体系结构 3>数据经过体系结构,怎么封装? 4>端口号 5>大小端序 6>TCP/UDP传输层的协议 三、系统函数API学习框…

MySQL中如何进行表的优化和压缩?

在MySQL中,可以通过以下方式进行表的优化和压缩: 使用合适的存储引擎(Storage Engine):MySQL提供了多种存储引擎,如InnoDB、MyISAM等。不同的存储引擎在表的优化和压缩方面有不同的特点。例如,I…

从0开始编写BP,自适应学习率的BP神经网络,不使用MATLAB工具箱,纯手写matlab代码,以BP分类为例...

与上篇文章不同,仔细读了上篇文章的小伙伴应该知道,BP神经网络是有一个学习率的,而这个学习率很大程度上决定着神经网络的效果。这里采用自适应学习率,实现纯手写BP神经网络。 编程时,激活函数选择Sigmoid函数&#xf…

【Java基础面试三十一】、String a = “abc“; ,说一下这个过程会创建什么,放在哪里?

文章底部有个人公众号:热爱技术的小郑。主要分享开发知识、学习资料、毕业设计指导等。有兴趣的可以关注一下。为何分享? 踩过的坑没必要让别人在再踩,自己复盘也能加深记忆。利己利人、所谓双赢。 面试官:String a “abc”; &am…

Spring Security登录表单配置(3)

1、登录表单配置 1.1、快速入门 理解了入门案例之后&#xff0c;接下来我们再来看一下登录表单的详细配置&#xff0c;首先创建一个新的Spring Boot项目&#xff0c;引入Web和Spring Security依赖&#xff0c;代码如下&#xff1a; <dependency><groupId>org.sp…

Squeeze-and-Attention Networks for Semantic Segmentation

0.摘要 最近&#xff0c;将注意力机制整合到分割网络中可以通过更重视提供更多信息的特征来提高它们的表征能力。然而&#xff0c;这些注意力机制忽视了语义分割的一个隐含子任务&#xff0c;并受到卷积核的网格结构的限制。在本文中&#xff0c;我们提出了一种新颖的squeeze-a…