-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConvertHStoHD5_31channel_31dim.m
68 lines (45 loc) · 2.49 KB
/
ConvertHStoHD5_31channel_31dim.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
function ConvertHStoHD5_31channel_31dim(local_HS_data,local_HS_label,local_size_data,local_size_label,label_dimension,data_dimension,local_stride,chunksz,local_amount_hd5_image,filename)
%% global variable to determine the numbers of images included by each hdf5 file
global FILE_COUNT;
global TOTALCT;
global CREATED_FLAG;
%% initialization
data = zeros(local_size_data,local_size_data, data_dimension, 1);
label = zeros(local_size_data, local_size_data, label_dimension, 1);
padding = abs(local_size_data - local_size_label)/2;
count = 0;
% local_HS_label=local_HS_label/max(local_HS_label(:)); % normalize HS_Label
%% juding if exceed the amount ,which is the numbers of images included by each hdf5 file.
FILE_COUNT=FILE_COUNT+1;
if FILE_COUNT >local_amount_hd5_image
FILE_COUNT=1;
CREATED_FLAG = false;
TOTALCT=0;
end
%% loading all the .mat will need a for loop
[img_width,img_height,img_channel] = size(local_HS_label); % the choosed label and data are same size
for x = 1 : local_stride : img_width-local_size_data+1
for y = 1 :local_stride : img_height-local_size_data+1
subim_input = local_HS_data(x : x+local_size_data-1, y : y+local_size_data-1,:);
subim_label = local_HS_label(x+padding : x+padding+local_size_label-1, y+padding : y+padding+local_size_label-1,:);
count=count+1;
data(:, :,:,count) = subim_input;
label(:, :,:,count) = subim_label;
end
end
order = randperm(count);
data = data(:, :, :, order);
label = label(:, :, :, order);
clear subim_input;
clear subim_label;
%% writing to HDF5
for batchno = 1:floor(count/chunksz)
last_read=(batchno-1)*chunksz;
batchdata = data(:,:,:,last_read+1:last_read+chunksz);
batchlabs = label(:,:,:,last_read+1:last_read+chunksz);
startloc = struct('dat',[1,1,1,TOTALCT+1], 'lab', [1,1,1,TOTALCT+1]);
curr_dat_sz = store2hdf5(filename, batchdata, batchlabs, ~CREATED_FLAG, startloc, chunksz); % the flag affects whether append to the previous file
CREATED_FLAG = true;
TOTALCT = curr_dat_sz(end);
end
h5disp(filename);