当前位置: 首页 > news >正文

大红门做网站的公司北京网站优化培训

大红门做网站的公司,北京网站优化培训,福州网站设计企业网站建站,网站建设技术课程设计报告文章目录 TIF图像转JPGTIF标签转PNG图像重叠裁剪图像重命名数据集转COCO格式数据集转VOC格式 遥感图像不同于一般的自然图像,由于波段数量、图像位深度等原因,TIF图像数据不能使用简单的格式转换方法。本文以Potsdam数据集为例,制作能够直接用…

文章目录

  • TIF图像转JPG
  • TIF标签转PNG
  • 图像重叠裁剪
  • 图像重命名
  • 数据集转COCO格式
  • 数据集转VOC格式

遥感图像不同于一般的自然图像,由于波段数量、图像位深度等原因,TIF图像数据不能使用简单的格式转换方法。本文以Potsdam数据集为例,制作能够直接用于深度学习的数据集。
Potsdam数据集的内容如下:
在这里插入图片描述
本文使用2_Ortho_RGB(图像数据RGB通道顺序)和5_Labels_all(标签数据)作为示例进行数据集制作。
在这里插入图片描述
在这里插入图片描述

TIF图像转JPG

TIF转JPG借鉴了文章怎么读取tif格式的卫星遥感数据,并将数据输入到神经网络模型中进行训练

# -*- coding: UTF-8 -*-
import numpy as np
import os
from PIL import Image
from osgeo import gdaldef readTif(imgPath, bandsOrder=[1, 2, 3]):"""读取GEO tif影像的前三个波段值,并按照R.G.B顺序存储到形状为【原长*原宽*3】的数组中:param imgPath: 图像存储全路径:param bandsOrder: RGB对应的波段顺序,如高分二号多光谱包含蓝B,绿g,红R,近红Nir外四个波段,RGB对应的波段为3,2,1:return: R.G.B三维数组"""dataset = gdal.Open(imgPath, gdal.GA_ReadOnly)cols = dataset.RasterXSizerows = dataset.RasterYSizedata = np.empty([rows, cols, 3], dtype=float)for i in range(3):band = dataset.GetRasterBand(bandsOrder[i])oneband_data = band.ReadAsArray()data[:, :, i] = oneband_datareturn datadef stretchImg(imgPath, resultPath, lower_percent=0.5, higher_percent=99.5):"""#将光谱DN值映射至0-255,并保存:param imgPath: 需要转换的tif影像路径(***.tif):param resultPath: 转换后的文件存储路径(***.jpg):param lower_percent: 低值拉伸比率:param higher_percent: 高值拉伸比率:return: 无返回参数,直接输出图片"""RGB_Array = readTif(imgPath)band_Num = RGB_Array.shape[2]JPG_Array = np.zeros_like(RGB_Array, dtype=np.uint8)for i in range(band_Num):minValue = 0maxValue = 255# 获取数组RGB_Array某个百分比分位上的值low_value = np.percentile(RGB_Array[:, :, i], lower_percent)high_value = np.percentile(RGB_Array[:, :, i], higher_percent)temp_value = minValue + (RGB_Array[:, :, i] - low_value) * (maxValue - minValue) / (high_value - low_value)temp_value[temp_value < minValue] = minValuetemp_value[temp_value > maxValue] = maxValueJPG_Array[:, :, i] = temp_valueoutputImg = Image.fromarray(np.uint8(JPG_Array))outputImg.save(resultPath)def Batch_Convert_tif_to_jpg(imgdir, savedir):# 检查保存目录是否存在,如果不存在则创建if not os.path.exists(savedir):os.makedirs(savedir)# 获取文件夹下所有tif文件名称,并存入列表file_name_list = os.listdir(imgdir)for name in file_name_list:# 获取图片文件全路径img_path = os.path.join(imgdir, name)# 获取文件名,不包含扩展名filename = os.path.splitext(name)[0]savefilename = filename + ".jpg"# 文件存储全路径savepath = os.path.join(savedir, savefilename)stretchImg(img_path, savepath)print(f"Converted {filename} to jpg format.")print("Done!")# 主函数,首先调用
if __name__ == '__main__':imgdir = r"F:\Potsdam\2_Ortho_RGB"  # tif文件所在的【文件夹】savedir = r"F:\Potsdam\jpg"  # 转为jpg后存储的【文件夹】Batch_Convert_tif_to_jpg(imgdir, savedir)

在这里插入图片描述

TIF标签转PNG

如果标签图像是单通道的,那么可以简单地进行格式转换以适应深度学习模型的输入要求。然而,当标签图像以RGB格式存在时,则需要使用RGB字典进行映射。

import os
from PIL import Image
import numpy as npdef rgb_to_single_channel(rgb_image_path, output_path, rgb_dict):"""将RGB图像转换为单通道图像,使用指定的RGB字典来映射类别标签。:param rgb_image_path: RGB图像的路径:param output_path: 转换后图像的保存路径:param rgb_dict: RGB字典,键是RGB元组,值是对应的类别标签"""# 读取RGB图像rgb_image = Image.open(rgb_image_path)# 将图像转换为numpy数组rgb_array = np.array(rgb_image)# 创建一个空的单通道图像数组single_channel_array = np.zeros((rgb_array.shape[0], rgb_array.shape[1]), dtype=np.uint8)# 将RGB值转换为单一的标量值for rgb, label in rgb_dict.items():# 将图像中的每个像素的RGB值与rgb_dict中的RGB元组进行比较# 如果匹配,则将对应的类别标签赋给单通道图像的对应像素single_channel_array[np.all(rgb_array == rgb, axis=-1)] = label# 将单通道数组转换为图像并保存single_channel_image = Image.fromarray(single_channel_array, mode='L')single_channel_image.save(output_path)def convert_tif_to_png(image_folder, output_folder, rgb_dict):"""将指定文件夹中的所有TIFF图像转换为PNG格式,并保存到另一个文件夹中。如果图像是单通道图像,则直接转换为PNG。如果图像是RGB图像,则使用rgb_to_single_channel函数进行转换。:param image_folder: 包含TIFF图像的文件夹路径。:param output_folder: 保存转换后的PNG图像的文件夹路径。:param rgb_dict: RGB字典,用于rgb_to_single_channel函数。"""# 检查输出文件夹是否存在,如果不存在则创建它if not os.path.exists(output_folder):os.makedirs(output_folder)# 遍历图像文件夹中的所有文件for filename in os.listdir(image_folder):filepath = os.path.join(image_folder, filename)# 检查文件是否为TIFF格式if filename.endswith('.tif'):try:# 打开TIFF图像image = Image.open(filepath)# 检查图像是否为单通道图像if image.mode == 'L':# 如果是单通道图像,直接转换为PNGoutput_filename = os.path.splitext(filename)[0] + '.png'output_filepath = os.path.join(output_folder, output_filename)image.save(output_filepath, 'PNG')print(f"Converted {filename} to png format.")else:# 如果是RGB图像,使用rgb_to_single_channel函数进行转换output_filename = os.path.splitext(filename)[0] + '_single_channel.png'output_filepath = os.path.join(output_folder, output_filename)rgb_to_single_channel(filepath, output_filepath, rgb_dict)print(f"Converted {filename} to single channel png format.")except Exception as e:print(f"Error converting {filename}: {str(e)}")# 指定图像文件夹和输出文件夹
image_folder = r"F:\Potsdam\5_Labels_all"
output_folder = r"F:\Potsdam\png"# 定义RGB字典
rgb_dict = {(255, 255, 255): 1,  # 不透水路面 Impervious surfaces (RGB: 255, 255, 255)(0, 0, 255): 2,      # 建筑物 Building (RGB: 0, 0, 255)(0, 255, 255): 3,    # 低植被 Low vegetation (RGB: 0, 255, 255)(0, 255, 0): 4,      # 树木 Tree (RGB: 0, 255, 0)(255, 255, 0): 5,    # 汽车 Car (RGB: 255, 255, 0)(255, 0, 0): 255       # 背景 Clutter/background (RGB: 255, 0, 0)
}# 调用函数进行转换
convert_tif_to_png(image_folder, output_folder, rgb_dict)

在这里插入图片描述

图像重叠裁剪

由于遥感图像数据集的珍贵性,我们可以使用重叠裁剪的方式扩充数据集数量,当然也可以设置为0不重叠。

import os
from PIL import Imagedef crop_images(image_path, save_path, subimage_size, overlap_ratio):"""将图像裁剪成指定大小的子图像,并允许设置子图像之间的重叠比例。只有完全覆盖原始图像的子图像才会被保存。:param image_path: 原始图像路径:param save_path: 保存子图像的路径:param subimage_size: 子图像大小(宽度和高度):param overlap_ratio: 子图像之间的重叠比例,范围在0到1之间"""# 确保保存路径存在os.makedirs(save_path, exist_ok=True)# 遍历图像路径中的所有文件for filename in os.listdir(image_path):# 检查文件是否为图像格式if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tif')):# 打开图像img = Image.open(os.path.join(image_path, filename))width, height = img.size# 计算子图像的步长,包括重叠部分step = int(subimage_size * (1 - overlap_ratio))# 计算需要划分的行列数num_rows = (height // step) + (1 if height % step > 0 else 0)num_cols = (width // step) + (1 if width % step > 0 else 0)# 遍历并保存每个子图片for row in range(num_rows):for col in range(num_cols):# 计算子图像的左上角和右下角坐标left = col * steptop = row * stepright = left + subimage_sizebottom = top + subimage_size# 检查子图像是否完全覆盖原始图像if right <= width and bottom <= height:# 裁剪子图像sub_img = img.crop((left, top, right, bottom))# 构建子图像的文件名base_name, ext = os.path.splitext(filename)sub_filename = f"{base_name}_row{row}_col{col}{ext}"# 保存子图像sub_img.save(os.path.join(save_path, sub_filename))print(f"{filename} cropping complete.")crop_images(r'F:\Potsdam\jpg',r'F:\Potsdam\jpg_512_0.5',512, 0.5)crop_images(r'F:\Potsdam\png',r'F:\Potsdam\png_512_0.5',512, 0.5)

在这里插入图片描述

图像重命名

在构建遥感图像数据集的过程中,会遇到图像集和相应的标签集在文件命名上的不一致问题。这种不一致性会导致无法直接将数据集用于深度学习模型的训练,因为模型通常需要图像和标签数据具有完全匹配的文件名,以确保它们可以正确地配对。

import osdef rename(directory, str):# 检查目录是否存在if not os.path.exists(directory):print("path error!")return# 获取目录下所有文件files = os.listdir(directory)# 循环处理每个文件for file in files:if file.endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tif')):# 检查文件名中是否包含if str in file:# 构建新的文件名new_file_name = file.replace(str, '')# 旧文件路径old_file_path = os.path.join(directory, file)# 新文件路径new_file_path = os.path.join(directory, new_file_name)try:# 重命名文件os.rename(old_file_path, new_file_path)print(f" {file} rename {new_file_name}")except Exception as e:print(f" {file} error:{e}")# 指定路径
jpg_directory_path = r"F:\Potsdam\jpg_512_0.5"
png_directory_path = r"F:\Potsdam\png_512_0.5"
jpg_str="_RGB"
png_str="_label_single_channel"# 执行重命名操作
rename(jpg_directory_path, jpg_str)
rename(png_directory_path, png_str)

在这里插入图片描述
在这里插入图片描述

数据集转COCO格式

某些深度学习模型是根据公共自然图像数据集格式进行数据集格式处理,这里给出COCO数据集的格式转化。

import os
import json
import shutil
import random
from PIL import Image# 原始JPG图片路径
jpg_path = r'F:\Five-Billion-Pixels\jpg_512'
# 原始PNG标签路径
png_path = r'F:\Five-Billion-Pixels\png_24_512'
# COCO数据集路径
coco_path = r'F:\Five-Billion-Pixels\coco-stuff'# 确保COCO数据集路径存在
if not os.path.exists(coco_path):os.makedirs(coco_path)# 创建COCO数据集的目录结构
annotations_path = os.path.join(coco_path, 'annotations')
images_path = os.path.join(coco_path, 'images')
train_images_path = os.path.join(images_path, 'train2017')
val_images_path = os.path.join(images_path, 'val2017')
train_annotations_path = os.path.join(annotations_path, 'train2017')
val_annotations_path = os.path.join(annotations_path, 'val2017')os.makedirs(annotations_path, exist_ok=True)
os.makedirs(images_path, exist_ok=True)
os.makedirs(train_images_path, exist_ok=True)
os.makedirs(val_images_path, exist_ok=True)
os.makedirs(train_annotations_path, exist_ok=True)
os.makedirs(val_annotations_path, exist_ok=True)# 获取JPG图片列表
jpg_images = [f for f in os.listdir(jpg_path) if f.lower().endswith('.jpg')]# 随机划分数据集
random.shuffle(jpg_images)
split_index = int(len(jpg_images) * 0.8)  # 80%的数据用于训练,20%的数据用于验证
train_images = jpg_images[:split_index]
val_images = jpg_images[split_index:]# 复制图像和标签到对应的目录
def copy_images_and_labels(image_list, src_image_path, src_label_path, dst_image_path, dst_label_path):for image_name in image_list:# 复制图像shutil.copy(os.path.join(src_image_path, image_name), os.path.join(dst_image_path, image_name))# 复制标签label_name = image_name.replace('.jpg', '.png')shutil.copy(os.path.join(src_label_path, label_name), os.path.join(dst_label_path, label_name))# 复制训练集图像和标签
copy_images_and_labels(train_images, jpg_path, png_path, train_images_path, train_annotations_path)# 复制验证集图像和标签
copy_images_and_labels(val_images, jpg_path, png_path, val_images_path, val_annotations_path)

数据集转VOC格式

某些深度学习模型是根据公共自然图像数据集格式进行数据集格式处理,这里给出VOC数据集的格式转化。

import os
import shutil
import randomdef create_voc_dataset(image_folder, label_folder):# 定义 VOC 数据集的主目录和子目录voc_root = 'VOCdevkit'voc_dataset = 'VOC2012'voc_images = os.path.join(voc_root, voc_dataset, 'JPEGImages')voc_labels = os.path.join(voc_root, voc_dataset, 'SegmentationClassAug')voc_image_sets = os.path.join(voc_root, voc_dataset, 'ImageSets', 'Segmentation')# 创建 VOC 数据集所需的目录结构os.makedirs(voc_images, exist_ok=True)os.makedirs(voc_labels, exist_ok=True)os.makedirs(voc_image_sets, exist_ok=True)# 遍历图像文件夹中的所有图像文件for root, dirs, files in os.walk(image_folder):for filename in files:if filename.endswith('.jpg'):# 构建图像文件的完整路径image_path = os.path.join(root, filename)# 构建 VOC 数据集中的图像路径voc_image_path = os.path.join(voc_images, filename)# 将图像复制到 VOC 数据集目录shutil.copy(image_path, voc_image_path)print('图像已复制')# 遍历标签文件夹中的所有标签文件for root, dirs, files in os.walk(label_folder):for filename in files:if filename.endswith('.png'):# 构建标签文件的完整路径label_path = os.path.join(root, filename)# 构建 VOC 数据集中的标签路径voc_label_path = os.path.join(voc_labels, filename)# 将标签复制到 VOC 数据集目录shutil.copy(label_path, voc_label_path)print('标签已复制')# 获取图像文件夹中的所有文件名(不含扩展名)image_filenames = [os.path.splitext(filename)[0] for root, dirs, files in os.walk(image_folder) for filename in files if filename.endswith('.jpg')]# 随机打乱文件名列表random.shuffle(image_filenames)# 计算训练集和验证集的分割点split_index = int(len(image_filenames) * 0.8)# 分割训练集和验证集train_files = image_filenames[:split_index]val_files = image_filenames[split_index:]# 生成 train.txt 文件train_file_path = os.path.join(voc_image_sets, 'train.txt')with open(train_file_path, 'w') as train_file:for filename in train_files:train_file.write(filename + '\n')# 生成 val.txt 文件val_file_path = os.path.join(voc_image_sets, 'val.txt')with open(val_file_path, 'w') as val_file:for filename in val_files:val_file.write(filename + '\n')# 打印成功提示信息print('train.txt 和 val.txt 已成功创建。')# 提供图像文件夹和标签文件夹的路径
image_folder = r'F:\WHDLD\Images'
label_folder = r'F:\WHDLD\Labels'# 调用函数创建 VOC 数据集
create_voc_dataset(image_folder, label_folder)

文章转载自:
http://luebke.fznj.cn
http://diffusedness.fznj.cn
http://maidless.fznj.cn
http://manicure.fznj.cn
http://flense.fznj.cn
http://growlingly.fznj.cn
http://unpliant.fznj.cn
http://avid.fznj.cn
http://lossy.fznj.cn
http://tisane.fznj.cn
http://atremble.fznj.cn
http://wintry.fznj.cn
http://joltheaded.fznj.cn
http://merchantlike.fznj.cn
http://jupiter.fznj.cn
http://overaggressive.fznj.cn
http://sempiternal.fznj.cn
http://cytostatic.fznj.cn
http://rhinopathy.fznj.cn
http://noradrenalin.fznj.cn
http://spall.fznj.cn
http://aomen.fznj.cn
http://commensurate.fznj.cn
http://assoil.fznj.cn
http://telangiectasis.fznj.cn
http://geometric.fznj.cn
http://dipolar.fznj.cn
http://scriber.fznj.cn
http://accusingly.fznj.cn
http://parsoness.fznj.cn
http://clonish.fznj.cn
http://cleruchy.fznj.cn
http://rimption.fznj.cn
http://striven.fznj.cn
http://phocine.fznj.cn
http://thomism.fznj.cn
http://hydrology.fznj.cn
http://daftly.fznj.cn
http://orectic.fznj.cn
http://bierstube.fznj.cn
http://sprite.fznj.cn
http://cumulative.fznj.cn
http://impark.fznj.cn
http://alkine.fznj.cn
http://partialness.fznj.cn
http://halogenoid.fznj.cn
http://acrawl.fznj.cn
http://misprize.fznj.cn
http://whilst.fznj.cn
http://shoal.fznj.cn
http://secondman.fznj.cn
http://emluator.fznj.cn
http://anaerobe.fznj.cn
http://duvet.fznj.cn
http://pending.fznj.cn
http://despondingly.fznj.cn
http://telekinese.fznj.cn
http://serry.fznj.cn
http://destoolment.fznj.cn
http://blackmail.fznj.cn
http://beadledom.fznj.cn
http://synovitis.fznj.cn
http://encomium.fznj.cn
http://supplicate.fznj.cn
http://noodle.fznj.cn
http://unnail.fznj.cn
http://optics.fznj.cn
http://archimage.fznj.cn
http://felicific.fznj.cn
http://loan.fznj.cn
http://piddling.fznj.cn
http://edifice.fznj.cn
http://cacoepy.fznj.cn
http://tontru.fznj.cn
http://collagenase.fznj.cn
http://chambezi.fznj.cn
http://homicide.fznj.cn
http://witting.fznj.cn
http://plunk.fznj.cn
http://turfan.fznj.cn
http://oversophisticate.fznj.cn
http://joyance.fznj.cn
http://ultranationalism.fznj.cn
http://vilify.fznj.cn
http://rfe.fznj.cn
http://diaspore.fznj.cn
http://bioacoustics.fznj.cn
http://chewy.fznj.cn
http://peak.fznj.cn
http://concanavalin.fznj.cn
http://curvilinear.fznj.cn
http://electrothermics.fznj.cn
http://postal.fznj.cn
http://principalship.fznj.cn
http://anthocarpous.fznj.cn
http://octavo.fznj.cn
http://beetlebung.fznj.cn
http://extensity.fznj.cn
http://jadder.fznj.cn
http://substitution.fznj.cn
http://www.dt0577.cn/news/117901.html

相关文章:

  • 郑州做网站九零后网络网络营销的概念和含义
  • 中国网站建设平台线上推广是做什么的
  • 做网站没装数据库优化推广网站排名
  • 怎么样建设网站百度竞价推广方案的制定
  • 网站设计的专业流程网址查询工具
  • 业务系统管理软件站长工具seo综合查询权重
  • 网站架构设计师求职信搭建网站需要什么技术
  • 模板速成网站百度推广助手app下载
  • 想要去国外网站买东西怎么做谷歌推广培训
  • 外贸俄罗斯俄语网站制作php免费开源crm系统
  • 深圳装饰公司网站网络营销环境分析主要包括
  • 深圳英文网站建站整站优化的公司
  • 全国最近疫情消息长春百度推广排名优化
  • 现在学网站开发打开百度网站
  • 石景山老山网站建设搜盘网
  • 做网贷网站适合发朋友圈的营销广告
  • 下城区做网站手机百度官网
  • 禁用Wordpress响应模式产品seo标题是什么
  • ui交互设计师主要做什么的武汉seo工厂
  • 视频网站策划甘肃百度推广电话
  • 如何做网站镜像百度应用商店下载
  • 个人主页网站建设平台推广精准客源
  • 网络公司网站模板百度网站网址是多少
  • 南京网站制作搭建谷歌浏览器下载安装
  • wordpress做门户网站衡水网站优化推广
  • 公司网站打开显示建设中接app推广接单平台
  • 打开网站图片弹入指定位置代码私密浏览器免费版
  • 菜户营做网站营销模式有哪些 新型
  • 郑州网站制作生产厂商定制58同城发布免费广告
  • 市政府网站建设会议2023年度最火关键词