-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathlevir_cd_256.m
173 lines (153 loc) · 5 KB
/
levir_cd_256.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
%Dataset preparation code for DSFIN dataset (MATLAB)
%Download LEVIR dataset here: https://www.dropbox.com/s/h9jl2ygznsaeg5d/LEVIR-CD-256.zip
%This code generate 256x256 image partches required for the train/val/test
%Please create folders according to following format.
%DSIFN-CD-256
%------(train)
% |---> A
% |---> B
% |---> label
%------(val)
% |---> A
% |---> B
% |---> label
%------(test)
% |---> A
% |---> B
% |---> label
%Then run this code
%Then copy all images in train-A, val-A, test-A to a folder name A
%Then copy all images in train-B, val-B, test-B to a folder name B
%Then copy all images in train-label, val-label, test-label to a folder name label
clear all;
close all;
clc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Train-A
imgs_name = struct2cell(dir('LEVIR-CD/train/A/*.png'));
for i=1:1:length(imgs_name)
img_file_name = imgs_name{1,i};
temp = imread(strcat('LEVIR-CD/train/A/',img_file_name));
c=1;
for j=1:4
for k=1:4
patch = temp((j-1)*256+1: j*256, (k-1)*256+1: k*256, :);
imwrite(patch, strcat('LEVIR-CD256/train/A/', img_file_name(1:end-4), '_', num2str(c), '.png'));
c=c+1;
end
end
end
%Train-B
imgs_name = struct2cell(dir('LEVIR-CD/train/B/*.png'));
for i=1:1:length(imgs_name)
img_file_name = imgs_name{1,i};
temp = imread(strcat('LEVIR-CD/train/B/',img_file_name));
c=1;
for j=1:4
for k=1:4
patch = temp((j-1)*256+1: j*256, (k-1)*256+1: k*256, :);
imwrite(patch, strcat('LEVIR-CD256/train/B/', img_file_name(1:end-4), '_', num2str(c), '.png'));
c=c+1;
end
end
end
%Train-label
imgs_name = struct2cell(dir('LEVIR-CD/train/label/*.png'));
for i=1:1:length(imgs_name)
img_file_name = imgs_name{1,i};
temp = imread(strcat('LEVIR-CD/train/label/',img_file_name));
c=1;
for j=1:4
for k=1:4
patch = temp((j-1)*256+1: j*256, (k-1)*256+1: k*256, :);
imwrite(patch, strcat('LEVIR-CD256/train/label/', img_file_name(1:end-4), '_', num2str(c), '.png'));
c=c+1;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Test-A
imgs_name = struct2cell(dir('LEVIR-CD/test/A/*.png'));
for i=1:1:length(imgs_name)
img_file_name = imgs_name{1,i};
temp = imread(strcat('LEVIR-CD/test/A/',img_file_name));
c=1;
for j=1:4
for k=1:4
patch = temp((j-1)*256+1: j*256, (k-1)*256+1: k*256, :);
imwrite(patch, strcat('LEVIR-CD256/test/A/', img_file_name(1:end-4), '_', num2str(c), '.png'));
c=c+1;
end
end
end
%Test-B
imgs_name = struct2cell(dir('LEVIR-CD/test/B/*.png'));
for i=1:1:length(imgs_name)
img_file_name = imgs_name{1,i};
temp = imread(strcat('LEVIR-CD/test/B/',img_file_name));
c=1;
for j=1:4
for k=1:4
patch = temp((j-1)*256+1: j*256, (k-1)*256+1: k*256, :);
imwrite(patch, strcat('LEVIR-CD256/test/B/', img_file_name(1:end-4), '_', num2str(c), '.png'));
c=c+1;
end
end
end
%Test-label
imgs_name = struct2cell(dir('LEVIR-CD/test/label/*.png'));
for i=1:1:length(imgs_name)
img_file_name = imgs_name{1,i};
temp = imread(strcat('LEVIR-CD/test/label/',img_file_name));
c=1;
for j=1:4
for k=1:4
patch = temp((j-1)*256+1: j*256, (k-1)*256+1: k*256, :);
imwrite(patch, strcat('LEVIR-CD256/test/label/', img_file_name(1:end-4), '_', num2str(c), '.png'));
c=c+1;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%val-A
imgs_name = struct2cell(dir('LEVIR-CD/val/A/*.png'));
for i=1:1:length(imgs_name)
img_file_name = imgs_name{1,i};
temp = imread(strcat('LEVIR-CD/val/A/',img_file_name));
c=1;
for j=1:4
for k=1:4
patch = temp((j-1)*256+1: j*256, (k-1)*256+1: k*256, :);
imwrite(patch, strcat('LEVIR-CD256/val/A/', img_file_name(1:end-4), '_', num2str(c), '.png'));
c=c+1;
end
end
end
%val-B
imgs_name = struct2cell(dir('LEVIR-CD/val/B/*.png'));
for i=1:1:length(imgs_name)
img_file_name = imgs_name{1,i};
temp = imread(strcat('LEVIR-CD/val/B/',img_file_name));
c=1;
for j=1:4
for k=1:4
patch = temp((j-1)*256+1: j*256, (k-1)*256+1: k*256, :);
imwrite(patch, strcat('LEVIR-CD256/val/B/', img_file_name(1:end-4), '_', num2str(c), '.png'));
c=c+1;
end
end
end
%val-label
imgs_name = struct2cell(dir('LEVIR-CD/val/label/*.png'));
for i=1:1:length(imgs_name)
img_file_name = imgs_name{1,i};
temp = imread(strcat('LEVIR-CD/val/label/',img_file_name));
c=1;
for j=1:4
for k=1:4
patch = temp((j-1)*256+1: j*256, (k-1)*256+1: k*256, :);
imwrite(patch, strcat('LEVIR-CD256/val/label/', img_file_name(1:end-4), '_', num2str(c), '.png'));
c=c+1;
end
end
end