近年来,深度学习在遥感影像地物分类中取得了一系列显著的效果。CNN可以很好的获取影像纹理信息,捕捉像素与像素之间的空间特征,因此,一个训练好的深度学习模型在地物提取中具有很大的优势。但模型的训练却是一个很繁琐的任务,需要人工准备数据集,贴标签,训练模型等。本文将以sar影像为例实现冰水二分类的数据集批量准备工作(划线取点截取小图片保存):
2.预处理思路:
a.人工划线:对应在冰和水上画n条线(自己设置,注意自己需要针对类别所占比例控制线条数量和长度)
b.保存小图片:获取直线上点坐标,以每个像素点为中心取21×21的小图片(类似mnist数据集,尺寸自己设置),保存至文件夹
c. 创建label:以保存的小图片名称+空格+类别(0或者1)将label保存至新创建的txt文档中
3.代码实现:
a.创建一个main函数调用drawTrainingSamples(img);CreateTrainSmallImages(img);drawValSamples(img);CreateValSmallImages(img);这四个函数,功能分别是和划训练集,创建训练集,划验证集,创建验证集
1
2
3
4
5
6
7
8
9
10
11
|
clear
;
clc
;
img =
imread
(
'150905_multilook_4_s1a-ew-grd-hv-20150905t174712-20150905t174812-007583-00a7f0-002.tiff'
);
%准备训练集数据
drawTrainingSamples(img);
CreateTrainSmallImages(img);
%准备验证集数据
drawValSamples(img);
CreateValSmallImages(img);
|
b.drawTrainingSamples(img)
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
|
function
[] = drawTrainingSamples(img)
n_ice=4;
n_water=4;
h_im=imshow(img);
bw_train_ice=
zeros
(
size
(img));
bw_train_water=
zeros
(
size
(img));
fprintf
(
'please draw four lines on the picture for preparing the training sets of Ice'
);
for
i
= 1:n_ice
h = imline;
bw = createMask(h,h_im);
bw_train_ice=bw_train_ice+bw;
end
figure
,imshow(bw_train_ice);
h_im=imshow(img);
fprintf
(
'please draw four lines on the picture for preparing the training sets of Water'
);
for
i
= 1:n_water
h = imline;
bw = createMask(h,h_im);
bw_train_water=bw_train_water+bw;
end
figure
,imshow(bw_train_water);
save
(
'bw_train_ice.mat'
,
'bw_train_ice'
);
save
(
'bw_train_water.mat'
,
'bw_train_water'
);
|
c.CreateTrainSmallImages(img)
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
|
function
[] = CreateTrainSmallImages(img)
%创建小图片
load
bw_train_ice;
load
bw_train_water;
fprintf
(
'Creating training small images...'
);
[X,Y]=
find
(bw_train_ice==1);
A=[X,Y];
A;
[a,b]=
size
(A);
mkdir
(
'train'
);
for
i
=1:a
m=A(
i
,1);
n=A(
i
,2);
SmallImage=img(m-10:m+10,n-10:n+10);
imwrite
(SmallImage,[
'train/'
,
num2str
(
i
),
'.jpg'
]);
fid =
fopen
(
'train.txt'
,
'a'
);
t=[
num2str
(
i
),
'.jpg'
];
fprintf
(fid,
'%s %d \n'
, t,0);
fclose
(fid);
end
[X,Y]=
find
(bw_train_water==1);
B=[X,Y];
B;
[a,b]=
size
(B);
for
j
=1:a
m=B(
j
,1);
n=B(
j
,2);
SmallImage=img(m-10:m+10,n-10:n+10);
j
=
i
+
j
;
imwrite
(SmallImage,[
'train/'
,
num2str
(
j
),
'.jpg'
]);
fid =
fopen
(
'train.txt'
,
'a'
);
t=[
num2str
(
j
),
'.jpg'
];
fprintf
(fid,
'%s %d \n'
, t,1);
fclose
(fid);
end
end
|
d.drawValSamples(img)
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
|
function
[] = drawValSamples(img)
n_ice=4;
n_water=4;
h_im=imshow(img);
bw_val_ice=
zeros
(
size
(img));
bw_val_water=
zeros
(
size
(img));
fprintf
(
'please draw four lines on the picture for preparing the validition sets of Ice'
);
for
i
= 1:n_ice
h = imline;
bw = createMask(h,h_im);
bw_val_ice=bw_val_ice+bw;
end
figure
,imshow(bw_val_ice);
h_im=imshow(img);
fprintf
(
'please draw four lines on the picture for preparing the validition sets of Water'
);
for
i
= 1:n_water
h = imline;
bw = createMask(h,h_im);
bw_val_water=bw_val_water+bw;
end
figure
,imshow(bw_val_water);
save
(
'bw_val_ice.mat'
,
'bw_val_ice'
);
save
(
'bw_val_water.mat'
,
'bw_val_water'
);
|
e.CreateValSmallImages(img)
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
|
function
[] = CreateValSmallImages(img)
%创建小图片
load
bw_val_ice;
load
bw_val_water;
[X,Y]=
find
(bw_val_ice==1);
A=[X,Y];
A;
[a,b]=
size
(A);
mkdir
(
'val'
);
fprintf
(
'Creating validition sets small images...'
);
for
i
=1:a
m=A(
i
,1);
n=A(
i
,2);
SmallImage=img(m-10:m+10,n-10:n+10);
imwrite
(SmallImage,[
'val/'
,
num2str
(
i
),
'.jpg'
]);
fid =
fopen
(
'val.txt'
,
'a'
);
t=[
num2str
(
i
),
'.jpg'
];
fprintf
(fid,
'%s %d \n'
, t,0);
fclose
(fid);
end
[X,Y]=
find
(bw_val_water==1);
B=[X,Y];
B;
[a,b]=
size
(B);
for
j
=1:a
m=B(
j
,1);
n=B(
j
,2);
SmallImage=img(m-10:m+10,n-10:n+10);
j
=
i
+
j
;
imwrite
(SmallImage,[
'val/'
,
num2str
(
j
),
'.jpg'
]);
fid =
fopen
(
'val.txt'
,
'a'
);
t=[
num2str
(
j
),
'.jpg'
];
fprintf
(fid,
'%s %d \n'
, t,1);
fclose
(fid);
end
end
|