给的PAN数据没有Resize,于是选择结合这段代码
#!/usr/bin/env python
# coding=utf-8
'''
@Author: wjm
@Date: 2020-06-13 20:12:23
LastEditTime: 2024-04-08 10:34:01
@Description: file content
'''
import numpy as np
import glob, os, h5py
import os
import cv2
from scipy import misc
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
class image_to_patch:
def __init__(self, patch_size, scale, ms_path, ms_image_path, pan_path, pan_image_path):
self.stride = patch_size
self.scale = scale
self.ms_path = ms_path
self.ms_image_path = ms_image_path
self.pan_path = pan_path
self.pan_image_path = pan_image_path
if not os.path.exists(ms_image_path):
os.mkdir(ms_image_path)
if not os.path.exists(pan_image_path):
os.mkdir(pan_image_path)
def to_patch(self):
#read MS image, CMYK space
img_ms = self.imread(self.ms_path)
img_ms = self.modcrop(img_ms, self.scale)
#read PAN image, Gray space
img_pan = self.imread(self.pan_path)
img_pan = self.modcrop(img_pan, self.scale)
# high, width
h, w = img_ms.size
n_ms = 1
n_pan = 1
# MS image
for x in range(0, h - self.stride, self.stride):
for y in range(0, w - self.stride, self.stride):
box = [x, y, x + self.stride, y + self.stride]
sub_img_label = img_ms.crop(box)
sub_img_label.save(os.path.join(self.ms_image_path, str(n_ms)+'.tif'))
n_ms = n_ms + 1
# PAN image
for x in range(0, h - self.stride, self.stride):
for y in range(0, w - self.stride, self.stride):
box = [x, y, x + self.stride, y + self.stride]
sub_img_label = img_pan.crop(box)
sub_img_label.save(os.path.join(self.pan_image_path, str(n_pan)+'.tif'))
n_pan = n_pan + 1
def imread(self, path):
img = Image.open(path)
return img
def modcrop(self, img, scale =3):
h, w = img.size
h = (h // scale) * scale
w = (w // scale) * scale
box=(0,0,h,w)
img = img.crop(box)
return img
class downsample:
def __init__(self, pan_pach, scale, d_pan_path):
image = Image.open(pan_pach).convert('L')
d_image = image.resize((int(image.size[0]/scale),int(image.size[1]/scale)), Image.BICUBIC)
d_image.save(d_pan_path)
if __name__ == '__main__':
image_size = 1024
scale = 4
ms_pach = r'../MS.tif'
ms_image_path = r'./ms'
pan_pach = r'H:\数据集\4 WorldView-4\pan_8' ### PAN图像存在的地方
pan_image_path = r'./pan' ###
# downsample pan image
for i in range(0,500):
pan_pachs = os.path.join(pan_pach, str(i)+'.tif')
downsample(pan_pachs, scale, os.path.join(r'H:\数据集\4 WorldView-4\pan', str(i)+'.tif'))
# image to patch
# task = image_to_patch(image_size, scale, ms_pach, ms_image_path, pan_pach, pan_image_path)
# task.to_patch()
制作MTF图像,要注意sensor,和每次数据集里面的图片数量
%% MTF filters the image I_MS using a Gaussin filter matched with the Modulation Transfer Function (MTF) of the MultiSpectral (MS) sensor.
clear; close all;
addpath Tools;
path = 'H:\数据集\1 IKONOS\';
sensor = 'IKONOS'; %% QB, IKONOS, GeoEye1, WV4, WV2, WV3
scale = 4;
for i = 0 : 199
path_ms = strcat(path, 'ms\', num2str(i), '.tif');
ms = imread(path_ms);
image_ms = double(ms);
image_ms = MTF(image_ms, sensor ,scale);
if exist(strcat(path,'/mtf/')) == 0
mkdir(strcat(path,'/mtf/'));
end
str1 = strcat(path, '/mtf/', num2str(i), '.tif');
imwrite(uint8(image_ms), str1, 'tif');
end
学到了
锤死你