-
Notifications
You must be signed in to change notification settings - Fork 1
/
img_process.py
1641 lines (1543 loc) · 83.6 KB
/
img_process.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# preprocess img
# 20180916
import os
import cv2
import numpy as np
from tqdm import tqdm
from libtiff import TIFF
from scipy import misc
import shutil
def gen_destfolder(src_basepath: str, dst_basepath=' '):
src_category = []
for entry in os.scandir(src_basepath):
if entry.is_dir() and entry.name != 'out_dataset':
src_category.append(entry.name)
if dst_basepath == ' ':
dst_basepath = os.path.join(src_basepath, 'out_dataset')
if not os.path.exists(dst_basepath):
os.makedirs(dst_basepath)
return src_category, dst_basepath
def img_gray_with_category(src_basepath: str, dst_basepath, src_category='all', min_Width=1, min_Heigh=1, ext=['png']):
"""
do graying on RGB images and save them
:param src_basepath: source_path/category/image files
:param src_category: list of the names of imgs' categories, which are also the sub folders' name of src_basepath
Eg. ['a', 'b'], default to 'all', which means all sub folders will be processed
:param dst_basepath: gray imgs' storaged path
:param min_Heigh: the minimal images' height that will be grayed
:param min_Width: the maximal images' width that will be grayed
:param ext: the extension of the images to be grayed
:return True
"""
if not os.path.exists(src_basepath):
raise FileExistsError('path not found! : %s' % src_basepath)
os.makedirs(dst_basepath, exist_ok=True)
for this_cate in os.scandir(src_basepath):
if this_cate.is_dir():
if src_category == 'all' or this_cate.name in [src_category]:
this_cate_srcpath = os.path.join(src_basepath, this_cate)
this_cate_dstpath = os.path.join(dst_basepath, this_cate.name+'_gray')
os.makedirs(this_cate_dstpath, exist_ok=True)
pbar = tqdm(os.scandir(this_cate_srcpath))
for entry in pbar:
pbar.set_description("Processing %s" % entry.path)
if entry.is_file():
# get the file's extension
extension = os.path.splitext(entry.path)[1][1:]
if extension in ext:
# print("find png file: %s" % entry.name)
# gray img
tmp_img = cv2.imread(entry.path)
# print(entry.path)
if len(tmp_img.shape) == 3:
img_h, img_w, img_c = tmp_img.shape
if img_h>=min_Heigh and img_w>=min_Width:
if img_c==3: # RGB图像
tmp_gray_img = np.zeros([img_h, img_w], dtype=np.float32)
for ind_h in range(img_h):
for ind_w in range(img_w):
tmp_gray_img[ind_h][ind_w] = np.int(0.39 * tmp_img[ind_h][ind_w][0] +
0.5 * tmp_img[ind_h][ind_w][1] +
0.11 * tmp_img[ind_h][ind_w][2])
# gray = 0.39 * R + 0.5 * G + 0.11 * B
# save gray img
# print("\tsaving gray img: %s ..." % entry.name)
cv2.imwrite(os.path.join(this_cate_dstpath, entry.name), tmp_gray_img)
return True
def img_gray(source_path: str, save_path, extensions=['png']):
"""
do graying on RGB image and save them
:param source_path: folder's path that stores original RGB images files, struction source_path/images
:param save_path: gray images save path
:param extensions: the extension of the images to be grayed
:return True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
os.makedirs(save_path, exist_ok=True)
pbar = tqdm(os.scandir(source_path))
for img_file in pbar:
if img_file.is_file():
extension = os.path.splitext(img_file.path)[1][1:]
if extension in extensions:
pbar.set_description("Processing %s" % img_file.name)
# gray img
tmp_img = cv2.imread(img_file.path, -1)
# print(entry.path)
if len(tmp_img.shape) == 3:
img_h, img_w, img_c = tmp_img.shape
if img_c == 3:
tmp_gray_img = np.zeros([img_h, img_w], dtype=np.float32)
for ind_h in range(img_h):
for ind_w in range(img_w):
tmp_gray_img[ind_h][ind_w] = np.int(0.39 * tmp_img[ind_h][ind_w][0] +
0.5 * tmp_img[ind_h][ind_w][1] +
0.11 * tmp_img[ind_h][ind_w][2])
# gray = 0.39 * R + 0.5 * G + 0.11 * B
# save gray img
# print("\tsaving gray img: %s ..." % entry.name)
cv2.imwrite(os.path.join(save_path, img_file.name), tmp_gray_img)
is_successful = True
return True
def gen_npy_file(source_path, save_path, npy_file_name, img_extension='png'):
"""
concat images data with only one channel in soruce path and generate npy file
:param source_path: source_path/image files
:param save_path: npy file's save path
:param npy_file_name:
:param img_extension: source images' file extension
:return: Ture
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
pbar = tqdm(os.scandir(source_path))
is_first = True
total_data = []
for img_files in pbar:
if img_files.is_file():
extension = os.path.splitext(img_files.path)[1][1:]
if extension == img_extension:
pbar.set_description("Processing %s" % img_files.name)
if extension == 'npy':
source_img = np.load(img_files.path)
else:
source_img = cv2.imread(img_files.path, -1)
if is_first:
total_data = np.reshape(source_img, [1, -1])
is_first = False
else:
total_data = np.concatenate([total_data, np.reshape(source_img, [1, -1])], axis=0)
os.makedirs(save_path, exist_ok=True)
np.save(os.path.join(save_path, npy_file_name), total_data)
return True
# crop
def __img_crop_from_center(original_img, crop_width, crop_height):
"""
split a image with fixed size centrally
:param original_img: the original img data matrix
:param crop_width: the sample image's width
:param crop_height: the sample image's height
:return if successful, return splited image data, else False
"""
if len(original_img.shape) == 3:
ori_height, ori_width, chanels = original_img.shape
if (ori_width >= crop_width) and (ori_height >= crop_width):
up_left_raw = int((ori_height - crop_height) / 2)
up_left_col = int((ori_width - crop_width) / 2)
crop_img = original_img[
up_left_raw:up_left_raw+crop_height,
up_left_col:up_left_col+crop_width,
:]
return crop_img
else:
ori_height, ori_width= original_img.shape
if (ori_width >= crop_width) and (ori_height >= crop_width):
up_left_raw = int((ori_height - crop_height) / 2)
up_left_col = int((ori_width - crop_width) / 2)
crop_img = original_img[
up_left_raw:up_left_raw+crop_height,
up_left_col:up_left_col+crop_width]
return crop_img
return False
def crop_imgs_and_save_smaller(source_path, save_path, crop_height: int, crop_width: int, extension='png'):
"""
crop fix sized images and save them, also save the pictures whose size is smaller than crop window's size
:param source_path: source images path: the struct should be source_path/category_folder/images files
:param save_path: the processed images' save path
:param crop_height: crop window's height
:param crop_width: crop window's width
:param extension: source images' file extension
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
for category in os.scandir(source_path):
if category.is_dir():
os.makedirs(os.path.join(save_path, 'h'+str(crop_height)+'w'+str(crop_width), category.name), exist_ok=True)
os.makedirs(os.path.join(save_path, 'smaller_than_crop_window', 'h'+str(crop_height)+'w'+str(crop_width),
category.name), exist_ok=True)
pbar = tqdm(os.scandir(category.path))
for img_files in pbar:
this_extension = os.path.splitext(img_files.path)[1][1:]
if this_extension == extension:
pbar.set_description("Processing %s" % img_files.name)
img_data = cv2.imread(img_files.path, -1)
img_height = img_data.shape[0]
img_width = img_data.shape[1]
filename_no_extension, _ = os.path.splitext(img_files.name)
if img_height >= crop_height and img_width >= crop_width:
crop_img_data = __img_crop_from_center(img_data, crop_width, crop_height)
cv2.imwrite(os.path.join(save_path, 'h'+str(crop_height)+'w'+str(crop_width), category.name,
filename_no_extension+'.png'), crop_img_data)
else:
cv2.imwrite(
os.path.join(save_path, 'smaller_than_crop_window',
'h'+str(crop_height)+'w'+str(crop_width), category.name,
filename_no_extension + '.png'), img_data)
return True
# padding
# def img_padding_with_category(src_basepath: str, src_category=None, dst_basepath=None, out_size=[[64, 64],[128, 128]],
# ext=['png'], is_save_img = False):
# """
# padding zero to src imgs to get fix sized imgs, and save as npy files
# :param src_basepath: folder's path that stores original images files, src_basepath/category/image files
# :param src_category: the names of imgs' categories, which are also the sub folders' name of src_basepath
# :param dst_basepath: processed imgs' storaged path, default to src_basepath/out_dataset
# :param out_size: every row represent one of the images' out size :[height, width],
# and the row's index bigger, the size bigger.
# :param ext: the extension of the images to be processed
# :param is_save_img: whether to save processed images, default to False
# :return
# """
# # if source images' stored folder not exist, raise error
# if not os.path.exists(src_basepath):
# raise FileExistsError('path not found! : %s' % src_basepath)
# # if the source images' category information is not provided, then generate it.
# if src_category is None:
# src_category = []
# for entry in os.scandir(src_basepath):
# if entry.is_dir() and entry.name != 'out_dataset':
# src_category.append(entry.name)
# # if the after-processed images' stored folder is not provided, then set it to src_basepath\out_dataset
# if dst_basepath is None:
# dst_basepath = os.path.join(src_basepath, 'out_dataset')
# # generate folder to save images with size of outsize1 or outsize2
# num_outsize = np.array(out_size).shape[0]
# path_size = []
# for ind_size in range(num_outsize):
# path_size.append(os.path.join(dst_basepath, ('%d_%d' % (out_size[ind_size][0], out_size[ind_size][1]))))
# os.makedirs(path_size[ind_size], exist_ok=True) # if exist, don't raise exception
# # do padding
# for this_cate in src_category:
# this_cate_srcpath = os.path.join(src_basepath, this_cate)
# pbar = tqdm(os.scandir(this_cate_srcpath))
# for entry in pbar:
# pbar.set_description("Processing %s" % entry.name)
# if entry.is_file():
# # get the file's extension
# extension = os.path.splitext(entry.path)[1][1:]
# if extension in ext:
# # print('find image : %s' % entry.path)
# # if extension == 'tiff':
# # tif = TIFF.open(entry.path, mode='r')
# # tmp_img = tif.read_image()
# # tif.close
# # else:
# # tmp_img = cv2.imread(entry.path, cv2.IMREAD_GRAYSCALE)
# tmp_img = cv2.imread(entry.path, -1) # <0 returns the image as is
# # print(tmp_img[85][108])
# # print("image's shape:")
# # print(tmp_img.shape)
# # print(len(tmp_img.shape))
# # img_h, img_w = tmp_img.shape
# # this_size = 0
# # for ind_size in range(num_outsize):
# # if img_h <= out_size[ind_size][0] and img_w <= out_size[ind_size][1]:
# # break
# # this_size += 1
# # # print('out image size index : %d' % this_size)
# # if this_size < num_outsize: # image with valid size
# # # print('gray image')
# # save_img = np.zeros([out_size[this_size][0], out_size[this_size][1]], dtype=np.uint16)
# # start_r = int(np.floor((out_size[this_size][0]-img_h)/2.0))
# # start_c = int(np.floor((out_size[this_size][1]-img_w)/2.0))
# # for ind_r in range(img_h):
# # for ind_c in range(img_w):
# # save_img[start_r+ind_r, start_c+ind_c] = tmp_img[ind_r, ind_c]
# is_valid, save_img, this_size = __matrix_padding(tmp_img, out_size=out_size, dtype=np.uint16)
# if is_valid:
# if is_save_img:
# os.makedirs(os.path.join(path_size[this_size], this_cate), exist_ok=True)
# cv2.imwrite(os.path.join(path_size[this_size], this_cate, entry.name), save_img)
# return
def padding_images_with_zero(
source_path, save_path, out_size=[[32, 32], [64, 64], [128, 128], [256, 256], [512, 512]],
image_extension='png', is_save_npy=False):
"""
padding images with zeros to get required size images, the final image's size is determined by it original size.
Eg. when the out_size is [[32, 32], [64, 64]]
if the original size is [h<=32, w<=32], then final size is [32, 32]
[h<=64, 32<w<=64] or [32<h<=64, w<=64] --> [64, 64]
[h>64, w] or [h, w>64] --> will not be processed sand saved
:param source_path: the original images' path, source path/image files
:param save_path: the output images' save path
:param out_size: the output image size, Eg. [[32, 32], [64, 64], ...]
:param image_extension: image file's extension, default to 'png', also support 'npy'
:param is_save_npy: bool. if to save npy files, default to false
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
os.makedirs(save_path, exist_ok=True)
pbar = tqdm(os.scandir(source_path))
for img_files in pbar:
if img_files.is_file():
extension = os.path.splitext(img_files.path)[1][1:]
if extension == image_extension:
pbar.set_description("Processing %s" % img_files.path)
if extension == 'npy':
img_data = np.load(img_files.path)
else:
img_data = cv2.imread(img_files.path, -1)
is_valid, out_img, _ = __matrix_padding_multi_size_soft(img_data, out_size=out_size)
img_filename, _ = os.path.splitext(img_files.name)
if is_valid:
cv2.imwrite(os.path.join(save_path, img_filename+'.png'), out_img)
if is_save_npy:
np.save(os.path.join(save_path, img_filename+'.npy'), out_img)
def padding_images_with_zero_with_category(
source_path, save_path, out_size=[[32, 32], [64, 64], [128, 128], [256, 256], [512, 512]],
image_extension='png', is_save_npy=False):
"""
padding images with zeros to get required size images, the final image's size is determined by it original size.
Eg. when the out_size is [[32, 32], [64, 64]]
if the original size is [h<=32, w<=32], then final size is [32, 32]
[h<=64, 32<w<=64] or [32<h<=64, w<=64] --> [64, 64]
[h>64, w] or [h, w>64] --> will not be processed and saved
:param source_path: the original images' path, source path/category/image files
:param save_path: the output images' save path
:param out_size: the output image size, Eg. [[32, 32], [64, 64], ...]
:param image_extension: image file's extension, default to 'png', also support 'npy'
:param is_save_npy: bool. if to save npy files, default to false
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
for category in os.scandir(source_path):
if category.is_dir():
padding_images_with_zero(category.path, os.path.join(save_path, category.name), out_size, image_extension,
is_save_npy)
return True
def padding_images_with_zero_to_square_size(source_path, save_path, image_extension='png'):
"""
padding images with zeros to get squared images, the output image's size is determined by it's larger side:
[out_h, out_w] = [max(w, h), max(w, h)]
:param source_path: the original images' path, source path/image files
:param save_path: the output images' save path
:param image_extension: image file's extension, default to 'png', also support 'npy'
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
os.makedirs(save_path, exist_ok=True)
pbar = tqdm(os.scandir(source_path))
for img_files in pbar:
if img_files.is_file():
extension = os.path.splitext(img_files.path)[1][1:]
if extension == image_extension:
pbar.set_description("Processing %s" % img_files.path)
if extension == 'npy':
img_data = np.load(img_files.path)
else:
img_data = cv2.imread(img_files.path, -1)
is_valid, out_img = __matrix_padding(img_data, out_size=[np.max(img_data.shape),
np.max(img_data.shape)]) # suppose c<= w or h
if is_valid:
cv2.imwrite(os.path.join(save_path, img_files.name), out_img)
def padding_images_with_zero_to_square_size_with_category(source_path, save_path, image_extension='png'):
"""
padding images with zeros to get squared images, the output image's size is determined by it's larger side:
[out_h, out_w] = [max(w, h), max(w, h)]
:param source_path: the original images' path, source path/category/image files
:param save_path: the output images' save path
:param image_extension: image file's extension, default to 'png', also support 'npy'
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
os.makedirs(save_path, exist_ok=True)
for category in os.scandir(source_path):
if category.is_dir():
padding_images_with_zero_to_square_size(
source_path=category.path,
save_path=os.path.join(save_path, category.name),
image_extension=image_extension
)
def __matrix_padding_multi_size_soft(src_mat, out_size=[[64, 64], [128, 128]], dtype=np.float32):
"""
padding zero to src matrix to get larger size matrix
if and only if source image's width and height are both small than out's, do padding
:param src_mat: source matrix
:param out_size: every row represent one of the images' out size :[height, width],
and the row's index bigger, the size bigger.
:param dtype: the src matrix's date type, default to np.float32
:return is_valid: if src_mat's size is smaller than out's , is_valid set to be Ture, else Flase
:return out_mat: if is_valid is true, then out_mat be useful, else will be useless.
"""
num_outsize = np.array(out_size).shape[0]
img_c = None
if len(src_mat.shape) == 2:
img_h, img_w = src_mat.shape
else:
img_h, img_w, img_c = src_mat.shape
this_size = 0
for ind_size in range(num_outsize):
if img_h <= out_size[ind_size][0] and img_w <= out_size[ind_size][1]:
break
this_size += 1
# print('out image size index : %d' % this_size)
is_valid = False
if this_size < num_outsize: # image with valid size
if img_c is None:
out_mat = np.zeros([out_size[this_size][0], out_size[this_size][1]], dtype=dtype)
else:
out_mat = np.zeros([out_size[this_size][0], out_size[this_size][1], img_c], dtype=dtype)
# print('gray image')
start_r = int(np.floor((out_size[this_size][0] - img_h) / 2.0))
start_c = int(np.floor((out_size[this_size][1] - img_w) / 2.0))
for ind_r in range(img_h):
for ind_c in range(img_w):
out_mat[start_r + ind_r, start_c + ind_c] = src_mat[ind_r, ind_c]
is_valid = True
return is_valid, out_mat, this_size
else:
return is_valid, 0, 0
def __matrix_padding_force_to_get_fix_sized_matrix(src_mat, padding_height, padding_width):
"""
force padding zero to original matrix
ori width <= padding width and ori height <= padding height: padding around
ori width <= padding width and ori height > padding height: padding on width direction, crop on height direction
ori width > padding width and ori height <= padding height: crop on W directory, padding on H directory
ori width > padding width and ori height > padding height: crop around
:param src_mat: source matrix
:param padding_height: out matrix's height
:param padding_width: out matrix's width
:return: padding_mat
"""
if len(src_mat.shape) == 2:
src_height, src_width = src_mat.shape
if src_width <= padding_width and src_height <= padding_height:
padding_mat = np.zeros([padding_height, padding_width])
start_h = int(np.floor((padding_height - src_height) / 2.0))
start_w = int(np.floor((padding_width - src_width) / 2.0))
padding_mat[start_h:start_h+src_height, start_w:start_w+src_width] = src_mat
elif src_width <= padding_width and src_height > padding_height:
padding_mat = np.zeros([padding_height, padding_width])
start_h = int(np.floor((src_height - padding_height) / 2.0))
start_w = int(np.floor((padding_width - src_width) / 2.0))
padding_mat[:, start_w:start_w+src_width] = src_mat[start_h:start_h+padding_height, :]
elif src_width > padding_width and src_height <= padding_height:
padding_mat = np.zeros([padding_height, padding_width])
start_h = int(np.floor((padding_height - src_height) / 2.0))
start_w = int(np.floor((src_width - padding_width) / 2.0))
padding_mat[start_h:start_h+src_height, :] = src_mat[:, start_w:start_w+padding_width]
else:
start_h = int(np.floor((src_height - padding_height) / 2.0))
start_w = int(np.floor((src_width - padding_width) / 2.0))
padding_mat = src_mat[start_h:start_h+padding_height, start_w:start_w+padding_width]
else:
src_height, src_width, channels = src_mat.shape
if src_width <= padding_width and src_height <= padding_height:
padding_mat = np.zeros([padding_height, padding_width, channels])
start_h = int(np.floor((padding_height - src_height) / 2.0))
start_w = int(np.floor((padding_width - src_width) / 2.0))
padding_mat[start_h:start_h+src_height, start_w:start_w+src_width, :] = src_mat
elif src_width <= padding_width and src_height > padding_height:
padding_mat = np.zeros([padding_height, padding_width, channels])
start_h = int(np.floor((src_height - padding_height) / 2.0))
start_w = int(np.floor((padding_width - src_width) / 2.0))
padding_mat[:, start_w:start_w+src_width] = src_mat[start_h:start_h+padding_height, :]
elif src_width > padding_width and src_height <= padding_height:
padding_mat = np.zeros([padding_height, padding_width, channels])
start_h = int(np.floor((padding_height - src_height) / 2.0))
start_w = int(np.floor((src_width - padding_width) / 2.0))
padding_mat[start_h:start_h+src_height, :] = src_mat[:, start_w:start_w+padding_width]
else:
start_h = int(np.floor((src_height - padding_height) / 2.0))
start_w = int(np.floor((src_width - padding_width) / 2.0))
padding_mat = src_mat[start_h:start_h + padding_height, start_w:start_w+padding_width]
return padding_mat
def __matrix_padding(src_mat, out_size=[64, 64], dtype=np.float32):
"""
padding zero to src matrix to get larger size matrix
if and only if source image's width and height are both not bigger than out's, do padding
:param src_mat: source matrix
:param out_size: list of int:[height, width]
:param dtype: the src matrix's date type, default to np.float32
:return is_valid: if src_mat's size is smaller than out's , is_valid set to be Ture, else Flase
:return out_mat: if is_valid is true, then out_mat be useful, else will be useless.
"""
img_c = None
if len(src_mat.shape) == 2:
img_h, img_w = src_mat.shape
else:
img_h, img_w, img_c = src_mat.shape
is_valid = False
if img_h <= out_size[0] and img_w <= out_size[1]: # image with valid size
if img_c is None:
out_mat = np.zeros([out_size[0], out_size[1]], dtype=dtype)
else:
out_mat = np.zeros([out_size[0], out_size[1], img_c], dtype=dtype)
# print('gray image')
start_r = int(np.floor((out_size[0] - img_h) / 2.0))
start_c = int(np.floor((out_size[1] - img_w) / 2.0))
for ind_r in range(img_h):
for ind_c in range(img_w):
out_mat[start_r + ind_r, start_c + ind_c] = src_mat[ind_r, ind_c]
is_valid = True
return is_valid, out_mat
else:
return is_valid, 0
def __dataset_padding(src_mat, ori_size, out_size=[[128, 128]], dtype=np.float32):
"""
padding zero to src matrix to get larger size matrix
:param src_mat: source matrix, every line represent a matrix which has been reshaped to a
vector with lenth ori_size[0]*ori_size[1]
:param ori_size: original matrix size [height, width]
:param out_size: every row represent one of the images' out size :[height, width],
and the row's index bigger, the size bigger.
:param dtype: the src matrix's date type, default to np.float32
:return out_mat: if is_valid is true, then out_mat be useful, else will be useless.
"""
if not len(np.array(src_mat).shape) == 2:
raise ValueError('input must be 2D matrix!')
out_mat = np.zeros((src_mat.shape[0], out_size[0][0]*out_size[0][1]), dtype=dtype)
for ind_mat in range(src_mat.shape[0]):
_, tmp_mat, _ = __matrix_padding_multi_size_soft(np.reshape(src_mat[ind_mat], (ori_size[0], ori_size[1])),
out_size=out_size, dtype=dtype)
out_mat[ind_mat] = np.reshape(tmp_mat, out_size[0][0]*out_size[0][1])
return out_mat
def padding_to_fix_sized_and_save_imgs(source_path, save_path, padding_height, padding_width,
source_extension='png'):
"""
padding zeros to images data to get fix sized images and save them. But if the source images's size is larger than
the required size, do croping on them
:param source_path: source images path: the struct should be source_path/images files
:param save_path: the processed images' save path
:param padding_height: output image's height
:param padding_width: output image's width
:param extension: source images' file extension
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
pbar = tqdm(os.scandir(source_path))
for img_files in pbar:
extension = os.path.splitext(img_files.path)[1][1:]
if extension == source_extension:
pbar.set_description("Processing %s" % img_files.name)
img_data = cv2.imread(img_files.path, -1)
filename_no_extension, _ = os.path.splitext(img_files.name)
padding_img = __matrix_padding_force_to_get_fix_sized_matrix(img_data, padding_height, padding_width)
cv2.imwrite(os.path.join(save_path, category.name, filename_no_extension+'.png'), padding_img)
return True
def padding_to_fix_sized_and_save_imgs_with_category(source_path, save_path, padding_height, padding_width,
source_extension='png'):
"""
padding zeros to images data to get fix sized images and save them. But if the source images's size is larger than
the required size, do croping on them
:param source_path: source images path: the struct should be source_path/category_folder/images files
:param save_path: the processed images' save path
:param padding_height: output image's height
:param padding_width: output image's width
:param extension: source images' file extension
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
for category in os.scandir(source_path):
if category.is_dir():
os.makedirs(os.path.join(save_path, category.name), exist_ok=True)
pbar = tqdm(os.scandir(category.path))
for img_files in pbar:
extension = os.path.splitext(img_files.path)[1][1:]
if extension == source_extension:
pbar.set_description("Processing %s" % img_files.name)
img_data = cv2.imread(img_files.path, -1)
filename_no_extension, _ = os.path.splitext(img_files.name)
padding_img = __matrix_padding_force_to_get_fix_sized_matrix(img_data, padding_height, padding_width)
cv2.imwrite(os.path.join(save_path, category.name, filename_no_extension+'.png'), padding_img)
return True
# slide sample
# def __sample_img_with_slide_windows(img, sample_width, sample_height, save_path=None, img_name=None):
# """
# sample a image with fixed size slide windows to get several fixed
# sized small images
# :param img: the original img data matrix
# :param sample_width: the sample image's width
# :param sample_height: the sample image's height
# :param save_path: sample images' save_path
# :param img_name: image files' save name, without extension !
# :return if successful, return the sample images data saved in a 2D
# matrix, which each line represents a sample image with size
# (1, sample_width*sample_height, channels); else return False
# """
# isfirst = True
# if len(img.shape) == 3:
# ori_height, ori_width, chanels = img.shape
# if ori_height >= sample_height and ori_width >= sample_width:
# sample_per_row = ori_width - sample_width + 1
# sample_per_column = ori_height - sample_height + 1
# for ind_row in range(sample_per_column):
# for ind_col in range(sample_per_row):
# if save_path is not None:
# os.makedirs(save_path, exist_ok=True)
# cv2.imwrite(os.path.join(save_path, img_name+'_'+str(int(
# ind_row*sample_per_row+ind_col+1))+'.png'),
# img[ind_row:ind_row + sample_height, ind_col:ind_col + sample_width, :])
# if isfirst:
# expand_img_data = np.reshape(
# img[ind_row:ind_row+sample_height,
# ind_col:ind_col+sample_width,
# :],
# (1, sample_width*sample_height, chanels),
# order='C')
# isfirst = False
# else:
# expand_img_data = np.append(
# expand_img_data,
# np.reshape(img[ind_row:ind_row+sample_height,
# ind_col:ind_col+sample_width,
# :],
# (1, sample_width*sample_height, chanels),
# order='C'),
# axis=0)
# return expand_img_data
# else:
# ori_height, ori_width = img.shape
# if ori_height >= sample_height and ori_width >= sample_width:
# sample_per_row = ori_width - sample_width + 1
# sample_per_column = ori_height - sample_height + 1
# for ind_row in range(sample_per_column):
# for ind_col in range(sample_per_row):
# if save_path is not None:
# os.makedirs(save_path, exist_ok=True)
# cv2.imwrite(os.path.join(save_path, img_name+'_'+str(int(ind_row*sample_per_row+ind_col+1))
# + '.png'),
# img[ind_row:ind_row + sample_height, ind_col:ind_col + sample_width])
# np.save(os.path.join(save_path, img_name+'_'+str(int(ind_row*sample_per_row+ind_col+1))
# + '.npy'),
# img[ind_row:ind_row + sample_height, ind_col:ind_col + sample_width])
# if isfirst:
# expand_img_data = np.reshape(
# img[ind_row:ind_row + sample_height,
# ind_col:ind_col + sample_width],
# (1, sample_width * sample_height),
# order='C')
# isfirst = False
# else:
# expand_img_data = np.append(
# expand_img_data,
# np.reshape(img[ind_row:ind_row + sample_height,
# ind_col:ind_col + sample_width],
# (1, sample_width * sample_height),
# order='C'),
# axis=0)
# return expand_img_data
def __sample_img_with_slide_windows(img, sample_width, sample_height, save_path=None, img_name=None, stride=[1, 1],
is_save_npy=False):
"""
sample a image with fixed size slide windows to get several fixed
sized small images
:param img: the original img data matrix
:param sample_width: the sample image's width
:param sample_height: the sample image's height
:param save_path: sample images' save_path, default to None, which means not to save sample image files
:param img_name: image files' save name, without extension !
:param stride: stride, a 2 length list, [h_stride, w_stride]
:param is_save_npy: if to save npy files, default to False
:return if successful, return the sample images data saved in a 2D
matrix, which each line represents a sample image with size
(1, sample_width*sample_height, channels); else return False
"""
isfirst = True
h_stride = np.int(stride[0])
w_stride = np.int(stride[1])
if len(img.shape) == 3:
ori_height, ori_width, chanels = img.shape
if ori_height >= sample_height and ori_width >= sample_width:
sample_per_row = np.int(np.ceil((ori_width - sample_width + 1) / w_stride))
sample_per_column = np.int(np.ceil((ori_height - sample_height + 1) / h_stride))
for _ind_row in range(sample_per_column):
ind_row = _ind_row * h_stride
print('processed %.4f %%' % ((_ind_row*1.0+1.0)/sample_per_column*100))
for _ind_col in range(sample_per_row):
ind_col = _ind_col * w_stride
if save_path is not None:
os.makedirs(save_path, exist_ok=True)
cv2.imwrite(os.path.join(save_path, img_name+'_'+str(int(
ind_row*sample_per_row+ind_col+1))+'.png'),
img[ind_row:ind_row + sample_height, ind_col:ind_col + sample_width, :])
if is_save_npy:
np.save(os.path.join(save_path, img_name+'_'+str(int(ind_row*sample_per_row+ind_col+1))
+ '.npy'),
img[ind_row:ind_row + sample_height, ind_col:ind_col + sample_width])
if isfirst:
expand_img_data = np.reshape(
img[ind_row:ind_row+sample_height,
ind_col:ind_col+sample_width,
:],
(1, sample_width*sample_height, chanels),
order='C')
isfirst = False
else:
expand_img_data = np.append(
expand_img_data,
np.reshape(img[ind_row:ind_row+sample_height,
ind_col:ind_col+sample_width,
:],
(1, sample_width*sample_height, chanels),
order='C'),
axis=0)
return expand_img_data
else:
ori_height, ori_width = img.shape
if ori_height >= sample_height and ori_width >= sample_width:
sample_per_row = np.int(np.ceil((ori_width - sample_width + 1) / w_stride))
sample_per_column = np.int(np.ceil((ori_height - sample_height + 1) / h_stride))
for ind_row in range(sample_per_column):
ind_row = ind_row * h_stride
for ind_col in range(sample_per_row):
ind_col = ind_col * w_stride
if save_path is not None:
os.makedirs(save_path, exist_ok=True)
cv2.imwrite(os.path.join(save_path, img_name+'_'+str(int(ind_row*sample_per_row+ind_col+1))
+ '.png'),
img[ind_row:ind_row + sample_height, ind_col:ind_col + sample_width])
if is_save_npy:
np.save(os.path.join(save_path, img_name+'_'+str(int(ind_row*sample_per_row+ind_col+1))
+ '.npy'),
img[ind_row:ind_row + sample_height, ind_col:ind_col + sample_width])
if isfirst:
expand_img_data = np.reshape(
img[ind_row:ind_row + sample_height,
ind_col:ind_col + sample_width],
(1, sample_width * sample_height),
order='C')
isfirst = False
else:
expand_img_data = np.append(
expand_img_data,
np.reshape(img[ind_row:ind_row + sample_height,
ind_col:ind_col + sample_width],
(1, sample_width * sample_height),
order='C'),
axis=0)
return expand_img_data
def sample_img_with_slide_window_and_save_npy_with_category(source_path, slide_height, slide_width, source_extension='png',
stride=[1, 1]):
"""
sample with slide window on images to get fix sized images, and save as .npy files
out images will be saved in source_path/h**w**/category/images,
npy file will be saved in source_path/category/npyfile
:param source_path: source images' path, source path/category/files
:param slide_height: slide window's height
:param slide_width: slide window's width
:param source_extension: source images' file extension
:param stride: stride, a 2 length list, [h_stride, w_stride]
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
for category in os.scandir(source_path):
if category.is_dir():
is_save_npy = False
pbar = tqdm(os.scandir(category.path))
is_first = True
total_dataset = []
for img_files in pbar:
if img_files.is_file():
extension = os.path.splitext(img_files.path)[1][1:]
filename_no_extension, _ = os.path.splitext(img_files.name)
if extension == source_extension:
is_save_npy = True
pbar.set_description("Processing %s" % img_files.name)
if extension == 'npy':
img_data = np.load(img_files.path)
else:
img_data = cv2.imread(img_files.path, -1)
if is_first:
total_dataset = __sample_img_with_slide_windows(
img_data, slide_width, slide_height,
save_path=os.path.join(source_path, 'h'+str(slide_height)+'w'+str(slide_width),
category.name),
img_name=filename_no_extension, stride=stride)
is_first = False
else:
total_dataset = np.append(total_dataset, __sample_img_with_slide_windows(
img_data, slide_width, slide_height,
save_path=os.path.join(source_path, 'h'+str(slide_height)+'w'+str(slide_width),
category.name),
img_name=filename_no_extension, stride=stride), axis=0)
if is_save_npy:
np.save(os.path.join(source_path, category.name+'.npy'), total_dataset)
return True
# def sample_with_slide_window_and_save_npy(source_path, slide_height, slide_width, npy_save_name, source_extension='png'):
# """
# sample with slide window on images to get fix sized images, and save as .npy files
# :param source_path: source images' path, source path/files
# :param slide_height: slide window's height
# :param slide_width: slide window's width
# :param npy_save_name: the npy file's save name
# :param source_extension: source images' file extension
# :return: True
# """
# if not os.path.exists(source_path):
# raise FileExistsError('path not found! : %s' % source_path)
# pbar = tqdm(os.scandir(source_path))
# is_first = True
# for img_files in pbar:
# if img_files.is_file():
# extension = os.path.splitext(img_files.path)[1][1:]
# filename_no_extension, _ = os.path.splitext(img_files.name)
# if extension == source_extension:
# pbar.set_description("Processing %s" % img_files.name)
# if extension == 'npy':
# img_data = np.load(img_files.path)
# else:
# img_data = cv2.imread(img_files.path, -1)
# if is_first:
# total_dataset = __sample_img_with_slide_windows(
# img_data, slide_width, slide_height,
# save_path=os.path.join(source_path, 'h'+str(slide_height)+'w'+str(slide_width)),
# img_name=filename_no_extension)
# is_first = False
# else:
# total_dataset = np.append(total_dataset, __sample_img_with_slide_windows(
# img_data, slide_width, slide_height,
# save_path=os.path.join(source_path, 'h'+str(slide_height)+'w'+str(slide_width)),
# img_name=filename_no_extension), axis=0)
# if npy_save_name is not None:
# np.save(os.path.join(source_path, npy_save_name), total_dataset)
# return True
def sample_with_slide_window_and_save_npy_stride(source_path, slide_height, slide_width, npy_save_name,
source_extension='png', stride=[1, 1]):
"""
sample with slide window on images to get fix sized images, and save as .npy files
:param source_path: source images' path, source path/files
:param slide_height: slide window's height
:param slide_width: slide window's width
:param npy_save_name: the npy file's save name
:param source_extension: source images' file extension
:param stride: stride, a 2 length list, [h_stride, w_stride]
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
pbar = tqdm(os.scandir(source_path))
is_first = True
for img_files in pbar:
if img_files.is_file():
extension = os.path.splitext(img_files.path)[1][1:]
filename_no_extension, _ = os.path.splitext(img_files.name)
if extension == source_extension:
pbar.set_description("Processing %s" % img_files.name)
if extension == 'npy':
img_data = np.load(img_files.path)
else:
img_data = cv2.imread(img_files.path, -1)
if is_first:
total_dataset = __sample_img_with_slide_windows(
img_data, slide_width, slide_height,
save_path=os.path.join(source_path, 'h'+str(slide_height)+'w'+str(slide_width)),
img_name=filename_no_extension, stride=stride)
is_first = False
else:
total_dataset = np.append(total_dataset, __sample_img_with_slide_windows(
img_data, slide_width, slide_height,
save_path=os.path.join(source_path, 'h'+str(slide_height)+'w'+str(slide_width)),
img_name=filename_no_extension, stride=stride), axis=0)
if npy_save_name is not None:
np.save(os.path.join(source_path, npy_save_name), total_dataset)
return True
def sample_with_slide_window_and_save_npy_given_image(
img_path, save_path, slide_height, slide_width, stride=[1, 1]):
"""
crop images of given size from original image and save them to save_path
:param img_path: str, the original image file path
:param save_path: str, the directory to save croped images
:param slide_height: int, the height of cropped image
:param slide_width: int, the width of cropped image
:param stride: int, the step size between two cropped images in horizontal or vertical direction
:return: True
"""
ori_img = cv2.imread(img_path, -1)
os.makedirs(save_path, exist_ok=True)
crop_img = __sample_img_with_slide_windows(ori_img, slide_height, slide_width, stride=stride)
for i in range(1, crop_img.shape[0]+1):
cv2.imwrite(
os.path.join(save_path, img_path.split(os.sep)[-1].split('.')[0]+f'_{i}.png'),
np.reshape(crop_img[i-1], [slide_height, slide_width]))
# resize
def resize_img_mat(img_mat, ori_height, ori_widhth, new_height, new_width):
"""
resize each image in img_mat to size [new_height, new_width].
:param img_mat: ndarray, each row store one image vector of length ori_height*ori_width
:param ori_height: int, the height of each image in img_mat
:param ori_widhth: int, the width of each image in img_mat
:param new_height: int, the new height of each image in img_mat
:param new_width: int, the new width of each image in img_mat
:return: resize_mat
"""
assert (ori_height * ori_widhth == img_mat.shape[1])
for ind in range(img_mat.shape[0]):
new_img = np.reshape(
cv2.resize(np.reshape(img_mat[ind], [ori_height, ori_widhth]), (new_width, new_height)),
[1, -1])
if not ind:
resize_mat = new_img
else:
resize_mat = np.concatenate([resize_mat, new_img], axis=0)
return resize_mat
def resize_img_and_save_to_folder_opensarship_slc_with_category(
source_path, save_path, source_extension='tif', new_size=[88, 88], is_save_npy=False, is_save_img=True):
"""
resize SLC mode OpenSARShip images to fixed size and save them
the source path's struct : root/category_folders/image files
:param source_path: source images' root ptah
:param save_path: the path to save resized images
:param source_extension: source image files' extension, default to 'tif'
:param is_save_npy : if to save npy files whose name is same to original image file, npy files contain float values
:param is_save_img: if to save image files, default to True
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
for category in os.scandir(source_path):
if category.is_dir():
pbar = tqdm(os.scandir(category.path))
for img_files in pbar:
extension = os.path.splitext(img_files.path)[1][1:]
if extension == source_extension:
pbar.set_description("Processing %s" % img_files.name)
tif = TIFF.open(img_files.path, mode='r')
source_img = tif.read_image()
# 1st channel is the real part for VH
# 2st channel is the imaginary part for VH
# to get the amplitude value of VH
img_amplitude = np.sqrt(
np.square(source_img[:, :, 0]) +
np.square(source_img[:, :, 1]))
# image = misc.toimage(img_amplitude)
# im_resize = misc.imresize(image, (new_size[0], new_size[
# 1]))
im_resize = cv2.resize(img_amplitude, (new_size[1], new_size[0]))
filename_no_extension, extension = os.path.splitext(
img_files.name)
os.makedirs(os.path.join(save_path, category.name),
exist_ok=True)
os.chdir(os.path.join(save_path, category.name))
# misc.imsave(filename_no_extension+'.png', im_resize)
if is_save_img: