Switch to unified view

a b/DataAugmentation/imgaug.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "heading",
5
   "metadata": {
6
    "collapsed": false
7
   },
8
   "level": 1,
9
   "source": [
10
    "The Notebook for Defect Detection Image Augumentation"
11
   ]
12
  },
13
  {
14
   "cell_type": "heading",
15
   "metadata": {},
16
   "level": 2,
17
   "source": [
18
    "Path"
19
   ]
20
  },
21
  {
22
   "cell_type": "code",
23
   "execution_count": null,
24
   "metadata": {},
25
   "outputs": [],
26
   "source": [
27
    "dataPath = \".Data_postiveOnly\""
28
   ]
29
  },
30
  {
31
   "cell_type": "heading",
32
   "metadata": {},
33
   "level": 2,
34
   "source": [
35
    "Import"
36
   ]
37
  },
38
  {
39
   "cell_type": "code",
40
   "execution_count": null,
41
   "metadata": {},
42
   "outputs": [],
43
   "source": [
44
    "import imgaug as ia\n",
45
    "from imgaug import augmenters as iaa\n",
46
    "import skimage.io\n",
47
    "import errno\n",
48
    "import numpy as np\n",
49
    "import skimage.color as color\n",
50
    "import matplotlib.pyplot as plt\n",
51
    "import numpy as np\n",
52
    "import os\n",
53
    "from skimage.color import gray2rgb"
54
   ]
55
  },
56
  {
57
   "cell_type": "heading",
58
   "metadata": {},
59
   "level": 2,
60
   "source": [
61
    "Helper Functions"
62
   ]
63
  },
64
  {
65
   "cell_type": "code",
66
   "execution_count": null,
67
   "metadata": {},
68
   "outputs": [],
69
   "source": [
70
    "def bboxSetupInImage(datapath,txtFile,img):\n",
71
    "    \"\"\"\n",
72
    "    This is the function that reads in the bounding box files and then using imgaug to set up the bounding box on images\n",
73
    "    \n",
74
    "    :param txtFile: the txt file that store bounding box information \n",
75
    "    :param img: the image file variable to represent the img to be plotted bounding box on it \n",
76
    "    :return bbs: the image with bounding box in it\n",
77
    "    \"\"\"\n",
78
    "    with open( datapath + 'bounding_boxes/' + txtFile,'r') as f:\n",
79
    "        content = [line.rstrip('\\n') for line in f]\n",
80
    "        iaBBoxList = []\n",
81
    "        for bbline in content:\n",
82
    "            bbox = bbline.strip().split()\n",
83
    "            #print(bbox[1])\n",
84
    "            if len(bbox) == 5:\n",
85
    "                iaBBoxList.append(ia.BoundingBox(\n",
86
    "                    x1=float(bbox[2]), \n",
87
    "                    y1=float(bbox[1]), \n",
88
    "                    x2=float(bbox[4]), \n",
89
    "                    y2=float(bbox[3]),\n",
90
    "                    label = bbox[0]))\n",
91
    "        bbs = ia.BoundingBoxesOnImage(iaBBoxList, shape=img.shape)\n",
92
    "        return bbs"
93
   ]
94
  },
95
  {
96
   "cell_type": "code",
97
   "execution_count": 27,
98
   "metadata": {},
99
   "outputs": [],
100
   "source": [
101
    "def saveAugbbox2TXT(txtFile,bbs):\n",
102
    "    \"\"\"\n",
103
    "    This is the function that save the augmented bounding box files into ChainerCV bbox format\n",
104
    "    \n",
105
    "    :param txtFile: the txt file that want to save\n",
106
    "    :param bbs: bounding box lists \n",
107
    "    \"\"\"\n",
108
    "    with open('' + txtFile,'w') as f:\n",
109
    "        for i in range(len(bbs.bounding_boxes)):\n",
110
    "            bb = bbs_aug.bounding_boxes[i]\n",
111
    "            # print(\"%s %.2f %.2f %.2f %.2f\"%(bb.label,bb.y1,bb.x1,bb.y2,bb.x2))\n",
112
    "            f.write(\"%s %.2f %.2f %.2f %.2f\\n\"%(bb.label,bb.y1,bb.x1,bb.y2,bb.x2))"
113
   ]
114
  },
115
  {
116
   "cell_type": "code",
117
   "execution_count": 28,
118
   "metadata": {},
119
   "outputs": [],
120
   "source": [
121
    "def getImageList(imageTXT):\n",
122
    "    \"\"\"\n",
123
    "    Function to loop the testing images for test\n",
124
    "    :param imageTXT: the txt that stores the \n",
125
    "    :return: imageFileList: the list contains all the original test image list\n",
126
    "    \"\"\"\n",
127
    "    imageFileList = list()\n",
128
    "    with open(imageTXT,'r') as f:\n",
129
    "        lines = f.readlines()\n",
130
    "        for line in lines:\n",
131
    "            imageFileList.append(line.strip())\n",
132
    "    return imageFileList"
133
   ]
134
  },
135
  {
136
   "cell_type": "code",
137
   "execution_count": 29,
138
   "metadata": {},
139
   "outputs": [],
140
   "source": [
141
    "def createFolder(folderName):\n",
142
    "    \"\"\"\n",
143
    "    Safely create folder when needed\n",
144
    "    \n",
145
    "    :param folderName : the directory that you  want to safely create\n",
146
    "    :return: None\n",
147
    "    \"\"\"\n",
148
    "    if not os.path.exists(folderName):\n",
149
    "        try:\n",
150
    "            os.makedirs(folderName)\n",
151
    "        except OSError as exc:  # Guard against race condition\n",
152
    "            if exc.errno != errno.EEXIST:\n",
153
    "                raise"
154
   ]
155
  },
156
  {
157
   "cell_type": "heading",
158
   "metadata": {},
159
   "level": 2,
160
   "source": [
161
    "Gaussian Noise"
162
   ]
163
  },
164
  {
165
   "cell_type": "code",
166
   "execution_count": 30,
167
   "metadata": {
168
    "collapsed": true
169
   },
170
   "outputs": [
171
    {
172
     "name": "stdout",
173
     "output_type": "stream",
174
     "text": [
175
      "0501_300kx_1nm_clhaadf3_0006.jpg\n"
176
     ]
177
    },
178
    {
179
     "name": "stdout",
180
     "output_type": "stream",
181
     "text": [
182
      "0501_300kx_1nm_clhaadf3_0008.jpg\n"
183
     ]
184
    },
185
    {
186
     "name": "stdout",
187
     "output_type": "stream",
188
     "text": [
189
      "0501_300kx_1nm_clhaadf3_0012.jpg\n"
190
     ]
191
    },
192
    {
193
     "name": "stdout",
194
     "output_type": "stream",
195
     "text": [
196
      "0501_300kx_1nm_clhaadf3_0016.jpg\n0501_300kx_1nm_clhaadf3_0018.jpg\n"
197
     ]
198
    },
199
    {
200
     "name": "stdout",
201
     "output_type": "stream",
202
     "text": [
203
      "0501_300kx_1nm_clhaadf3_0020.jpg\n0501_300kx_1nm_clhaadf3_0022.jpg\n"
204
     ]
205
    },
206
    {
207
     "name": "stdout",
208
     "output_type": "stream",
209
     "text": [
210
      "0501_300kx_1nm_clhaadf3_0024.jpg\n0501_300kx_1nm_clhaadf3_0026.jpg\n"
211
     ]
212
    },
213
    {
214
     "name": "stdout",
215
     "output_type": "stream",
216
     "text": [
217
      "0501_300kx_1nm_clhaadf3_0030.jpg\n0501_300kx_1nm_clhaadf3_0031.jpg\n"
218
     ]
219
    },
220
    {
221
     "name": "stdout",
222
     "output_type": "stream",
223
     "text": [
224
      "0501_300kx_1nm_clhaadf3_0034.jpg\n0501_300kx_1nm_clhaadf3_0036.jpg\n"
225
     ]
226
    },
227
    {
228
     "name": "stdout",
229
     "output_type": "stream",
230
     "text": [
231
      "0501_300kx_1nm_clhaadf3_0040.jpg\n0501_300kx_1nm_clhaadf3_0044.jpg\n"
232
     ]
233
    },
234
    {
235
     "name": "stdout",
236
     "output_type": "stream",
237
     "text": [
238
      "0501_300kx_1nm_clhaadf3_0046.jpg\n0501_300kx_1nm_clhaadf3_0048.jpg\n"
239
     ]
240
    },
241
    {
242
     "name": "stdout",
243
     "output_type": "stream",
244
     "text": [
245
      "0501_300kx_1nm_clhaadf3_0050.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0002.jpg\n"
246
     ]
247
    },
248
    {
249
     "name": "stdout",
250
     "output_type": "stream",
251
     "text": [
252
      "200kV_500kx_p2nm_8cmCL_grain1_0002_n.jpg\n"
253
     ]
254
    },
255
    {
256
     "name": "stdout",
257
     "output_type": "stream",
258
     "text": [
259
      "200kV_500kx_p2nm_8cmCL_grain1_0004_n.jpg\n"
260
     ]
261
    },
262
    {
263
     "name": "stdout",
264
     "output_type": "stream",
265
     "text": [
266
      "200kV_500kx_p2nm_8cmCL_grain1_0048 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0050 - Copy.jpg\n"
267
     ]
268
    },
269
    {
270
     "name": "stdout",
271
     "output_type": "stream",
272
     "text": [
273
      "200kV_500kx_p2nm_8cmCL_grain1_0052 - Copy.jpg\n"
274
     ]
275
    },
276
    {
277
     "name": "stdout",
278
     "output_type": "stream",
279
     "text": [
280
      "200kV_500kx_p2nm_8cmCL_grain1_0054 - Copy.jpg\n"
281
     ]
282
    },
283
    {
284
     "name": "stdout",
285
     "output_type": "stream",
286
     "text": [
287
      "200kV_500kx_p2nm_8cmCL_grain1_0058 - Copy.jpg\n"
288
     ]
289
    },
290
    {
291
     "name": "stdout",
292
     "output_type": "stream",
293
     "text": [
294
      "200kV_500kx_p2nm_8cmCL_grain1_0060 - Copy.jpg\n"
295
     ]
296
    },
297
    {
298
     "name": "stdout",
299
     "output_type": "stream",
300
     "text": [
301
      "200kV_500kx_p2nm_8cmCL_grain1_0062 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0064 - Copy.jpg\n"
302
     ]
303
    },
304
    {
305
     "name": "stdout",
306
     "output_type": "stream",
307
     "text": [
308
      "200kV_500kx_p2nm_8cmCL_grain1_0066 - Copy.jpg\n"
309
     ]
310
    },
311
    {
312
     "name": "stdout",
313
     "output_type": "stream",
314
     "text": [
315
      "200kV_500kx_p2nm_8cmCL_grain1_0068 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0070 - Copy.jpg\n"
316
     ]
317
    },
318
    {
319
     "name": "stdout",
320
     "output_type": "stream",
321
     "text": [
322
      "200kV_500kx_p2nm_8cmCL_grain1_0072 - Copy.jpg\n"
323
     ]
324
    },
325
    {
326
     "name": "stdout",
327
     "output_type": "stream",
328
     "text": [
329
      "200kV_500kx_p2nm_8cmCL_grain1_0074 - Copy.jpg\n"
330
     ]
331
    },
332
    {
333
     "name": "stdout",
334
     "output_type": "stream",
335
     "text": [
336
      "200kV_500kx_p2nm_8cmCL_grain1_0076 - Copy.jpg\n"
337
     ]
338
    },
339
    {
340
     "name": "stdout",
341
     "output_type": "stream",
342
     "text": [
343
      "200kV_500kx_p2nm_8cmCL_grain1_0078 - Copy.jpg\n"
344
     ]
345
    },
346
    {
347
     "name": "stdout",
348
     "output_type": "stream",
349
     "text": [
350
      "200kV_500kx_p2nm_8cmCL_grain1_0080 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0084 - Copy.jpg\n"
351
     ]
352
    },
353
    {
354
     "name": "stdout",
355
     "output_type": "stream",
356
     "text": [
357
      "200kV_500kx_p2nm_8cmCL_grain1_0086 - Copy.jpg\n"
358
     ]
359
    },
360
    {
361
     "name": "stdout",
362
     "output_type": "stream",
363
     "text": [
364
      "200kV_500kx_p2nm_8cmCL_grain1_0090 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0092 - Copy.jpg\n"
365
     ]
366
    },
367
    {
368
     "name": "stdout",
369
     "output_type": "stream",
370
     "text": [
371
      "200kV_500kx_p2nm_8cmCL_grain1_0109.jpg\n"
372
     ]
373
    },
374
    {
375
     "name": "stdout",
376
     "output_type": "stream",
377
     "text": [
378
      "200kV_500kx_p2nm_8cmCL_grain1_0111.jpg\n"
379
     ]
380
    },
381
    {
382
     "name": "stdout",
383
     "output_type": "stream",
384
     "text": [
385
      "200kV_500kx_p2nm_8cmCL_grain1_0129.jpg\n"
386
     ]
387
    },
388
    {
389
     "name": "stdout",
390
     "output_type": "stream",
391
     "text": [
392
      "200kV_500kx_p2nm_8cmCL_grain2_0010.jpg\n200kV_500kx_p2nm_8cmCL_grain2_0012.jpg\n"
393
     ]
394
    },
395
    {
396
     "name": "stdout",
397
     "output_type": "stream",
398
     "text": [
399
      "200kV_500kx_p2nm_8cmCL_grain3_4_0258.jpg\n"
400
     ]
401
    },
402
    {
403
     "name": "stdout",
404
     "output_type": "stream",
405
     "text": [
406
      "2501_300kx_1nm_clhaadf3_0032.jpg\n2501_300kx_1nm_clhaadf3_0034.jpg\n"
407
     ]
408
    },
409
    {
410
     "name": "stdout",
411
     "output_type": "stream",
412
     "text": [
413
      "2501_300kx_1nm_clhaadf3_0036.jpg\n"
414
     ]
415
    },
416
    {
417
     "name": "stdout",
418
     "output_type": "stream",
419
     "text": [
420
      "2501_300kx_1nm_clhaadf3_0038.jpg\n2501_300kx_1nm_clhaadf3_0040.jpg\n"
421
     ]
422
    },
423
    {
424
     "name": "stdout",
425
     "output_type": "stream",
426
     "text": [
427
      "2501_300kx_1nm_clhaadf3_0042.jpg\n2501_300kx_1nm_clhaadf3_0044.jpg\n"
428
     ]
429
    },
430
    {
431
     "name": "stdout",
432
     "output_type": "stream",
433
     "text": [
434
      "2501_300kx_1nm_clhaadf3_0046.jpg\n2501_300kx_1nm_clhaadf3_0048.jpg\n"
435
     ]
436
    },
437
    {
438
     "name": "stdout",
439
     "output_type": "stream",
440
     "text": [
441
      "2501_300kx_1nm_clhaadf3_0050.jpg\n"
442
     ]
443
    },
444
    {
445
     "name": "stdout",
446
     "output_type": "stream",
447
     "text": [
448
      "2501_300kx_1nm_clhaadf3_0052.jpg\n2501_300kx_1nm_clhaadf3_0054.jpg\n"
449
     ]
450
    },
451
    {
452
     "name": "stdout",
453
     "output_type": "stream",
454
     "text": [
455
      "2ROI_100kx_4100CL_foil1.jpg\n"
456
     ]
457
    },
458
    {
459
     "name": "stdout",
460
     "output_type": "stream",
461
     "text": [
462
      "3ROI_100kx_4100CL_foil1.jpg\n4ROI_100kx_4100CL_foil1.jpg\n"
463
     ]
464
    },
465
    {
466
     "name": "stdout",
467
     "output_type": "stream",
468
     "text": [
469
      "5401_300kx_1nm_clhaadf3_0004.jpg\n5401_300kx_1nm_clhaadf3_0006.jpg\n"
470
     ]
471
    },
472
    {
473
     "name": "stdout",
474
     "output_type": "stream",
475
     "text": [
476
      "5401_300kx_1nm_clhaadf3_0016.jpg\n"
477
     ]
478
    },
479
    {
480
     "name": "stdout",
481
     "output_type": "stream",
482
     "text": [
483
      "5401_300kx_1nm_clhaadf3_0018.jpg\n5401_300kx_1nm_clhaadf3_0022.jpg\n"
484
     ]
485
    },
486
    {
487
     "name": "stdout",
488
     "output_type": "stream",
489
     "text": [
490
      "5401_300kx_1nm_clhaadf3_0024.jpg\n5401_300kx_1nm_clhaadf3_0026.jpg\n"
491
     ]
492
    },
493
    {
494
     "name": "stdout",
495
     "output_type": "stream",
496
     "text": [
497
      "5401_300kx_1nm_clhaadf3_0030.jpg\n"
498
     ]
499
    },
500
    {
501
     "name": "stdout",
502
     "output_type": "stream",
503
     "text": [
504
      "5ROI_100kx_4100CL_foil1 copy.jpg\n"
505
     ]
506
    },
507
    {
508
     "name": "stdout",
509
     "output_type": "stream",
510
     "text": [
511
      "6ROI_100kx_4100CL_foil1 copy.jpg\n"
512
     ]
513
    },
514
    {
515
     "name": "stdout",
516
     "output_type": "stream",
517
     "text": [
518
      "7ROI_100kx_4100CL_foil1 copy.jpg\n"
519
     ]
520
    },
521
    {
522
     "name": "stdout",
523
     "output_type": "stream",
524
     "text": [
525
      "9ROI_100kx_4100CL_foil1.jpg\n"
526
     ]
527
    },
528
    {
529
     "name": "stdout",
530
     "output_type": "stream",
531
     "text": [
532
      "BF X500K, 01 (2).jpg\n"
533
     ]
534
    },
535
    {
536
     "name": "stdout",
537
     "output_type": "stream",
538
     "text": [
539
      "BF X500K, 01 (3).jpg\n"
540
     ]
541
    },
542
    {
543
     "name": "stdout",
544
     "output_type": "stream",
545
     "text": [
546
      "BF X500K, 02 (2).jpg\n"
547
     ]
548
    },
549
    {
550
     "name": "stdout",
551
     "output_type": "stream",
552
     "text": [
553
      "BF X500K, 03 (2).jpg\n"
554
     ]
555
    },
556
    {
557
     "name": "stdout",
558
     "output_type": "stream",
559
     "text": [
560
      "BF X500K, 04.jpg\n"
561
     ]
562
    },
563
    {
564
     "name": "stdout",
565
     "output_type": "stream",
566
     "text": [
567
      "BF X500K, 05 (2).jpg\n"
568
     ]
569
    },
570
    {
571
     "name": "stdout",
572
     "output_type": "stream",
573
     "text": [
574
      "BF X500K, 05.jpg\n"
575
     ]
576
    },
577
    {
578
     "name": "stdout",
579
     "output_type": "stream",
580
     "text": [
581
      "BF X500K, 06 (3).jpg\n"
582
     ]
583
    },
584
    {
585
     "name": "stdout",
586
     "output_type": "stream",
587
     "text": [
588
      "BF X500K, 06.jpg\n"
589
     ]
590
    },
591
    {
592
     "name": "stdout",
593
     "output_type": "stream",
594
     "text": [
595
      "BF X500K, 07 (2).jpg\nBF X500K, 08 (2).jpg\n"
596
     ]
597
    },
598
    {
599
     "name": "stdout",
600
     "output_type": "stream",
601
     "text": [
602
      "BF X500K, 08 (3).jpg\nBF X500K, 08.jpg\n"
603
     ]
604
    },
605
    {
606
     "name": "stdout",
607
     "output_type": "stream",
608
     "text": [
609
      "BF X500K, 10 (2).jpg\n"
610
     ]
611
    },
612
    {
613
     "name": "stdout",
614
     "output_type": "stream",
615
     "text": [
616
      "BF X500K, 10 (3).jpg\n"
617
     ]
618
    },
619
    {
620
     "name": "stdout",
621
     "output_type": "stream",
622
     "text": [
623
      "BF X500K, 10.jpg\n"
624
     ]
625
    },
626
    {
627
     "name": "stdout",
628
     "output_type": "stream",
629
     "text": [
630
      "BF X500K, 11 (2).jpg\n"
631
     ]
632
    },
633
    {
634
     "name": "stdout",
635
     "output_type": "stream",
636
     "text": [
637
      "BF X500K, 11 (3).jpg\nBF X500K, 11.jpg\n"
638
     ]
639
    },
640
    {
641
     "name": "stdout",
642
     "output_type": "stream",
643
     "text": [
644
      "BF X500K, 12 (2).jpg\n"
645
     ]
646
    },
647
    {
648
     "name": "stdout",
649
     "output_type": "stream",
650
     "text": [
651
      "BF X500K, 12 (3).jpg\nBF X500K, 12.jpg\n"
652
     ]
653
    },
654
    {
655
     "name": "stdout",
656
     "output_type": "stream",
657
     "text": [
658
      "BF X500K, 13 (3).jpg\n"
659
     ]
660
    },
661
    {
662
     "name": "stdout",
663
     "output_type": "stream",
664
     "text": [
665
      "BF X500K, 14.jpg\n"
666
     ]
667
    },
668
    {
669
     "name": "stdout",
670
     "output_type": "stream",
671
     "text": [
672
      "BF X500K, 15.jpg\nBF X500K, 16.jpg\n"
673
     ]
674
    },
675
    {
676
     "name": "stdout",
677
     "output_type": "stream",
678
     "text": [
679
      "K713_300kx_store4_grid1_0005.jpg\nK713_300kx_store4_grid1_0007.jpg\n"
680
     ]
681
    },
682
    {
683
     "name": "stdout",
684
     "output_type": "stream",
685
     "text": [
686
      "K713_300kx_store4_grid1_0009.jpg\n"
687
     ]
688
    },
689
    {
690
     "name": "stdout",
691
     "output_type": "stream",
692
     "text": [
693
      "K713_300kx_store4_grid1_0011.jpg\n"
694
     ]
695
    },
696
    {
697
     "name": "stdout",
698
     "output_type": "stream",
699
     "text": [
700
      "K713_300kx_store4_grid1_0013.jpg\nK713_300kx_store4_grid1_0015.jpg\n"
701
     ]
702
    },
703
    {
704
     "name": "stdout",
705
     "output_type": "stream",
706
     "text": [
707
      "K713_300kx_store4_grid1_0017.jpg\n"
708
     ]
709
    },
710
    {
711
     "name": "stdout",
712
     "output_type": "stream",
713
     "text": [
714
      "K713_300kx_store4_grid1_0019.jpg\n"
715
     ]
716
    },
717
    {
718
     "name": "stdout",
719
     "output_type": "stream",
720
     "text": [
721
      "dalong2.jpg\n"
722
     ]
723
    },
724
    {
725
     "name": "stdout",
726
     "output_type": "stream",
727
     "text": [
728
      "dalong3.jpg\n"
729
     ]
730
    },
731
    {
732
     "name": "stdout",
733
     "output_type": "stream",
734
     "text": [
735
      "g1_backonzone_GBtowardsfrom_0005.jpg\n"
736
     ]
737
    },
738
    {
739
     "name": "stdout",
740
     "output_type": "stream",
741
     "text": [
742
      "g1_frontonzone_GBtowardsback_0006.jpg\n"
743
     ]
744
    },
745
    {
746
     "name": "stdout",
747
     "output_type": "stream",
748
     "text": [
749
      "g2_midonzone_GBtowardsfront_0005.jpg\n"
750
     ]
751
    },
752
    {
753
     "name": "stdout",
754
     "output_type": "stream",
755
     "text": [
756
      "g2_midonzone_GBtowardsfront_0034.jpg\n"
757
     ]
758
    },
759
    {
760
     "name": "stdout",
761
     "output_type": "stream",
762
     "text": [
763
      "g2_midonzone_GBtowardsmmiddle_0003.jpg\n"
764
     ]
765
    },
766
    {
767
     "name": "stdout",
768
     "output_type": "stream",
769
     "text": [
770
      "g2_midonzone_GBtowardsmmiddle_0005.jpg\n"
771
     ]
772
    },
773
    {
774
     "name": "stdout",
775
     "output_type": "stream",
776
     "text": [
777
      "grid1_roi1_500kx_0p5nm_haadf1_0001.jpg\n"
778
     ]
779
    },
780
    {
781
     "name": "stdout",
782
     "output_type": "stream",
783
     "text": [
784
      "grid1_roi1_500kx_0p5nm_haadf1_0003.jpg\n"
785
     ]
786
    },
787
    {
788
     "name": "stdout",
789
     "output_type": "stream",
790
     "text": [
791
      "grid1_roi1_500kx_0p5nm_haadf1_0005.jpg\n"
792
     ]
793
    },
794
    {
795
     "name": "stdout",
796
     "output_type": "stream",
797
     "text": [
798
      "grid1_roi1_500kx_0p5nm_haadf1_0007.jpg\n"
799
     ]
800
    },
801
    {
802
     "name": "stdout",
803
     "output_type": "stream",
804
     "text": [
805
      "grid1_roi1_500kx_0p5nm_haadf1_0009.jpg\n"
806
     ]
807
    },
808
    {
809
     "name": "stdout",
810
     "output_type": "stream",
811
     "text": [
812
      "grid1_roi1_500kx_0p5nm_haadf1_0011.jpg\n"
813
     ]
814
    },
815
    {
816
     "name": "stdout",
817
     "output_type": "stream",
818
     "text": [
819
      "grid1_roi1_500kx_0p5nm_haadf1_0013.jpg\n"
820
     ]
821
    },
822
    {
823
     "name": "stdout",
824
     "output_type": "stream",
825
     "text": [
826
      "grid1_roi1_500kx_0p5nm_haadf1_0015.jpg\n"
827
     ]
828
    },
829
    {
830
     "name": "stdout",
831
     "output_type": "stream",
832
     "text": [
833
      "grid1_roi1_500kx_0p5nm_haadf1_0017.jpg\n"
834
     ]
835
    },
836
    {
837
     "name": "stdout",
838
     "output_type": "stream",
839
     "text": [
840
      "grid1_roi1_500kx_0p5nm_haadf1_0019.jpg\n"
841
     ]
842
    },
843
    {
844
     "name": "stdout",
845
     "output_type": "stream",
846
     "text": [
847
      "grid1_roi1_500kx_0p5nm_haadf1_0021.jpg\n"
848
     ]
849
    },
850
    {
851
     "name": "stdout",
852
     "output_type": "stream",
853
     "text": [
854
      "grid1_roi1_500kx_0p5nm_haadf1_0023.jpg\n"
855
     ]
856
    },
857
    {
858
     "name": "stdout",
859
     "output_type": "stream",
860
     "text": [
861
      "grid1_roi1_500kx_0p5nm_haadf1_0027.jpg\n"
862
     ]
863
    },
864
    {
865
     "name": "stdout",
866
     "output_type": "stream",
867
     "text": [
868
      "grid1_roi1_500kx_0p5nm_haadf1_0029.jpg\n"
869
     ]
870
    },
871
    {
872
     "name": "stdout",
873
     "output_type": "stream",
874
     "text": [
875
      "grid1_roi1_500kx_0p5nm_haadf1_0031.jpg\n"
876
     ]
877
    },
878
    {
879
     "name": "stdout",
880
     "output_type": "stream",
881
     "text": [
882
      "grid1_roi2_500kx_0p5nm_haadf1_0035.jpg\n"
883
     ]
884
    },
885
    {
886
     "name": "stdout",
887
     "output_type": "stream",
888
     "text": [
889
      "grid1_roi2_500kx_0p5nm_haadf1_0037.jpg\n"
890
     ]
891
    },
892
    {
893
     "name": "stdout",
894
     "output_type": "stream",
895
     "text": [
896
      "grid1_roi2_500kx_0p5nm_haadf1_0039.jpg\n"
897
     ]
898
    },
899
    {
900
     "name": "stdout",
901
     "output_type": "stream",
902
     "text": [
903
      "grid1_roi2_500kx_0p5nm_haadf1_0041.jpg\n"
904
     ]
905
    },
906
    {
907
     "name": "stdout",
908
     "output_type": "stream",
909
     "text": [
910
      "grid1_roi2_500kx_0p5nm_haadf1_0044.jpg\n"
911
     ]
912
    },
913
    {
914
     "name": "stdout",
915
     "output_type": "stream",
916
     "text": [
917
      "grid1_roi2_500kx_0p5nm_haadf1_0045.jpg\n"
918
     ]
919
    },
920
    {
921
     "name": "stdout",
922
     "output_type": "stream",
923
     "text": [
924
      "grid1_roi2_500kx_0p5nm_haadf1_0049.jpg\n"
925
     ]
926
    },
927
    {
928
     "name": "stdout",
929
     "output_type": "stream",
930
     "text": [
931
      "grid1_roi2_500kx_0p5nm_haadf1_0051.jpg\n"
932
     ]
933
    },
934
    {
935
     "name": "stdout",
936
     "output_type": "stream",
937
     "text": [
938
      "grid1_roi2_500kx_0p5nm_haadf1_0053.jpg\n"
939
     ]
940
    },
941
    {
942
     "name": "stdout",
943
     "output_type": "stream",
944
     "text": [
945
      "grid1_roi2_500kx_0p5nm_haadf1_0055.jpg\n"
946
     ]
947
    },
948
    {
949
     "name": "stdout",
950
     "output_type": "stream",
951
     "text": [
952
      "grid1_roi2_500kx_0p5nm_haadf1_0057.jpg\n"
953
     ]
954
    },
955
    {
956
     "name": "stdout",
957
     "output_type": "stream",
958
     "text": [
959
      "grid1_roi2_500kx_0p5nm_haadf1_0059.jpg\n"
960
     ]
961
    },
962
    {
963
     "name": "stdout",
964
     "output_type": "stream",
965
     "text": [
966
      "grid1_roi2_500kx_0p5nm_haadf1_0061.jpg\n"
967
     ]
968
    },
969
    {
970
     "name": "stdout",
971
     "output_type": "stream",
972
     "text": [
973
      "grid1_roi2_500kx_0p5nm_haadf1_0063.jpg\n"
974
     ]
975
    },
976
    {
977
     "name": "stdout",
978
     "output_type": "stream",
979
     "text": [
980
      "grid1_roi2_500kx_0p5nm_haadf1_0065.jpg\n"
981
     ]
982
    },
983
    {
984
     "name": "stdout",
985
     "output_type": "stream",
986
     "text": [
987
      "grid1_roi2_500kx_0p5nm_haadf1_0067.jpg\n"
988
     ]
989
    },
990
    {
991
     "name": "stdout",
992
     "output_type": "stream",
993
     "text": [
994
      "grid1_roi2_500kx_0p5nm_haadf1_0069.jpg\n"
995
     ]
996
    },
997
    {
998
     "name": "stdout",
999
     "output_type": "stream",
1000
     "text": [
1001
      "BF X500K, 05 (3).jpg\n"
1002
     ]
1003
    },
1004
    {
1005
     "name": "stdout",
1006
     "output_type": "stream",
1007
     "text": [
1008
      "0501_300kx_1nm_clhaadf3_0038.jpg\n"
1009
     ]
1010
    }
1011
   ],
1012
   "source": [
1013
    "\n",
1014
    "##################################################\n",
1015
    "# 1. Define data augmentation operations\n",
1016
    "##################################################\n",
1017
    "\n",
1018
    "trainImageTxtFile = dataPath + \"trainimages.txt\"\n",
1019
    "imageList = getImageList(trainImageTxtFile)\n",
1020
    "\n",
1021
    "current_operation = \"GaussianNoise\"\n",
1022
    "\n",
1023
    "# Add gaussian noise.\n",
1024
    "# For 50% of all images, we sample the noise once per pixel.\n",
1025
    "# For the other 50% of all images, we sample the noise per pixel AND\n",
1026
    "# channel. This can change the color (not only brightness) of the\n",
1027
    "# pixels.\n",
1028
    "   \n",
1029
    "from imgaug import augmenters as iaa\n",
1030
    "ia.seed(1)\n",
1031
    "seq = iaa.Sequential([\n",
1032
    "        iaa.AdditiveGaussianNoise(loc=0, \n",
1033
    "                                  scale=(0.0, 0.05*255), \n",
1034
    "                                  per_channel=0.5)\n",
1035
    "])\n",
1036
    "\n",
1037
    "# seq = iaa.Sequential([ \n",
1038
    "#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.\n",
1039
    "#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.\n",
1040
    "#     iaa.GammaContrast((0.5, 1.5))\n",
1041
    "# ])\n",
1042
    "\n",
1043
    "# Make our sequence deterministic.\n",
1044
    "# We can now apply it to the image and then to the BBs and it will\n",
1045
    "# lead to the same augmentations.\n",
1046
    "# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!\n",
1047
    "seq_det = seq.to_deterministic()\n",
1048
    "\n",
1049
    "##################################################\n",
1050
    "# 2. loop through images\n",
1051
    "##################################################\n",
1052
    "\n",
1053
    "for img in imageList:\n",
1054
    "    print(img)\n",
1055
    "    # Grayscale images must have shape (height, width, 1) each.\n",
1056
    "    #print(os.listdir(dataPath+'images/'))\n",
1057
    "    currentimage = skimage.io.imread(dataPath+'images/'+img).astype(np.uint8)\n",
1058
    "    # gray2rgb() simply duplicates the gray values over the three color channels.\n",
1059
    "    currentimage = color.gray2rgb(currentimage)\n",
1060
    "    bbs = bboxSetupInImage(dataPath , img.rstrip('.jpg') + '.txt',currentimage)\n",
1061
    "    # Augment BBs and images.\n",
1062
    "    # As we only have one image and list of BBs, we use\n",
1063
    "    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your\n",
1064
    "    # variables would likely already be lists.\n",
1065
    "    image_aug = seq_det.augment_images([currentimage])[0]\n",
1066
    "    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]\n",
1067
    "    augImgFolder = current_operation + \"Images\"\n",
1068
    "    augTxTFolder = current_operation + \"TXT\"\n",
1069
    "    createFolder(augImgFolder)\n",
1070
    "    createFolder(augTxTFolder)\n",
1071
    "    # Save aug images and bboxes\n",
1072
    "    skimage.io.imsave(augImgFolder + '/'+\n",
1073
    "                      img.rstrip('.jpg') + \n",
1074
    "                      '_' + current_operation +\n",
1075
    "                      '.jpg'\n",
1076
    "                      ,image_aug)\n",
1077
    "    saveAugbbox2TXT(augTxTFolder+ '/'+ \n",
1078
    "                    img.rstrip('.jpg') + \n",
1079
    "                    '_'+ current_operation +\n",
1080
    "                    '.txt',bbs_aug)\n",
1081
    "    # image with BBs before/after augmentation (shown below)\n",
1082
    "    # image_before = bbs.draw_on_image(currentimage, thickness=2)\n",
1083
    "    # image_after = bbs_aug.draw_on_image(image_aug, \n",
1084
    "    # thickness=2, color=[0, 0, 255])\n",
1085
    "    # image with BBs before/after augmentation (shown below)\n",
1086
    "    # plot and save figures before and after data augmentations\n",
1087
    "    #skimage.io.imshow(image_before)\n",
1088
    "    #skimage.io.imshow(image_after)\n",
1089
    "    # for i in range(len(bbs.bounding_boxes)):\n",
1090
    "    #     before = bbs.bounding_boxes[i]\n",
1091
    "    #     after = bbs_aug.bounding_boxes[i]\n",
1092
    "    #     print(\"BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)\" % (\n",
1093
    "    #         i,\n",
1094
    "    #         before.x1, before.y1, before.x2, before.y2,\n",
1095
    "    #         after.x1, after.y1, after.x2, after.y2)\n",
1096
    "    #     )"
1097
   ]
1098
  },
1099
  {
1100
   "cell_type": "heading",
1101
   "metadata": {},
1102
   "level": 1,
1103
   "source": [
1104
    "GaussianBlur"
1105
   ]
1106
  },
1107
  {
1108
   "cell_type": "code",
1109
   "execution_count": 31,
1110
   "metadata": {},
1111
   "outputs": [
1112
    {
1113
     "name": "stdout",
1114
     "output_type": "stream",
1115
     "text": [
1116
      "0501_300kx_1nm_clhaadf3_0006.jpg\n"
1117
     ]
1118
    },
1119
    {
1120
     "name": "stdout",
1121
     "output_type": "stream",
1122
     "text": [
1123
      "0501_300kx_1nm_clhaadf3_0008.jpg\n"
1124
     ]
1125
    },
1126
    {
1127
     "name": "stdout",
1128
     "output_type": "stream",
1129
     "text": [
1130
      "0501_300kx_1nm_clhaadf3_0012.jpg\n"
1131
     ]
1132
    },
1133
    {
1134
     "name": "stdout",
1135
     "output_type": "stream",
1136
     "text": [
1137
      "0501_300kx_1nm_clhaadf3_0016.jpg\n"
1138
     ]
1139
    },
1140
    {
1141
     "name": "stdout",
1142
     "output_type": "stream",
1143
     "text": [
1144
      "0501_300kx_1nm_clhaadf3_0018.jpg\n"
1145
     ]
1146
    },
1147
    {
1148
     "name": "stdout",
1149
     "output_type": "stream",
1150
     "text": [
1151
      "0501_300kx_1nm_clhaadf3_0020.jpg\n"
1152
     ]
1153
    },
1154
    {
1155
     "name": "stdout",
1156
     "output_type": "stream",
1157
     "text": [
1158
      "0501_300kx_1nm_clhaadf3_0022.jpg\n"
1159
     ]
1160
    },
1161
    {
1162
     "name": "stdout",
1163
     "output_type": "stream",
1164
     "text": [
1165
      "0501_300kx_1nm_clhaadf3_0024.jpg\n"
1166
     ]
1167
    },
1168
    {
1169
     "name": "stdout",
1170
     "output_type": "stream",
1171
     "text": [
1172
      "0501_300kx_1nm_clhaadf3_0026.jpg\n"
1173
     ]
1174
    },
1175
    {
1176
     "name": "stdout",
1177
     "output_type": "stream",
1178
     "text": [
1179
      "0501_300kx_1nm_clhaadf3_0030.jpg\n"
1180
     ]
1181
    },
1182
    {
1183
     "name": "stdout",
1184
     "output_type": "stream",
1185
     "text": [
1186
      "0501_300kx_1nm_clhaadf3_0031.jpg\n"
1187
     ]
1188
    },
1189
    {
1190
     "name": "stdout",
1191
     "output_type": "stream",
1192
     "text": [
1193
      "0501_300kx_1nm_clhaadf3_0034.jpg\n"
1194
     ]
1195
    },
1196
    {
1197
     "name": "stdout",
1198
     "output_type": "stream",
1199
     "text": [
1200
      "0501_300kx_1nm_clhaadf3_0036.jpg\n"
1201
     ]
1202
    },
1203
    {
1204
     "name": "stdout",
1205
     "output_type": "stream",
1206
     "text": [
1207
      "0501_300kx_1nm_clhaadf3_0040.jpg\n"
1208
     ]
1209
    },
1210
    {
1211
     "name": "stdout",
1212
     "output_type": "stream",
1213
     "text": [
1214
      "0501_300kx_1nm_clhaadf3_0044.jpg\n"
1215
     ]
1216
    },
1217
    {
1218
     "name": "stdout",
1219
     "output_type": "stream",
1220
     "text": [
1221
      "0501_300kx_1nm_clhaadf3_0046.jpg\n"
1222
     ]
1223
    },
1224
    {
1225
     "name": "stdout",
1226
     "output_type": "stream",
1227
     "text": [
1228
      "0501_300kx_1nm_clhaadf3_0048.jpg\n"
1229
     ]
1230
    },
1231
    {
1232
     "name": "stdout",
1233
     "output_type": "stream",
1234
     "text": [
1235
      "0501_300kx_1nm_clhaadf3_0050.jpg\n"
1236
     ]
1237
    },
1238
    {
1239
     "name": "stdout",
1240
     "output_type": "stream",
1241
     "text": [
1242
      "200kV_500kx_p2nm_8cmCL_grain1_0002.jpg\n"
1243
     ]
1244
    },
1245
    {
1246
     "name": "stdout",
1247
     "output_type": "stream",
1248
     "text": [
1249
      "200kV_500kx_p2nm_8cmCL_grain1_0002_n.jpg\n"
1250
     ]
1251
    },
1252
    {
1253
     "name": "stdout",
1254
     "output_type": "stream",
1255
     "text": [
1256
      "200kV_500kx_p2nm_8cmCL_grain1_0004_n.jpg\n"
1257
     ]
1258
    },
1259
    {
1260
     "name": "stdout",
1261
     "output_type": "stream",
1262
     "text": [
1263
      "200kV_500kx_p2nm_8cmCL_grain1_0048 - Copy.jpg\n"
1264
     ]
1265
    },
1266
    {
1267
     "name": "stdout",
1268
     "output_type": "stream",
1269
     "text": [
1270
      "200kV_500kx_p2nm_8cmCL_grain1_0050 - Copy.jpg\n"
1271
     ]
1272
    },
1273
    {
1274
     "name": "stdout",
1275
     "output_type": "stream",
1276
     "text": [
1277
      "200kV_500kx_p2nm_8cmCL_grain1_0052 - Copy.jpg\n"
1278
     ]
1279
    },
1280
    {
1281
     "name": "stdout",
1282
     "output_type": "stream",
1283
     "text": [
1284
      "200kV_500kx_p2nm_8cmCL_grain1_0054 - Copy.jpg\n"
1285
     ]
1286
    },
1287
    {
1288
     "name": "stdout",
1289
     "output_type": "stream",
1290
     "text": [
1291
      "200kV_500kx_p2nm_8cmCL_grain1_0058 - Copy.jpg\n"
1292
     ]
1293
    },
1294
    {
1295
     "name": "stdout",
1296
     "output_type": "stream",
1297
     "text": [
1298
      "200kV_500kx_p2nm_8cmCL_grain1_0060 - Copy.jpg\n"
1299
     ]
1300
    },
1301
    {
1302
     "name": "stdout",
1303
     "output_type": "stream",
1304
     "text": [
1305
      "200kV_500kx_p2nm_8cmCL_grain1_0062 - Copy.jpg\n"
1306
     ]
1307
    },
1308
    {
1309
     "name": "stdout",
1310
     "output_type": "stream",
1311
     "text": [
1312
      "200kV_500kx_p2nm_8cmCL_grain1_0064 - Copy.jpg\n"
1313
     ]
1314
    },
1315
    {
1316
     "name": "stdout",
1317
     "output_type": "stream",
1318
     "text": [
1319
      "200kV_500kx_p2nm_8cmCL_grain1_0066 - Copy.jpg\n"
1320
     ]
1321
    },
1322
    {
1323
     "name": "stdout",
1324
     "output_type": "stream",
1325
     "text": [
1326
      "200kV_500kx_p2nm_8cmCL_grain1_0068 - Copy.jpg\n"
1327
     ]
1328
    },
1329
    {
1330
     "name": "stdout",
1331
     "output_type": "stream",
1332
     "text": [
1333
      "200kV_500kx_p2nm_8cmCL_grain1_0070 - Copy.jpg\n"
1334
     ]
1335
    },
1336
    {
1337
     "name": "stdout",
1338
     "output_type": "stream",
1339
     "text": [
1340
      "200kV_500kx_p2nm_8cmCL_grain1_0072 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0074 - Copy.jpg\n"
1341
     ]
1342
    },
1343
    {
1344
     "name": "stdout",
1345
     "output_type": "stream",
1346
     "text": [
1347
      "200kV_500kx_p2nm_8cmCL_grain1_0076 - Copy.jpg\n"
1348
     ]
1349
    },
1350
    {
1351
     "name": "stdout",
1352
     "output_type": "stream",
1353
     "text": [
1354
      "200kV_500kx_p2nm_8cmCL_grain1_0078 - Copy.jpg\n"
1355
     ]
1356
    },
1357
    {
1358
     "name": "stdout",
1359
     "output_type": "stream",
1360
     "text": [
1361
      "200kV_500kx_p2nm_8cmCL_grain1_0080 - Copy.jpg\n"
1362
     ]
1363
    },
1364
    {
1365
     "name": "stdout",
1366
     "output_type": "stream",
1367
     "text": [
1368
      "200kV_500kx_p2nm_8cmCL_grain1_0084 - Copy.jpg\n"
1369
     ]
1370
    },
1371
    {
1372
     "name": "stdout",
1373
     "output_type": "stream",
1374
     "text": [
1375
      "200kV_500kx_p2nm_8cmCL_grain1_0086 - Copy.jpg\n"
1376
     ]
1377
    },
1378
    {
1379
     "name": "stdout",
1380
     "output_type": "stream",
1381
     "text": [
1382
      "200kV_500kx_p2nm_8cmCL_grain1_0090 - Copy.jpg\n"
1383
     ]
1384
    },
1385
    {
1386
     "name": "stdout",
1387
     "output_type": "stream",
1388
     "text": [
1389
      "200kV_500kx_p2nm_8cmCL_grain1_0092 - Copy.jpg\n"
1390
     ]
1391
    },
1392
    {
1393
     "name": "stdout",
1394
     "output_type": "stream",
1395
     "text": [
1396
      "200kV_500kx_p2nm_8cmCL_grain1_0109.jpg\n"
1397
     ]
1398
    },
1399
    {
1400
     "name": "stdout",
1401
     "output_type": "stream",
1402
     "text": [
1403
      "200kV_500kx_p2nm_8cmCL_grain1_0111.jpg\n"
1404
     ]
1405
    },
1406
    {
1407
     "name": "stdout",
1408
     "output_type": "stream",
1409
     "text": [
1410
      "200kV_500kx_p2nm_8cmCL_grain1_0129.jpg\n"
1411
     ]
1412
    },
1413
    {
1414
     "name": "stdout",
1415
     "output_type": "stream",
1416
     "text": [
1417
      "200kV_500kx_p2nm_8cmCL_grain2_0010.jpg\n"
1418
     ]
1419
    },
1420
    {
1421
     "name": "stdout",
1422
     "output_type": "stream",
1423
     "text": [
1424
      "200kV_500kx_p2nm_8cmCL_grain2_0012.jpg\n"
1425
     ]
1426
    },
1427
    {
1428
     "name": "stdout",
1429
     "output_type": "stream",
1430
     "text": [
1431
      "200kV_500kx_p2nm_8cmCL_grain3_4_0258.jpg\n"
1432
     ]
1433
    },
1434
    {
1435
     "name": "stdout",
1436
     "output_type": "stream",
1437
     "text": [
1438
      "2501_300kx_1nm_clhaadf3_0032.jpg\n"
1439
     ]
1440
    },
1441
    {
1442
     "name": "stdout",
1443
     "output_type": "stream",
1444
     "text": [
1445
      "2501_300kx_1nm_clhaadf3_0034.jpg\n"
1446
     ]
1447
    },
1448
    {
1449
     "name": "stdout",
1450
     "output_type": "stream",
1451
     "text": [
1452
      "2501_300kx_1nm_clhaadf3_0036.jpg\n"
1453
     ]
1454
    },
1455
    {
1456
     "name": "stdout",
1457
     "output_type": "stream",
1458
     "text": [
1459
      "2501_300kx_1nm_clhaadf3_0038.jpg\n2501_300kx_1nm_clhaadf3_0040.jpg\n"
1460
     ]
1461
    },
1462
    {
1463
     "name": "stdout",
1464
     "output_type": "stream",
1465
     "text": [
1466
      "2501_300kx_1nm_clhaadf3_0042.jpg\n"
1467
     ]
1468
    },
1469
    {
1470
     "name": "stdout",
1471
     "output_type": "stream",
1472
     "text": [
1473
      "2501_300kx_1nm_clhaadf3_0044.jpg\n"
1474
     ]
1475
    },
1476
    {
1477
     "name": "stdout",
1478
     "output_type": "stream",
1479
     "text": [
1480
      "2501_300kx_1nm_clhaadf3_0046.jpg\n"
1481
     ]
1482
    },
1483
    {
1484
     "name": "stdout",
1485
     "output_type": "stream",
1486
     "text": [
1487
      "2501_300kx_1nm_clhaadf3_0048.jpg\n"
1488
     ]
1489
    },
1490
    {
1491
     "name": "stdout",
1492
     "output_type": "stream",
1493
     "text": [
1494
      "2501_300kx_1nm_clhaadf3_0050.jpg\n"
1495
     ]
1496
    },
1497
    {
1498
     "name": "stdout",
1499
     "output_type": "stream",
1500
     "text": [
1501
      "2501_300kx_1nm_clhaadf3_0052.jpg\n"
1502
     ]
1503
    },
1504
    {
1505
     "name": "stdout",
1506
     "output_type": "stream",
1507
     "text": [
1508
      "2501_300kx_1nm_clhaadf3_0054.jpg\n"
1509
     ]
1510
    },
1511
    {
1512
     "name": "stdout",
1513
     "output_type": "stream",
1514
     "text": [
1515
      "2ROI_100kx_4100CL_foil1.jpg\n"
1516
     ]
1517
    },
1518
    {
1519
     "name": "stdout",
1520
     "output_type": "stream",
1521
     "text": [
1522
      "3ROI_100kx_4100CL_foil1.jpg\n"
1523
     ]
1524
    },
1525
    {
1526
     "name": "stdout",
1527
     "output_type": "stream",
1528
     "text": [
1529
      "4ROI_100kx_4100CL_foil1.jpg\n"
1530
     ]
1531
    },
1532
    {
1533
     "name": "stdout",
1534
     "output_type": "stream",
1535
     "text": [
1536
      "5401_300kx_1nm_clhaadf3_0004.jpg\n5401_300kx_1nm_clhaadf3_0006.jpg\n"
1537
     ]
1538
    },
1539
    {
1540
     "name": "stdout",
1541
     "output_type": "stream",
1542
     "text": [
1543
      "5401_300kx_1nm_clhaadf3_0016.jpg\n5401_300kx_1nm_clhaadf3_0018.jpg\n"
1544
     ]
1545
    },
1546
    {
1547
     "name": "stdout",
1548
     "output_type": "stream",
1549
     "text": [
1550
      "5401_300kx_1nm_clhaadf3_0022.jpg\n"
1551
     ]
1552
    },
1553
    {
1554
     "name": "stdout",
1555
     "output_type": "stream",
1556
     "text": [
1557
      "5401_300kx_1nm_clhaadf3_0024.jpg\n5401_300kx_1nm_clhaadf3_0026.jpg\n"
1558
     ]
1559
    },
1560
    {
1561
     "name": "stdout",
1562
     "output_type": "stream",
1563
     "text": [
1564
      "5401_300kx_1nm_clhaadf3_0030.jpg\n"
1565
     ]
1566
    },
1567
    {
1568
     "name": "stdout",
1569
     "output_type": "stream",
1570
     "text": [
1571
      "5ROI_100kx_4100CL_foil1 copy.jpg\n"
1572
     ]
1573
    },
1574
    {
1575
     "name": "stdout",
1576
     "output_type": "stream",
1577
     "text": [
1578
      "6ROI_100kx_4100CL_foil1 copy.jpg\n"
1579
     ]
1580
    },
1581
    {
1582
     "name": "stdout",
1583
     "output_type": "stream",
1584
     "text": [
1585
      "7ROI_100kx_4100CL_foil1 copy.jpg\n"
1586
     ]
1587
    },
1588
    {
1589
     "name": "stdout",
1590
     "output_type": "stream",
1591
     "text": [
1592
      "9ROI_100kx_4100CL_foil1.jpg\n"
1593
     ]
1594
    },
1595
    {
1596
     "name": "stdout",
1597
     "output_type": "stream",
1598
     "text": [
1599
      "BF X500K, 01 (2).jpg\n"
1600
     ]
1601
    },
1602
    {
1603
     "name": "stdout",
1604
     "output_type": "stream",
1605
     "text": [
1606
      "BF X500K, 01 (3).jpg\n"
1607
     ]
1608
    },
1609
    {
1610
     "name": "stdout",
1611
     "output_type": "stream",
1612
     "text": [
1613
      "BF X500K, 02 (2).jpg\n"
1614
     ]
1615
    },
1616
    {
1617
     "name": "stdout",
1618
     "output_type": "stream",
1619
     "text": [
1620
      "BF X500K, 03 (2).jpg\n"
1621
     ]
1622
    },
1623
    {
1624
     "name": "stdout",
1625
     "output_type": "stream",
1626
     "text": [
1627
      "BF X500K, 04.jpg\n"
1628
     ]
1629
    },
1630
    {
1631
     "name": "stdout",
1632
     "output_type": "stream",
1633
     "text": [
1634
      "BF X500K, 05 (2).jpg\n"
1635
     ]
1636
    },
1637
    {
1638
     "name": "stdout",
1639
     "output_type": "stream",
1640
     "text": [
1641
      "BF X500K, 05.jpg\n"
1642
     ]
1643
    },
1644
    {
1645
     "name": "stdout",
1646
     "output_type": "stream",
1647
     "text": [
1648
      "BF X500K, 06 (3).jpg\n"
1649
     ]
1650
    },
1651
    {
1652
     "name": "stdout",
1653
     "output_type": "stream",
1654
     "text": [
1655
      "BF X500K, 06.jpg\n"
1656
     ]
1657
    },
1658
    {
1659
     "name": "stdout",
1660
     "output_type": "stream",
1661
     "text": [
1662
      "BF X500K, 07 (2).jpg\n"
1663
     ]
1664
    },
1665
    {
1666
     "name": "stdout",
1667
     "output_type": "stream",
1668
     "text": [
1669
      "BF X500K, 08 (2).jpg\n"
1670
     ]
1671
    },
1672
    {
1673
     "name": "stdout",
1674
     "output_type": "stream",
1675
     "text": [
1676
      "BF X500K, 08 (3).jpg\n"
1677
     ]
1678
    },
1679
    {
1680
     "name": "stdout",
1681
     "output_type": "stream",
1682
     "text": [
1683
      "BF X500K, 08.jpg\n"
1684
     ]
1685
    },
1686
    {
1687
     "name": "stdout",
1688
     "output_type": "stream",
1689
     "text": [
1690
      "BF X500K, 10 (2).jpg\n"
1691
     ]
1692
    },
1693
    {
1694
     "name": "stdout",
1695
     "output_type": "stream",
1696
     "text": [
1697
      "BF X500K, 10 (3).jpg\n"
1698
     ]
1699
    },
1700
    {
1701
     "name": "stdout",
1702
     "output_type": "stream",
1703
     "text": [
1704
      "BF X500K, 10.jpg\n"
1705
     ]
1706
    },
1707
    {
1708
     "name": "stdout",
1709
     "output_type": "stream",
1710
     "text": [
1711
      "BF X500K, 11 (2).jpg\n"
1712
     ]
1713
    },
1714
    {
1715
     "name": "stdout",
1716
     "output_type": "stream",
1717
     "text": [
1718
      "BF X500K, 11 (3).jpg\n"
1719
     ]
1720
    },
1721
    {
1722
     "name": "stdout",
1723
     "output_type": "stream",
1724
     "text": [
1725
      "BF X500K, 11.jpg\n"
1726
     ]
1727
    },
1728
    {
1729
     "name": "stdout",
1730
     "output_type": "stream",
1731
     "text": [
1732
      "BF X500K, 12 (2).jpg\n"
1733
     ]
1734
    },
1735
    {
1736
     "name": "stdout",
1737
     "output_type": "stream",
1738
     "text": [
1739
      "BF X500K, 12 (3).jpg\n"
1740
     ]
1741
    },
1742
    {
1743
     "name": "stdout",
1744
     "output_type": "stream",
1745
     "text": [
1746
      "BF X500K, 12.jpg\n"
1747
     ]
1748
    },
1749
    {
1750
     "name": "stdout",
1751
     "output_type": "stream",
1752
     "text": [
1753
      "BF X500K, 13 (3).jpg\n"
1754
     ]
1755
    },
1756
    {
1757
     "name": "stdout",
1758
     "output_type": "stream",
1759
     "text": [
1760
      "BF X500K, 14.jpg\n"
1761
     ]
1762
    },
1763
    {
1764
     "name": "stdout",
1765
     "output_type": "stream",
1766
     "text": [
1767
      "BF X500K, 15.jpg\n"
1768
     ]
1769
    },
1770
    {
1771
     "name": "stdout",
1772
     "output_type": "stream",
1773
     "text": [
1774
      "BF X500K, 16.jpg\n"
1775
     ]
1776
    },
1777
    {
1778
     "name": "stdout",
1779
     "output_type": "stream",
1780
     "text": [
1781
      "K713_300kx_store4_grid1_0005.jpg\n"
1782
     ]
1783
    },
1784
    {
1785
     "name": "stdout",
1786
     "output_type": "stream",
1787
     "text": [
1788
      "K713_300kx_store4_grid1_0007.jpg\n"
1789
     ]
1790
    },
1791
    {
1792
     "name": "stdout",
1793
     "output_type": "stream",
1794
     "text": [
1795
      "K713_300kx_store4_grid1_0009.jpg\n"
1796
     ]
1797
    },
1798
    {
1799
     "name": "stdout",
1800
     "output_type": "stream",
1801
     "text": [
1802
      "K713_300kx_store4_grid1_0011.jpg\n"
1803
     ]
1804
    },
1805
    {
1806
     "name": "stdout",
1807
     "output_type": "stream",
1808
     "text": [
1809
      "K713_300kx_store4_grid1_0013.jpg\n"
1810
     ]
1811
    },
1812
    {
1813
     "name": "stdout",
1814
     "output_type": "stream",
1815
     "text": [
1816
      "K713_300kx_store4_grid1_0015.jpg\nK713_300kx_store4_grid1_0017.jpg"
1817
     ]
1818
    },
1819
    {
1820
     "name": "stdout",
1821
     "output_type": "stream",
1822
     "text": [
1823
      "\n"
1824
     ]
1825
    },
1826
    {
1827
     "name": "stdout",
1828
     "output_type": "stream",
1829
     "text": [
1830
      "K713_300kx_store4_grid1_0019.jpg\n"
1831
     ]
1832
    },
1833
    {
1834
     "name": "stdout",
1835
     "output_type": "stream",
1836
     "text": [
1837
      "dalong2.jpg\n"
1838
     ]
1839
    },
1840
    {
1841
     "name": "stdout",
1842
     "output_type": "stream",
1843
     "text": [
1844
      "dalong3.jpg\n"
1845
     ]
1846
    },
1847
    {
1848
     "name": "stdout",
1849
     "output_type": "stream",
1850
     "text": [
1851
      "g1_backonzone_GBtowardsfrom_0005.jpg\n"
1852
     ]
1853
    },
1854
    {
1855
     "name": "stdout",
1856
     "output_type": "stream",
1857
     "text": [
1858
      "g1_frontonzone_GBtowardsback_0006.jpg\n"
1859
     ]
1860
    },
1861
    {
1862
     "name": "stdout",
1863
     "output_type": "stream",
1864
     "text": [
1865
      "g2_midonzone_GBtowardsfront_0005.jpg\n"
1866
     ]
1867
    },
1868
    {
1869
     "name": "stdout",
1870
     "output_type": "stream",
1871
     "text": [
1872
      "g2_midonzone_GBtowardsfront_0034.jpg\n"
1873
     ]
1874
    },
1875
    {
1876
     "name": "stdout",
1877
     "output_type": "stream",
1878
     "text": [
1879
      "g2_midonzone_GBtowardsmmiddle_0003.jpg\n"
1880
     ]
1881
    },
1882
    {
1883
     "name": "stdout",
1884
     "output_type": "stream",
1885
     "text": [
1886
      "g2_midonzone_GBtowardsmmiddle_0005.jpg\n"
1887
     ]
1888
    },
1889
    {
1890
     "name": "stdout",
1891
     "output_type": "stream",
1892
     "text": [
1893
      "grid1_roi1_500kx_0p5nm_haadf1_0001.jpg\n"
1894
     ]
1895
    },
1896
    {
1897
     "name": "stdout",
1898
     "output_type": "stream",
1899
     "text": [
1900
      "grid1_roi1_500kx_0p5nm_haadf1_0003.jpg\n"
1901
     ]
1902
    },
1903
    {
1904
     "name": "stdout",
1905
     "output_type": "stream",
1906
     "text": [
1907
      "grid1_roi1_500kx_0p5nm_haadf1_0005.jpg\n"
1908
     ]
1909
    },
1910
    {
1911
     "name": "stdout",
1912
     "output_type": "stream",
1913
     "text": [
1914
      "grid1_roi1_500kx_0p5nm_haadf1_0007.jpg\n"
1915
     ]
1916
    },
1917
    {
1918
     "name": "stdout",
1919
     "output_type": "stream",
1920
     "text": [
1921
      "grid1_roi1_500kx_0p5nm_haadf1_0009.jpg\n"
1922
     ]
1923
    },
1924
    {
1925
     "name": "stdout",
1926
     "output_type": "stream",
1927
     "text": [
1928
      "grid1_roi1_500kx_0p5nm_haadf1_0011.jpg\n"
1929
     ]
1930
    },
1931
    {
1932
     "name": "stdout",
1933
     "output_type": "stream",
1934
     "text": [
1935
      "grid1_roi1_500kx_0p5nm_haadf1_0013.jpg\n"
1936
     ]
1937
    },
1938
    {
1939
     "name": "stdout",
1940
     "output_type": "stream",
1941
     "text": [
1942
      "grid1_roi1_500kx_0p5nm_haadf1_0015.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0017.jpg\n"
1943
     ]
1944
    },
1945
    {
1946
     "name": "stdout",
1947
     "output_type": "stream",
1948
     "text": [
1949
      "grid1_roi1_500kx_0p5nm_haadf1_0019.jpg\n"
1950
     ]
1951
    },
1952
    {
1953
     "name": "stdout",
1954
     "output_type": "stream",
1955
     "text": [
1956
      "grid1_roi1_500kx_0p5nm_haadf1_0021.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0023.jpg\n"
1957
     ]
1958
    },
1959
    {
1960
     "name": "stdout",
1961
     "output_type": "stream",
1962
     "text": [
1963
      "grid1_roi1_500kx_0p5nm_haadf1_0027.jpg\n"
1964
     ]
1965
    },
1966
    {
1967
     "name": "stdout",
1968
     "output_type": "stream",
1969
     "text": [
1970
      "grid1_roi1_500kx_0p5nm_haadf1_0029.jpg\n"
1971
     ]
1972
    },
1973
    {
1974
     "name": "stdout",
1975
     "output_type": "stream",
1976
     "text": [
1977
      "grid1_roi1_500kx_0p5nm_haadf1_0031.jpg\n"
1978
     ]
1979
    },
1980
    {
1981
     "name": "stdout",
1982
     "output_type": "stream",
1983
     "text": [
1984
      "grid1_roi2_500kx_0p5nm_haadf1_0035.jpg\n"
1985
     ]
1986
    },
1987
    {
1988
     "name": "stdout",
1989
     "output_type": "stream",
1990
     "text": [
1991
      "grid1_roi2_500kx_0p5nm_haadf1_0037.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0039.jpg\n"
1992
     ]
1993
    },
1994
    {
1995
     "name": "stdout",
1996
     "output_type": "stream",
1997
     "text": [
1998
      "grid1_roi2_500kx_0p5nm_haadf1_0041.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0044.jpg\n"
1999
     ]
2000
    },
2001
    {
2002
     "name": "stdout",
2003
     "output_type": "stream",
2004
     "text": [
2005
      "grid1_roi2_500kx_0p5nm_haadf1_0045.jpg\n"
2006
     ]
2007
    },
2008
    {
2009
     "name": "stdout",
2010
     "output_type": "stream",
2011
     "text": [
2012
      "grid1_roi2_500kx_0p5nm_haadf1_0049.jpg\n"
2013
     ]
2014
    },
2015
    {
2016
     "name": "stdout",
2017
     "output_type": "stream",
2018
     "text": [
2019
      "grid1_roi2_500kx_0p5nm_haadf1_0051.jpg\n"
2020
     ]
2021
    },
2022
    {
2023
     "name": "stdout",
2024
     "output_type": "stream",
2025
     "text": [
2026
      "grid1_roi2_500kx_0p5nm_haadf1_0053.jpg\n"
2027
     ]
2028
    },
2029
    {
2030
     "name": "stdout",
2031
     "output_type": "stream",
2032
     "text": [
2033
      "grid1_roi2_500kx_0p5nm_haadf1_0055.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0057.jpg\n"
2034
     ]
2035
    },
2036
    {
2037
     "name": "stdout",
2038
     "output_type": "stream",
2039
     "text": [
2040
      "grid1_roi2_500kx_0p5nm_haadf1_0059.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0061.jpg\n"
2041
     ]
2042
    },
2043
    {
2044
     "name": "stdout",
2045
     "output_type": "stream",
2046
     "text": [
2047
      "grid1_roi2_500kx_0p5nm_haadf1_0063.jpg\n"
2048
     ]
2049
    },
2050
    {
2051
     "name": "stdout",
2052
     "output_type": "stream",
2053
     "text": [
2054
      "grid1_roi2_500kx_0p5nm_haadf1_0065.jpg\n"
2055
     ]
2056
    },
2057
    {
2058
     "name": "stdout",
2059
     "output_type": "stream",
2060
     "text": [
2061
      "grid1_roi2_500kx_0p5nm_haadf1_0067.jpg\n"
2062
     ]
2063
    },
2064
    {
2065
     "name": "stdout",
2066
     "output_type": "stream",
2067
     "text": [
2068
      "grid1_roi2_500kx_0p5nm_haadf1_0069.jpg\n"
2069
     ]
2070
    },
2071
    {
2072
     "name": "stdout",
2073
     "output_type": "stream",
2074
     "text": [
2075
      "BF X500K, 05 (3).jpg\n"
2076
     ]
2077
    },
2078
    {
2079
     "name": "stdout",
2080
     "output_type": "stream",
2081
     "text": [
2082
      "0501_300kx_1nm_clhaadf3_0038.jpg\n"
2083
     ]
2084
    }
2085
   ],
2086
   "source": [
2087
    "##################################################\n",
2088
    "# 1. Define data augmentation operations\n",
2089
    "##################################################\n",
2090
    "\n",
2091
    "trainImageTxtFile = dataPath + \"trainimages.txt\"\n",
2092
    "imageList = getImageList(trainImageTxtFile)\n",
2093
    "\n",
2094
    "current_operation = \"GaussianBlur\"\n",
2095
    "\n",
2096
    "# blur images with a sigma of 0 to 3.0  \n",
2097
    "from imgaug import augmenters as iaa\n",
2098
    "ia.seed(1)\n",
2099
    "seq = iaa.Sequential([\n",
2100
    "  iaa.GaussianBlur(sigma=(0, 3)) \n",
2101
    "])\n",
2102
    "\n",
2103
    "# seq = iaa.Sequential([ \n",
2104
    "#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.\n",
2105
    "#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.\n",
2106
    "#     iaa.GammaContrast((0.5, 1.5))\n",
2107
    "# ])\n",
2108
    "\n",
2109
    "# Make our sequence deterministic.\n",
2110
    "# We can now apply it to the image and then to the BBs and it will\n",
2111
    "# lead to the same augmentations.\n",
2112
    "# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!\n",
2113
    "seq_det = seq.to_deterministic()\n",
2114
    "\n",
2115
    "##################################################\n",
2116
    "# 2. loop through images\n",
2117
    "##################################################\n",
2118
    "\n",
2119
    "for img in imageList:\n",
2120
    "    print(img)\n",
2121
    "    # Grayscale images must have shape (height, width, 1) each.\n",
2122
    "    #print(os.listdir(dataPath+'images/'))\n",
2123
    "    currentimage = skimage.io.imread(dataPath+'images/'+img).astype(np.uint8)\n",
2124
    "    # gray2rgb() simply duplicates the gray values over the three color channels.\n",
2125
    "    currentimage = color.gray2rgb(currentimage)\n",
2126
    "    bbs = bboxSetupInImage(dataPath , img.rstrip('.jpg') + '.txt',currentimage)\n",
2127
    "    # Augment BBs and images.\n",
2128
    "    # As we only have one image and list of BBs, we use\n",
2129
    "    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your\n",
2130
    "    # variables would likely already be lists.\n",
2131
    "    image_aug = seq_det.augment_images([currentimage])[0]\n",
2132
    "    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]\n",
2133
    "    augImgFolder = current_operation + \"Images\"\n",
2134
    "    augTxTFolder = current_operation + \"TXT\"\n",
2135
    "    createFolder(augImgFolder)\n",
2136
    "    createFolder(augTxTFolder)\n",
2137
    "    # Save aug images and bboxes\n",
2138
    "    skimage.io.imsave(augImgFolder + '/'+\n",
2139
    "                      img.rstrip('.jpg') + \n",
2140
    "                      '_' + current_operation +\n",
2141
    "                      '.jpg'\n",
2142
    "                      ,image_aug)\n",
2143
    "    saveAugbbox2TXT(augTxTFolder+ '/'+ \n",
2144
    "                    img.rstrip('.jpg') + \n",
2145
    "                    '_'+ current_operation +\n",
2146
    "                    '.txt',bbs_aug)\n",
2147
    "    # image with BBs before/after augmentation (shown below)\n",
2148
    "    # image_before = bbs.draw_on_image(currentimage, thickness=2)\n",
2149
    "    # image_after = bbs_aug.draw_on_image(image_aug, \n",
2150
    "    # thickness=2, color=[0, 0, 255])\n",
2151
    "    # image with BBs before/after augmentation (shown below)\n",
2152
    "    # plot and save figures before and after data augmentations\n",
2153
    "    #skimage.io.imshow(image_before)\n",
2154
    "    #skimage.io.imshow(image_after)\n",
2155
    "    # for i in range(len(bbs.bounding_boxes)):\n",
2156
    "    #     before = bbs.bounding_boxes[i]\n",
2157
    "    #     after = bbs_aug.bounding_boxes[i]\n",
2158
    "    #     print(\"BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)\" % (\n",
2159
    "    #         i,\n",
2160
    "    #         before.x1, before.y1, before.x2, before.y2,\n",
2161
    "    #         after.x1, after.y1, after.x2, after.y2)\n",
2162
    "    #     )"
2163
   ]
2164
  },
2165
  {
2166
   "cell_type": "heading",
2167
   "metadata": {},
2168
   "level": 2,
2169
   "source": [
2170
    "change brightness"
2171
   ]
2172
  },
2173
  {
2174
   "cell_type": "code",
2175
   "execution_count": 32,
2176
   "metadata": {},
2177
   "outputs": [
2178
    {
2179
     "name": "stdout",
2180
     "output_type": "stream",
2181
     "text": [
2182
      "0501_300kx_1nm_clhaadf3_0006.jpg\n0501_300kx_1nm_clhaadf3_0008.jpg\n"
2183
     ]
2184
    },
2185
    {
2186
     "name": "stdout",
2187
     "output_type": "stream",
2188
     "text": [
2189
      "0501_300kx_1nm_clhaadf3_0012.jpg\n"
2190
     ]
2191
    },
2192
    {
2193
     "name": "stdout",
2194
     "output_type": "stream",
2195
     "text": [
2196
      "0501_300kx_1nm_clhaadf3_0016.jpg\n0501_300kx_1nm_clhaadf3_0018.jpg\n"
2197
     ]
2198
    },
2199
    {
2200
     "name": "stdout",
2201
     "output_type": "stream",
2202
     "text": [
2203
      "0501_300kx_1nm_clhaadf3_0020.jpg\n0501_300kx_1nm_clhaadf3_0022.jpg\n"
2204
     ]
2205
    },
2206
    {
2207
     "name": "stdout",
2208
     "output_type": "stream",
2209
     "text": [
2210
      "0501_300kx_1nm_clhaadf3_0024.jpg\n0501_300kx_1nm_clhaadf3_0026.jpg\n"
2211
     ]
2212
    },
2213
    {
2214
     "name": "stdout",
2215
     "output_type": "stream",
2216
     "text": [
2217
      "0501_300kx_1nm_clhaadf3_0030.jpg\n0501_300kx_1nm_clhaadf3_0031.jpg\n"
2218
     ]
2219
    },
2220
    {
2221
     "name": "stdout",
2222
     "output_type": "stream",
2223
     "text": [
2224
      "0501_300kx_1nm_clhaadf3_0034.jpg\n"
2225
     ]
2226
    },
2227
    {
2228
     "name": "stdout",
2229
     "output_type": "stream",
2230
     "text": [
2231
      "0501_300kx_1nm_clhaadf3_0036.jpg\n0501_300kx_1nm_clhaadf3_0040.jpg\n"
2232
     ]
2233
    },
2234
    {
2235
     "name": "stdout",
2236
     "output_type": "stream",
2237
     "text": [
2238
      "0501_300kx_1nm_clhaadf3_0044.jpg\n"
2239
     ]
2240
    },
2241
    {
2242
     "name": "stdout",
2243
     "output_type": "stream",
2244
     "text": [
2245
      "0501_300kx_1nm_clhaadf3_0046.jpg\n0501_300kx_1nm_clhaadf3_0048.jpg\n"
2246
     ]
2247
    },
2248
    {
2249
     "name": "stdout",
2250
     "output_type": "stream",
2251
     "text": [
2252
      "0501_300kx_1nm_clhaadf3_0050.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0002.jpg\n"
2253
     ]
2254
    },
2255
    {
2256
     "name": "stdout",
2257
     "output_type": "stream",
2258
     "text": [
2259
      "200kV_500kx_p2nm_8cmCL_grain1_0002_n.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0004_n.jpg\n"
2260
     ]
2261
    },
2262
    {
2263
     "name": "stdout",
2264
     "output_type": "stream",
2265
     "text": [
2266
      "200kV_500kx_p2nm_8cmCL_grain1_0048 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0050 - Copy.jpg\n"
2267
     ]
2268
    },
2269
    {
2270
     "name": "stdout",
2271
     "output_type": "stream",
2272
     "text": [
2273
      "200kV_500kx_p2nm_8cmCL_grain1_0052 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0054 - Copy.jpg\n"
2274
     ]
2275
    },
2276
    {
2277
     "name": "stdout",
2278
     "output_type": "stream",
2279
     "text": [
2280
      "200kV_500kx_p2nm_8cmCL_grain1_0058 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0060 - Copy.jpg\n"
2281
     ]
2282
    },
2283
    {
2284
     "name": "stdout",
2285
     "output_type": "stream",
2286
     "text": [
2287
      "200kV_500kx_p2nm_8cmCL_grain1_0062 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0064 - Copy.jpg\n"
2288
     ]
2289
    },
2290
    {
2291
     "name": "stdout",
2292
     "output_type": "stream",
2293
     "text": [
2294
      "200kV_500kx_p2nm_8cmCL_grain1_0066 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0068 - Copy.jpg\n"
2295
     ]
2296
    },
2297
    {
2298
     "name": "stdout",
2299
     "output_type": "stream",
2300
     "text": [
2301
      "200kV_500kx_p2nm_8cmCL_grain1_0070 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0072 - Copy.jpg\n"
2302
     ]
2303
    },
2304
    {
2305
     "name": "stdout",
2306
     "output_type": "stream",
2307
     "text": [
2308
      "200kV_500kx_p2nm_8cmCL_grain1_0074 - Copy.jpg\n"
2309
     ]
2310
    },
2311
    {
2312
     "name": "stdout",
2313
     "output_type": "stream",
2314
     "text": [
2315
      "200kV_500kx_p2nm_8cmCL_grain1_0076 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0078 - Copy.jpg\n"
2316
     ]
2317
    },
2318
    {
2319
     "name": "stdout",
2320
     "output_type": "stream",
2321
     "text": [
2322
      "200kV_500kx_p2nm_8cmCL_grain1_0080 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0084 - Copy.jpg\n"
2323
     ]
2324
    },
2325
    {
2326
     "name": "stdout",
2327
     "output_type": "stream",
2328
     "text": [
2329
      "200kV_500kx_p2nm_8cmCL_grain1_0086 - Copy.jpg\n"
2330
     ]
2331
    },
2332
    {
2333
     "name": "stdout",
2334
     "output_type": "stream",
2335
     "text": [
2336
      "200kV_500kx_p2nm_8cmCL_grain1_0090 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0092 - Copy.jpg\n"
2337
     ]
2338
    },
2339
    {
2340
     "name": "stdout",
2341
     "output_type": "stream",
2342
     "text": [
2343
      "200kV_500kx_p2nm_8cmCL_grain1_0109.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0111.jpg\n"
2344
     ]
2345
    },
2346
    {
2347
     "name": "stdout",
2348
     "output_type": "stream",
2349
     "text": [
2350
      "200kV_500kx_p2nm_8cmCL_grain1_0129.jpg\n200kV_500kx_p2nm_8cmCL_grain2_0010.jpg\n"
2351
     ]
2352
    },
2353
    {
2354
     "name": "stdout",
2355
     "output_type": "stream",
2356
     "text": [
2357
      "200kV_500kx_p2nm_8cmCL_grain2_0012.jpg\n200kV_500kx_p2nm_8cmCL_grain3_4_0258.jpg\n"
2358
     ]
2359
    },
2360
    {
2361
     "name": "stdout",
2362
     "output_type": "stream",
2363
     "text": [
2364
      "2501_300kx_1nm_clhaadf3_0032.jpg\n"
2365
     ]
2366
    },
2367
    {
2368
     "name": "stdout",
2369
     "output_type": "stream",
2370
     "text": [
2371
      "2501_300kx_1nm_clhaadf3_0034.jpg\n"
2372
     ]
2373
    },
2374
    {
2375
     "name": "stdout",
2376
     "output_type": "stream",
2377
     "text": [
2378
      "2501_300kx_1nm_clhaadf3_0036.jpg\n"
2379
     ]
2380
    },
2381
    {
2382
     "name": "stdout",
2383
     "output_type": "stream",
2384
     "text": [
2385
      "2501_300kx_1nm_clhaadf3_0038.jpg\n"
2386
     ]
2387
    },
2388
    {
2389
     "name": "stdout",
2390
     "output_type": "stream",
2391
     "text": [
2392
      "2501_300kx_1nm_clhaadf3_0040.jpg\n2501_300kx_1nm_clhaadf3_0042.jpg\n"
2393
     ]
2394
    },
2395
    {
2396
     "name": "stdout",
2397
     "output_type": "stream",
2398
     "text": [
2399
      "2501_300kx_1nm_clhaadf3_0044.jpg\n2501_300kx_1nm_clhaadf3_0046.jpg\n"
2400
     ]
2401
    },
2402
    {
2403
     "name": "stdout",
2404
     "output_type": "stream",
2405
     "text": [
2406
      "2501_300kx_1nm_clhaadf3_0048.jpg\n"
2407
     ]
2408
    },
2409
    {
2410
     "name": "stdout",
2411
     "output_type": "stream",
2412
     "text": [
2413
      "2501_300kx_1nm_clhaadf3_0050.jpg\n2501_300kx_1nm_clhaadf3_0052.jpg\n"
2414
     ]
2415
    },
2416
    {
2417
     "name": "stdout",
2418
     "output_type": "stream",
2419
     "text": [
2420
      "2501_300kx_1nm_clhaadf3_0054.jpg\n2ROI_100kx_4100CL_foil1.jpg\n"
2421
     ]
2422
    },
2423
    {
2424
     "name": "stdout",
2425
     "output_type": "stream",
2426
     "text": [
2427
      "3ROI_100kx_4100CL_foil1.jpg\n"
2428
     ]
2429
    },
2430
    {
2431
     "name": "stdout",
2432
     "output_type": "stream",
2433
     "text": [
2434
      "4ROI_100kx_4100CL_foil1.jpg\n5401_300kx_1nm_clhaadf3_0004.jpg\n"
2435
     ]
2436
    },
2437
    {
2438
     "name": "stdout",
2439
     "output_type": "stream",
2440
     "text": [
2441
      "5401_300kx_1nm_clhaadf3_0006.jpg\n"
2442
     ]
2443
    },
2444
    {
2445
     "name": "stdout",
2446
     "output_type": "stream",
2447
     "text": [
2448
      "5401_300kx_1nm_clhaadf3_0016.jpg\n5401_300kx_1nm_clhaadf3_0018.jpg\n"
2449
     ]
2450
    },
2451
    {
2452
     "name": "stdout",
2453
     "output_type": "stream",
2454
     "text": [
2455
      "5401_300kx_1nm_clhaadf3_0022.jpg\n5401_300kx_1nm_clhaadf3_0024.jpg\n"
2456
     ]
2457
    },
2458
    {
2459
     "name": "stdout",
2460
     "output_type": "stream",
2461
     "text": [
2462
      "5401_300kx_1nm_clhaadf3_0026.jpg\n5401_300kx_1nm_clhaadf3_0030.jpg\n"
2463
     ]
2464
    },
2465
    {
2466
     "name": "stdout",
2467
     "output_type": "stream",
2468
     "text": [
2469
      "5ROI_100kx_4100CL_foil1 copy.jpg\n6ROI_100kx_4100CL_foil1 copy.jpg\n"
2470
     ]
2471
    },
2472
    {
2473
     "name": "stdout",
2474
     "output_type": "stream",
2475
     "text": [
2476
      "7ROI_100kx_4100CL_foil1 copy.jpg\n9ROI_100kx_4100CL_foil1.jpg\n"
2477
     ]
2478
    },
2479
    {
2480
     "name": "stdout",
2481
     "output_type": "stream",
2482
     "text": [
2483
      "BF X500K, 01 (2).jpg\n"
2484
     ]
2485
    },
2486
    {
2487
     "name": "stdout",
2488
     "output_type": "stream",
2489
     "text": [
2490
      "BF X500K, 01 (3).jpg\nBF X500K, 02 (2).jpg\n"
2491
     ]
2492
    },
2493
    {
2494
     "name": "stdout",
2495
     "output_type": "stream",
2496
     "text": [
2497
      "BF X500K, 03 (2).jpg\nBF X500K, 04.jpg\n"
2498
     ]
2499
    },
2500
    {
2501
     "name": "stdout",
2502
     "output_type": "stream",
2503
     "text": [
2504
      "BF X500K, 05 (2).jpg\n"
2505
     ]
2506
    },
2507
    {
2508
     "name": "stdout",
2509
     "output_type": "stream",
2510
     "text": [
2511
      "BF X500K, 05.jpg\nBF X500K, 06 (3).jpg\n"
2512
     ]
2513
    },
2514
    {
2515
     "name": "stdout",
2516
     "output_type": "stream",
2517
     "text": [
2518
      "BF X500K, 06.jpg\nBF X500K, 07 (2).jpg\n"
2519
     ]
2520
    },
2521
    {
2522
     "name": "stdout",
2523
     "output_type": "stream",
2524
     "text": [
2525
      "BF X500K, 08 (2).jpg\nBF X500K, 08 (3).jpg\n"
2526
     ]
2527
    },
2528
    {
2529
     "name": "stdout",
2530
     "output_type": "stream",
2531
     "text": [
2532
      "BF X500K, 08.jpg\nBF X500K, 10 (2).jpg\n"
2533
     ]
2534
    },
2535
    {
2536
     "name": "stdout",
2537
     "output_type": "stream",
2538
     "text": [
2539
      "BF X500K, 10 (3).jpg\n"
2540
     ]
2541
    },
2542
    {
2543
     "name": "stdout",
2544
     "output_type": "stream",
2545
     "text": [
2546
      "BF X500K, 10.jpg\nBF X500K, 11 (2).jpg\n"
2547
     ]
2548
    },
2549
    {
2550
     "name": "stdout",
2551
     "output_type": "stream",
2552
     "text": [
2553
      "BF X500K, 11 (3).jpg\nBF X500K, 11.jpg\n"
2554
     ]
2555
    },
2556
    {
2557
     "name": "stdout",
2558
     "output_type": "stream",
2559
     "text": [
2560
      "BF X500K, 12 (2).jpg\nBF X500K, 12 (3).jpg\n"
2561
     ]
2562
    },
2563
    {
2564
     "name": "stdout",
2565
     "output_type": "stream",
2566
     "text": [
2567
      "BF X500K, 12.jpg\n"
2568
     ]
2569
    },
2570
    {
2571
     "name": "stdout",
2572
     "output_type": "stream",
2573
     "text": [
2574
      "BF X500K, 13 (3).jpg\nBF X500K, 14.jpg\n"
2575
     ]
2576
    },
2577
    {
2578
     "name": "stdout",
2579
     "output_type": "stream",
2580
     "text": [
2581
      "BF X500K, 15.jpg\nBF X500K, 16.jpg\n"
2582
     ]
2583
    },
2584
    {
2585
     "name": "stdout",
2586
     "output_type": "stream",
2587
     "text": [
2588
      "K713_300kx_store4_grid1_0005.jpg\nK713_300kx_store4_grid1_0007.jpg\n"
2589
     ]
2590
    },
2591
    {
2592
     "name": "stdout",
2593
     "output_type": "stream",
2594
     "text": [
2595
      "K713_300kx_store4_grid1_0009.jpg\nK713_300kx_store4_grid1_0011.jpg\n"
2596
     ]
2597
    },
2598
    {
2599
     "name": "stdout",
2600
     "output_type": "stream",
2601
     "text": [
2602
      "K713_300kx_store4_grid1_0013.jpg\n"
2603
     ]
2604
    },
2605
    {
2606
     "name": "stdout",
2607
     "output_type": "stream",
2608
     "text": [
2609
      "K713_300kx_store4_grid1_0015.jpg\nK713_300kx_store4_grid1_0017.jpg\n"
2610
     ]
2611
    },
2612
    {
2613
     "name": "stdout",
2614
     "output_type": "stream",
2615
     "text": [
2616
      "K713_300kx_store4_grid1_0019.jpg\ndalong2.jpg\n"
2617
     ]
2618
    },
2619
    {
2620
     "name": "stdout",
2621
     "output_type": "stream",
2622
     "text": [
2623
      "dalong3.jpg\ng1_backonzone_GBtowardsfrom_0005.jpg\n"
2624
     ]
2625
    },
2626
    {
2627
     "name": "stdout",
2628
     "output_type": "stream",
2629
     "text": [
2630
      "g1_frontonzone_GBtowardsback_0006.jpg\n"
2631
     ]
2632
    },
2633
    {
2634
     "name": "stdout",
2635
     "output_type": "stream",
2636
     "text": [
2637
      "g2_midonzone_GBtowardsfront_0005.jpg\n"
2638
     ]
2639
    },
2640
    {
2641
     "name": "stdout",
2642
     "output_type": "stream",
2643
     "text": [
2644
      "g2_midonzone_GBtowardsfront_0034.jpg\n"
2645
     ]
2646
    },
2647
    {
2648
     "name": "stdout",
2649
     "output_type": "stream",
2650
     "text": [
2651
      "g2_midonzone_GBtowardsmmiddle_0003.jpg\n"
2652
     ]
2653
    },
2654
    {
2655
     "name": "stdout",
2656
     "output_type": "stream",
2657
     "text": [
2658
      "g2_midonzone_GBtowardsmmiddle_0005.jpg\n"
2659
     ]
2660
    },
2661
    {
2662
     "name": "stdout",
2663
     "output_type": "stream",
2664
     "text": [
2665
      "grid1_roi1_500kx_0p5nm_haadf1_0001.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0003.jpg\n"
2666
     ]
2667
    },
2668
    {
2669
     "name": "stdout",
2670
     "output_type": "stream",
2671
     "text": [
2672
      "grid1_roi1_500kx_0p5nm_haadf1_0005.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0007.jpg\n"
2673
     ]
2674
    },
2675
    {
2676
     "name": "stdout",
2677
     "output_type": "stream",
2678
     "text": [
2679
      "grid1_roi1_500kx_0p5nm_haadf1_0009.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0011.jpg\n"
2680
     ]
2681
    },
2682
    {
2683
     "name": "stdout",
2684
     "output_type": "stream",
2685
     "text": [
2686
      "grid1_roi1_500kx_0p5nm_haadf1_0013.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0015.jpg\n"
2687
     ]
2688
    },
2689
    {
2690
     "name": "stdout",
2691
     "output_type": "stream",
2692
     "text": [
2693
      "grid1_roi1_500kx_0p5nm_haadf1_0017.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0019.jpg\n"
2694
     ]
2695
    },
2696
    {
2697
     "name": "stdout",
2698
     "output_type": "stream",
2699
     "text": [
2700
      "grid1_roi1_500kx_0p5nm_haadf1_0021.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0023.jpg\n"
2701
     ]
2702
    },
2703
    {
2704
     "name": "stdout",
2705
     "output_type": "stream",
2706
     "text": [
2707
      "grid1_roi1_500kx_0p5nm_haadf1_0027.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0029.jpg\n"
2708
     ]
2709
    },
2710
    {
2711
     "name": "stdout",
2712
     "output_type": "stream",
2713
     "text": [
2714
      "grid1_roi1_500kx_0p5nm_haadf1_0031.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0035.jpg\n"
2715
     ]
2716
    },
2717
    {
2718
     "name": "stdout",
2719
     "output_type": "stream",
2720
     "text": [
2721
      "grid1_roi2_500kx_0p5nm_haadf1_0037.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0039.jpg\n"
2722
     ]
2723
    },
2724
    {
2725
     "name": "stdout",
2726
     "output_type": "stream",
2727
     "text": [
2728
      "grid1_roi2_500kx_0p5nm_haadf1_0041.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0044.jpg\n"
2729
     ]
2730
    },
2731
    {
2732
     "name": "stdout",
2733
     "output_type": "stream",
2734
     "text": [
2735
      "grid1_roi2_500kx_0p5nm_haadf1_0045.jpg\n"
2736
     ]
2737
    },
2738
    {
2739
     "name": "stdout",
2740
     "output_type": "stream",
2741
     "text": [
2742
      "grid1_roi2_500kx_0p5nm_haadf1_0049.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0051.jpg\n"
2743
     ]
2744
    },
2745
    {
2746
     "name": "stdout",
2747
     "output_type": "stream",
2748
     "text": [
2749
      "grid1_roi2_500kx_0p5nm_haadf1_0053.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0055.jpg\n"
2750
     ]
2751
    },
2752
    {
2753
     "name": "stdout",
2754
     "output_type": "stream",
2755
     "text": [
2756
      "grid1_roi2_500kx_0p5nm_haadf1_0057.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0059.jpg\n"
2757
     ]
2758
    },
2759
    {
2760
     "name": "stdout",
2761
     "output_type": "stream",
2762
     "text": [
2763
      "grid1_roi2_500kx_0p5nm_haadf1_0061.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0063.jpg\n"
2764
     ]
2765
    },
2766
    {
2767
     "name": "stdout",
2768
     "output_type": "stream",
2769
     "text": [
2770
      "grid1_roi2_500kx_0p5nm_haadf1_0065.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0067.jpg\n"
2771
     ]
2772
    },
2773
    {
2774
     "name": "stdout",
2775
     "output_type": "stream",
2776
     "text": [
2777
      "grid1_roi2_500kx_0p5nm_haadf1_0069.jpg\nBF X500K, 05 (3).jpg\n"
2778
     ]
2779
    },
2780
    {
2781
     "name": "stdout",
2782
     "output_type": "stream",
2783
     "text": [
2784
      "0501_300kx_1nm_clhaadf3_0038.jpg\n"
2785
     ]
2786
    }
2787
   ],
2788
   "source": [
2789
    "##################################################\n",
2790
    "# 1. Define data augmentation operations\n",
2791
    "##################################################\n",
2792
    "\n",
2793
    "trainImageTxtFile = dataPath + \"trainimages.txt\"\n",
2794
    "imageList = getImageList(trainImageTxtFile)\n",
2795
    "\n",
2796
    "current_operation = \"Brightness\"\n",
2797
    "\n",
2798
    "# Strengthen or weaken the contrast in each image.   \n",
2799
    "from imgaug import augmenters as iaa\n",
2800
    "ia.seed(1)\n",
2801
    "seq = iaa.Sequential([\n",
2802
    "    iaa.Multiply((1.2, 1.5))\n",
2803
    "])\n",
2804
    "\n",
2805
    "# seq = iaa.Sequential([ \n",
2806
    "#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.\n",
2807
    "#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.\n",
2808
    "#     iaa.GammaContrast((0.5, 1.5))\n",
2809
    "# ])\n",
2810
    "\n",
2811
    "# Make our sequence deterministic.\n",
2812
    "# We can now apply it to the image and then to the BBs and it will\n",
2813
    "# lead to the same augmentations.\n",
2814
    "# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!\n",
2815
    "seq_det = seq.to_deterministic()\n",
2816
    "\n",
2817
    "##################################################\n",
2818
    "# 2. loop through images\n",
2819
    "##################################################\n",
2820
    "\n",
2821
    "for img in imageList:\n",
2822
    "    print(img)\n",
2823
    "    # Grayscale images must have shape (height, width, 1) each.\n",
2824
    "    #print(os.listdir(dataPath+'images/'))\n",
2825
    "    currentimage = skimage.io.imread(dataPath+'images/'+img).astype(np.uint8)\n",
2826
    "    # gray2rgb() simply duplicates the gray values over the three color channels.\n",
2827
    "    currentimage = color.gray2rgb(currentimage)\n",
2828
    "    bbs = bboxSetupInImage(dataPath , img.rstrip('.jpg') + '.txt',currentimage)\n",
2829
    "    # Augment BBs and images.\n",
2830
    "    # As we only have one image and list of BBs, we use\n",
2831
    "    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your\n",
2832
    "    # variables would likely already be lists.\n",
2833
    "    image_aug = seq_det.augment_images([currentimage])[0]\n",
2834
    "    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]\n",
2835
    "    augImgFolder = current_operation + \"Images\"\n",
2836
    "    augTxTFolder = current_operation + \"TXT\"\n",
2837
    "    createFolder(augImgFolder)\n",
2838
    "    createFolder(augTxTFolder)\n",
2839
    "    # Save aug images and bboxes\n",
2840
    "    skimage.io.imsave(augImgFolder + '/'+\n",
2841
    "                      img.rstrip('.jpg') + \n",
2842
    "                      '_' + current_operation +\n",
2843
    "                      '.jpg'\n",
2844
    "                      ,image_aug)\n",
2845
    "    saveAugbbox2TXT(augTxTFolder+ '/'+ \n",
2846
    "                    img.rstrip('.jpg') + \n",
2847
    "                    '_'+ current_operation +\n",
2848
    "                    '.txt',bbs_aug)\n",
2849
    "    # image with BBs before/after augmentation (shown below)\n",
2850
    "    # image_before = bbs.draw_on_image(currentimage, thickness=2)\n",
2851
    "    # image_after = bbs_aug.draw_on_image(image_aug, \n",
2852
    "    # thickness=2, color=[0, 0, 255])\n",
2853
    "    # image with BBs before/after augmentation (shown below)\n",
2854
    "    # plot and save figures before and after data augmentations\n",
2855
    "    #skimage.io.imshow(image_before)\n",
2856
    "    #skimage.io.imshow(image_after)\n",
2857
    "    # for i in range(len(bbs.bounding_boxes)):\n",
2858
    "    #     before = bbs.bounding_boxes[i]\n",
2859
    "    #     after = bbs_aug.bounding_boxes[i]\n",
2860
    "    #     print(\"BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)\" % (\n",
2861
    "    #         i,\n",
2862
    "    #         before.x1, before.y1, before.x2, before.y2,\n",
2863
    "    #         after.x1, after.y1, after.x2, after.y2)\n",
2864
    "    #     )"
2865
   ]
2866
  },
2867
  {
2868
   "cell_type": "heading",
2869
   "metadata": {},
2870
   "level": 2,
2871
   "source": [
2872
    "Contrast Normalization"
2873
   ]
2874
  },
2875
  {
2876
   "cell_type": "code",
2877
   "execution_count": 33,
2878
   "metadata": {},
2879
   "outputs": [
2880
    {
2881
     "name": "stdout",
2882
     "output_type": "stream",
2883
     "text": [
2884
      "0501_300kx_1nm_clhaadf3_0006.jpg\n0501_300kx_1nm_clhaadf3_0008.jpg\n"
2885
     ]
2886
    },
2887
    {
2888
     "name": "stdout",
2889
     "output_type": "stream",
2890
     "text": [
2891
      "0501_300kx_1nm_clhaadf3_0012.jpg\n"
2892
     ]
2893
    },
2894
    {
2895
     "name": "stdout",
2896
     "output_type": "stream",
2897
     "text": [
2898
      "0501_300kx_1nm_clhaadf3_0016.jpg\n"
2899
     ]
2900
    },
2901
    {
2902
     "name": "stdout",
2903
     "output_type": "stream",
2904
     "text": [
2905
      "0501_300kx_1nm_clhaadf3_0018.jpg\n"
2906
     ]
2907
    },
2908
    {
2909
     "name": "stdout",
2910
     "output_type": "stream",
2911
     "text": [
2912
      "0501_300kx_1nm_clhaadf3_0020.jpg\n0501_300kx_1nm_clhaadf3_0022.jpg\n"
2913
     ]
2914
    },
2915
    {
2916
     "name": "stdout",
2917
     "output_type": "stream",
2918
     "text": [
2919
      "0501_300kx_1nm_clhaadf3_0024.jpg\n0501_300kx_1nm_clhaadf3_0026.jpg\n"
2920
     ]
2921
    },
2922
    {
2923
     "name": "stdout",
2924
     "output_type": "stream",
2925
     "text": [
2926
      "0501_300kx_1nm_clhaadf3_0030.jpg\n0501_300kx_1nm_clhaadf3_0031.jpg\n"
2927
     ]
2928
    },
2929
    {
2930
     "name": "stdout",
2931
     "output_type": "stream",
2932
     "text": [
2933
      "0501_300kx_1nm_clhaadf3_0034.jpg\n"
2934
     ]
2935
    },
2936
    {
2937
     "name": "stdout",
2938
     "output_type": "stream",
2939
     "text": [
2940
      "0501_300kx_1nm_clhaadf3_0036.jpg\n0501_300kx_1nm_clhaadf3_0040.jpg\n"
2941
     ]
2942
    },
2943
    {
2944
     "name": "stdout",
2945
     "output_type": "stream",
2946
     "text": [
2947
      "0501_300kx_1nm_clhaadf3_0044.jpg\n0501_300kx_1nm_clhaadf3_0046.jpg\n"
2948
     ]
2949
    },
2950
    {
2951
     "name": "stdout",
2952
     "output_type": "stream",
2953
     "text": [
2954
      "0501_300kx_1nm_clhaadf3_0048.jpg\n0501_300kx_1nm_clhaadf3_0050.jpg\n"
2955
     ]
2956
    },
2957
    {
2958
     "name": "stdout",
2959
     "output_type": "stream",
2960
     "text": [
2961
      "200kV_500kx_p2nm_8cmCL_grain1_0002.jpg\n"
2962
     ]
2963
    },
2964
    {
2965
     "name": "stdout",
2966
     "output_type": "stream",
2967
     "text": [
2968
      "200kV_500kx_p2nm_8cmCL_grain1_0002_n.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0004_n.jpg\n"
2969
     ]
2970
    },
2971
    {
2972
     "name": "stdout",
2973
     "output_type": "stream",
2974
     "text": [
2975
      "200kV_500kx_p2nm_8cmCL_grain1_0048 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0050 - Copy.jpg\n"
2976
     ]
2977
    },
2978
    {
2979
     "name": "stdout",
2980
     "output_type": "stream",
2981
     "text": [
2982
      "200kV_500kx_p2nm_8cmCL_grain1_0052 - Copy.jpg\n"
2983
     ]
2984
    },
2985
    {
2986
     "name": "stdout",
2987
     "output_type": "stream",
2988
     "text": [
2989
      "200kV_500kx_p2nm_8cmCL_grain1_0054 - Copy.jpg\n"
2990
     ]
2991
    },
2992
    {
2993
     "name": "stdout",
2994
     "output_type": "stream",
2995
     "text": [
2996
      "200kV_500kx_p2nm_8cmCL_grain1_0058 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0060 - Copy.jpg\n"
2997
     ]
2998
    },
2999
    {
3000
     "name": "stdout",
3001
     "output_type": "stream",
3002
     "text": [
3003
      "200kV_500kx_p2nm_8cmCL_grain1_0062 - Copy.jpg\n"
3004
     ]
3005
    },
3006
    {
3007
     "name": "stdout",
3008
     "output_type": "stream",
3009
     "text": [
3010
      "200kV_500kx_p2nm_8cmCL_grain1_0064 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0066 - Copy.jpg\n"
3011
     ]
3012
    },
3013
    {
3014
     "name": "stdout",
3015
     "output_type": "stream",
3016
     "text": [
3017
      "200kV_500kx_p2nm_8cmCL_grain1_0068 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0070 - Copy.jpg\n"
3018
     ]
3019
    },
3020
    {
3021
     "name": "stdout",
3022
     "output_type": "stream",
3023
     "text": [
3024
      "200kV_500kx_p2nm_8cmCL_grain1_0072 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0074 - Copy.jpg\n"
3025
     ]
3026
    },
3027
    {
3028
     "name": "stdout",
3029
     "output_type": "stream",
3030
     "text": [
3031
      "200kV_500kx_p2nm_8cmCL_grain1_0076 - Copy.jpg\n"
3032
     ]
3033
    },
3034
    {
3035
     "name": "stdout",
3036
     "output_type": "stream",
3037
     "text": [
3038
      "200kV_500kx_p2nm_8cmCL_grain1_0078 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0080 - Copy.jpg\n"
3039
     ]
3040
    },
3041
    {
3042
     "name": "stdout",
3043
     "output_type": "stream",
3044
     "text": [
3045
      "200kV_500kx_p2nm_8cmCL_grain1_0084 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0086 - Copy.jpg\n"
3046
     ]
3047
    },
3048
    {
3049
     "name": "stdout",
3050
     "output_type": "stream",
3051
     "text": [
3052
      "200kV_500kx_p2nm_8cmCL_grain1_0090 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0092 - Copy.jpg\n"
3053
     ]
3054
    },
3055
    {
3056
     "name": "stdout",
3057
     "output_type": "stream",
3058
     "text": [
3059
      "200kV_500kx_p2nm_8cmCL_grain1_0109.jpg\n"
3060
     ]
3061
    },
3062
    {
3063
     "name": "stdout",
3064
     "output_type": "stream",
3065
     "text": [
3066
      "200kV_500kx_p2nm_8cmCL_grain1_0111.jpg\n"
3067
     ]
3068
    },
3069
    {
3070
     "name": "stdout",
3071
     "output_type": "stream",
3072
     "text": [
3073
      "200kV_500kx_p2nm_8cmCL_grain1_0129.jpg\n"
3074
     ]
3075
    },
3076
    {
3077
     "name": "stdout",
3078
     "output_type": "stream",
3079
     "text": [
3080
      "200kV_500kx_p2nm_8cmCL_grain2_0010.jpg\n"
3081
     ]
3082
    },
3083
    {
3084
     "name": "stdout",
3085
     "output_type": "stream",
3086
     "text": [
3087
      "200kV_500kx_p2nm_8cmCL_grain2_0012.jpg\n"
3088
     ]
3089
    },
3090
    {
3091
     "name": "stdout",
3092
     "output_type": "stream",
3093
     "text": [
3094
      "200kV_500kx_p2nm_8cmCL_grain3_4_0258.jpg\n"
3095
     ]
3096
    },
3097
    {
3098
     "name": "stdout",
3099
     "output_type": "stream",
3100
     "text": [
3101
      "2501_300kx_1nm_clhaadf3_0032.jpg\n2501_300kx_1nm_clhaadf3_0034.jpg\n"
3102
     ]
3103
    },
3104
    {
3105
     "name": "stdout",
3106
     "output_type": "stream",
3107
     "text": [
3108
      "2501_300kx_1nm_clhaadf3_0036.jpg\n"
3109
     ]
3110
    },
3111
    {
3112
     "name": "stdout",
3113
     "output_type": "stream",
3114
     "text": [
3115
      "2501_300kx_1nm_clhaadf3_0038.jpg\n2501_300kx_1nm_clhaadf3_0040.jpg\n"
3116
     ]
3117
    },
3118
    {
3119
     "name": "stdout",
3120
     "output_type": "stream",
3121
     "text": [
3122
      "2501_300kx_1nm_clhaadf3_0042.jpg\n2501_300kx_1nm_clhaadf3_0044.jpg\n"
3123
     ]
3124
    },
3125
    {
3126
     "name": "stdout",
3127
     "output_type": "stream",
3128
     "text": [
3129
      "2501_300kx_1nm_clhaadf3_0046.jpg\n2501_300kx_1nm_clhaadf3_0048.jpg\n"
3130
     ]
3131
    },
3132
    {
3133
     "name": "stdout",
3134
     "output_type": "stream",
3135
     "text": [
3136
      "2501_300kx_1nm_clhaadf3_0050.jpg\n2501_300kx_1nm_clhaadf3_0052.jpg\n"
3137
     ]
3138
    },
3139
    {
3140
     "name": "stdout",
3141
     "output_type": "stream",
3142
     "text": [
3143
      "2501_300kx_1nm_clhaadf3_0054.jpg\n"
3144
     ]
3145
    },
3146
    {
3147
     "name": "stdout",
3148
     "output_type": "stream",
3149
     "text": [
3150
      "2ROI_100kx_4100CL_foil1.jpg\n"
3151
     ]
3152
    },
3153
    {
3154
     "name": "stdout",
3155
     "output_type": "stream",
3156
     "text": [
3157
      "3ROI_100kx_4100CL_foil1.jpg\n4ROI_100kx_4100CL_foil1.jpg\n"
3158
     ]
3159
    },
3160
    {
3161
     "name": "stdout",
3162
     "output_type": "stream",
3163
     "text": [
3164
      "5401_300kx_1nm_clhaadf3_0004.jpg\n5401_300kx_1nm_clhaadf3_0006.jpg\n"
3165
     ]
3166
    },
3167
    {
3168
     "name": "stdout",
3169
     "output_type": "stream",
3170
     "text": [
3171
      "5401_300kx_1nm_clhaadf3_0016.jpg\n5401_300kx_1nm_clhaadf3_0018.jpg\n"
3172
     ]
3173
    },
3174
    {
3175
     "name": "stdout",
3176
     "output_type": "stream",
3177
     "text": [
3178
      "5401_300kx_1nm_clhaadf3_0022.jpg\n"
3179
     ]
3180
    },
3181
    {
3182
     "name": "stdout",
3183
     "output_type": "stream",
3184
     "text": [
3185
      "5401_300kx_1nm_clhaadf3_0024.jpg\n5401_300kx_1nm_clhaadf3_0026.jpg\n"
3186
     ]
3187
    },
3188
    {
3189
     "name": "stdout",
3190
     "output_type": "stream",
3191
     "text": [
3192
      "5401_300kx_1nm_clhaadf3_0030.jpg\n"
3193
     ]
3194
    },
3195
    {
3196
     "name": "stdout",
3197
     "output_type": "stream",
3198
     "text": [
3199
      "5ROI_100kx_4100CL_foil1 copy.jpg\n"
3200
     ]
3201
    },
3202
    {
3203
     "name": "stdout",
3204
     "output_type": "stream",
3205
     "text": [
3206
      "6ROI_100kx_4100CL_foil1 copy.jpg\n7ROI_100kx_4100CL_foil1 copy.jpg\n"
3207
     ]
3208
    },
3209
    {
3210
     "name": "stdout",
3211
     "output_type": "stream",
3212
     "text": [
3213
      "9ROI_100kx_4100CL_foil1.jpg\nBF X500K, 01 (2).jpg\n"
3214
     ]
3215
    },
3216
    {
3217
     "name": "stdout",
3218
     "output_type": "stream",
3219
     "text": [
3220
      "BF X500K, 01 (3).jpg\nBF X500K, 02 (2).jpg\n"
3221
     ]
3222
    },
3223
    {
3224
     "name": "stdout",
3225
     "output_type": "stream",
3226
     "text": [
3227
      "BF X500K, 03 (2).jpg\nBF X500K, 04.jpg\n"
3228
     ]
3229
    },
3230
    {
3231
     "name": "stdout",
3232
     "output_type": "stream",
3233
     "text": [
3234
      "BF X500K, 05 (2).jpg\nBF X500K, 05.jpg\n"
3235
     ]
3236
    },
3237
    {
3238
     "name": "stdout",
3239
     "output_type": "stream",
3240
     "text": [
3241
      "BF X500K, 06 (3).jpg\nBF X500K, 06.jpg\n"
3242
     ]
3243
    },
3244
    {
3245
     "name": "stdout",
3246
     "output_type": "stream",
3247
     "text": [
3248
      "BF X500K, 07 (2).jpg\nBF X500K, 08 (2).jpg\n"
3249
     ]
3250
    },
3251
    {
3252
     "name": "stdout",
3253
     "output_type": "stream",
3254
     "text": [
3255
      "BF X500K, 08 (3).jpg\nBF X500K, 08.jpg\n"
3256
     ]
3257
    },
3258
    {
3259
     "name": "stdout",
3260
     "output_type": "stream",
3261
     "text": [
3262
      "BF X500K, 10 (2).jpg\nBF X500K, 10 (3).jpg\n"
3263
     ]
3264
    },
3265
    {
3266
     "name": "stdout",
3267
     "output_type": "stream",
3268
     "text": [
3269
      "BF X500K, 10.jpg\n"
3270
     ]
3271
    },
3272
    {
3273
     "name": "stdout",
3274
     "output_type": "stream",
3275
     "text": [
3276
      "BF X500K, 11 (2).jpg\nBF X500K, 11 (3).jpg\n"
3277
     ]
3278
    },
3279
    {
3280
     "name": "stdout",
3281
     "output_type": "stream",
3282
     "text": [
3283
      "BF X500K, 11.jpg\nBF X500K, 12 (2).jpg\n"
3284
     ]
3285
    },
3286
    {
3287
     "name": "stdout",
3288
     "output_type": "stream",
3289
     "text": [
3290
      "BF X500K, 12 (3).jpg\nBF X500K, 12.jpg\n"
3291
     ]
3292
    },
3293
    {
3294
     "name": "stdout",
3295
     "output_type": "stream",
3296
     "text": [
3297
      "BF X500K, 13 (3).jpg\nBF X500K, 14.jpg\n"
3298
     ]
3299
    },
3300
    {
3301
     "name": "stdout",
3302
     "output_type": "stream",
3303
     "text": [
3304
      "BF X500K, 15.jpg\nBF X500K, 16.jpg\n"
3305
     ]
3306
    },
3307
    {
3308
     "name": "stdout",
3309
     "output_type": "stream",
3310
     "text": [
3311
      "K713_300kx_store4_grid1_0005.jpg\nK713_300kx_store4_grid1_0007.jpg\n"
3312
     ]
3313
    },
3314
    {
3315
     "name": "stdout",
3316
     "output_type": "stream",
3317
     "text": [
3318
      "K713_300kx_store4_grid1_0009.jpg\nK713_300kx_store4_grid1_0011.jpg\n"
3319
     ]
3320
    },
3321
    {
3322
     "name": "stdout",
3323
     "output_type": "stream",
3324
     "text": [
3325
      "K713_300kx_store4_grid1_0013.jpg\nK713_300kx_store4_grid1_0015.jpg\n"
3326
     ]
3327
    },
3328
    {
3329
     "name": "stdout",
3330
     "output_type": "stream",
3331
     "text": [
3332
      "K713_300kx_store4_grid1_0017.jpg\nK713_300kx_store4_grid1_0019.jpg"
3333
     ]
3334
    },
3335
    {
3336
     "name": "stdout",
3337
     "output_type": "stream",
3338
     "text": [
3339
      "\ndalong2.jpg\n"
3340
     ]
3341
    },
3342
    {
3343
     "name": "stdout",
3344
     "output_type": "stream",
3345
     "text": [
3346
      "dalong3.jpg\ng1_backonzone_GBtowardsfrom_0005.jpg\n"
3347
     ]
3348
    },
3349
    {
3350
     "name": "stdout",
3351
     "output_type": "stream",
3352
     "text": [
3353
      "g1_frontonzone_GBtowardsback_0006.jpg\n"
3354
     ]
3355
    },
3356
    {
3357
     "name": "stdout",
3358
     "output_type": "stream",
3359
     "text": [
3360
      "g2_midonzone_GBtowardsfront_0005.jpg\n"
3361
     ]
3362
    },
3363
    {
3364
     "name": "stdout",
3365
     "output_type": "stream",
3366
     "text": [
3367
      "g2_midonzone_GBtowardsfront_0034.jpg\n"
3368
     ]
3369
    },
3370
    {
3371
     "name": "stdout",
3372
     "output_type": "stream",
3373
     "text": [
3374
      "g2_midonzone_GBtowardsmmiddle_0003.jpg\n"
3375
     ]
3376
    },
3377
    {
3378
     "name": "stdout",
3379
     "output_type": "stream",
3380
     "text": [
3381
      "g2_midonzone_GBtowardsmmiddle_0005.jpg\n"
3382
     ]
3383
    },
3384
    {
3385
     "name": "stdout",
3386
     "output_type": "stream",
3387
     "text": [
3388
      "grid1_roi1_500kx_0p5nm_haadf1_0001.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0003.jpg\n"
3389
     ]
3390
    },
3391
    {
3392
     "name": "stdout",
3393
     "output_type": "stream",
3394
     "text": [
3395
      "grid1_roi1_500kx_0p5nm_haadf1_0005.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0007.jpg\n"
3396
     ]
3397
    },
3398
    {
3399
     "name": "stdout",
3400
     "output_type": "stream",
3401
     "text": [
3402
      "grid1_roi1_500kx_0p5nm_haadf1_0009.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0011.jpg\n"
3403
     ]
3404
    },
3405
    {
3406
     "name": "stdout",
3407
     "output_type": "stream",
3408
     "text": [
3409
      "grid1_roi1_500kx_0p5nm_haadf1_0013.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0015.jpg\n"
3410
     ]
3411
    },
3412
    {
3413
     "name": "stdout",
3414
     "output_type": "stream",
3415
     "text": [
3416
      "grid1_roi1_500kx_0p5nm_haadf1_0017.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0019.jpg\n"
3417
     ]
3418
    },
3419
    {
3420
     "name": "stdout",
3421
     "output_type": "stream",
3422
     "text": [
3423
      "grid1_roi1_500kx_0p5nm_haadf1_0021.jpg\n"
3424
     ]
3425
    },
3426
    {
3427
     "name": "stdout",
3428
     "output_type": "stream",
3429
     "text": [
3430
      "grid1_roi1_500kx_0p5nm_haadf1_0023.jpg\n"
3431
     ]
3432
    },
3433
    {
3434
     "name": "stdout",
3435
     "output_type": "stream",
3436
     "text": [
3437
      "grid1_roi1_500kx_0p5nm_haadf1_0027.jpg\n"
3438
     ]
3439
    },
3440
    {
3441
     "name": "stdout",
3442
     "output_type": "stream",
3443
     "text": [
3444
      "grid1_roi1_500kx_0p5nm_haadf1_0029.jpg\n"
3445
     ]
3446
    },
3447
    {
3448
     "name": "stdout",
3449
     "output_type": "stream",
3450
     "text": [
3451
      "grid1_roi1_500kx_0p5nm_haadf1_0031.jpg\n"
3452
     ]
3453
    },
3454
    {
3455
     "name": "stdout",
3456
     "output_type": "stream",
3457
     "text": [
3458
      "grid1_roi2_500kx_0p5nm_haadf1_0035.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0037.jpg\n"
3459
     ]
3460
    },
3461
    {
3462
     "name": "stdout",
3463
     "output_type": "stream",
3464
     "text": [
3465
      "grid1_roi2_500kx_0p5nm_haadf1_0039.jpg\n"
3466
     ]
3467
    },
3468
    {
3469
     "name": "stdout",
3470
     "output_type": "stream",
3471
     "text": [
3472
      "grid1_roi2_500kx_0p5nm_haadf1_0041.jpg\n"
3473
     ]
3474
    },
3475
    {
3476
     "name": "stdout",
3477
     "output_type": "stream",
3478
     "text": [
3479
      "grid1_roi2_500kx_0p5nm_haadf1_0044.jpg\n"
3480
     ]
3481
    },
3482
    {
3483
     "name": "stdout",
3484
     "output_type": "stream",
3485
     "text": [
3486
      "grid1_roi2_500kx_0p5nm_haadf1_0045.jpg\n"
3487
     ]
3488
    },
3489
    {
3490
     "name": "stdout",
3491
     "output_type": "stream",
3492
     "text": [
3493
      "grid1_roi2_500kx_0p5nm_haadf1_0049.jpg\n"
3494
     ]
3495
    },
3496
    {
3497
     "name": "stdout",
3498
     "output_type": "stream",
3499
     "text": [
3500
      "grid1_roi2_500kx_0p5nm_haadf1_0051.jpg\n"
3501
     ]
3502
    },
3503
    {
3504
     "name": "stdout",
3505
     "output_type": "stream",
3506
     "text": [
3507
      "grid1_roi2_500kx_0p5nm_haadf1_0053.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0055.jpg\n"
3508
     ]
3509
    },
3510
    {
3511
     "name": "stdout",
3512
     "output_type": "stream",
3513
     "text": [
3514
      "grid1_roi2_500kx_0p5nm_haadf1_0057.jpg\n"
3515
     ]
3516
    },
3517
    {
3518
     "name": "stdout",
3519
     "output_type": "stream",
3520
     "text": [
3521
      "grid1_roi2_500kx_0p5nm_haadf1_0059.jpg\n"
3522
     ]
3523
    },
3524
    {
3525
     "name": "stdout",
3526
     "output_type": "stream",
3527
     "text": [
3528
      "grid1_roi2_500kx_0p5nm_haadf1_0061.jpg\n"
3529
     ]
3530
    },
3531
    {
3532
     "name": "stdout",
3533
     "output_type": "stream",
3534
     "text": [
3535
      "grid1_roi2_500kx_0p5nm_haadf1_0063.jpg\n"
3536
     ]
3537
    },
3538
    {
3539
     "name": "stdout",
3540
     "output_type": "stream",
3541
     "text": [
3542
      "grid1_roi2_500kx_0p5nm_haadf1_0065.jpg\n"
3543
     ]
3544
    },
3545
    {
3546
     "name": "stdout",
3547
     "output_type": "stream",
3548
     "text": [
3549
      "grid1_roi2_500kx_0p5nm_haadf1_0067.jpg\n"
3550
     ]
3551
    },
3552
    {
3553
     "name": "stdout",
3554
     "output_type": "stream",
3555
     "text": [
3556
      "grid1_roi2_500kx_0p5nm_haadf1_0069.jpg\n"
3557
     ]
3558
    },
3559
    {
3560
     "name": "stdout",
3561
     "output_type": "stream",
3562
     "text": [
3563
      "BF X500K, 05 (3).jpg\n"
3564
     ]
3565
    },
3566
    {
3567
     "name": "stdout",
3568
     "output_type": "stream",
3569
     "text": [
3570
      "0501_300kx_1nm_clhaadf3_0038.jpg\n"
3571
     ]
3572
    }
3573
   ],
3574
   "source": [
3575
    "##################################################\n",
3576
    "# 1. Define data augmentation operations\n",
3577
    "##################################################\n",
3578
    "\n",
3579
    "trainImageTxtFile = dataPath + \"trainimages.txt\"\n",
3580
    "imageList = getImageList(trainImageTxtFile)\n",
3581
    "\n",
3582
    "current_operation = \"ContrastNormalization\"\n",
3583
    "\n",
3584
    "# Improve or worsen the contrast of images.\n",
3585
    "\n",
3586
    "from imgaug import augmenters as iaa\n",
3587
    "ia.seed(1)\n",
3588
    "seq = iaa.Sequential([\n",
3589
    "    iaa.ContrastNormalization((0.8, 1.5), per_channel=True)\n",
3590
    "])\n",
3591
    "\n",
3592
    "# seq = iaa.Sequential([ \n",
3593
    "#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.\n",
3594
    "#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.\n",
3595
    "#     iaa.GammaContrast((0.5, 1.5))\n",
3596
    "# ])\n",
3597
    "\n",
3598
    "# Make our sequence deterministic.\n",
3599
    "# We can now apply it to the image and then to the BBs and it will\n",
3600
    "# lead to the same augmentations.\n",
3601
    "# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!\n",
3602
    "seq_det = seq.to_deterministic()\n",
3603
    "\n",
3604
    "##################################################\n",
3605
    "# 2. loop through images\n",
3606
    "##################################################\n",
3607
    "\n",
3608
    "for img in imageList:\n",
3609
    "    print(img)\n",
3610
    "    # Grayscale images must have shape (height, width, 1) each.\n",
3611
    "    #print(os.listdir(dataPath+'images/'))\n",
3612
    "    currentimage = skimage.io.imread(dataPath+'images/'+img).astype(np.uint8)\n",
3613
    "    # gray2rgb() simply duplicates the gray values over the three color channels.\n",
3614
    "    currentimage = color.gray2rgb(currentimage)\n",
3615
    "    bbs = bboxSetupInImage(dataPath , img.rstrip('.jpg') + '.txt',currentimage)\n",
3616
    "    # Augment BBs and images.\n",
3617
    "    # As we only have one image and list of BBs, we use\n",
3618
    "    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your\n",
3619
    "    # variables would likely already be lists.\n",
3620
    "    image_aug = seq_det.augment_images([currentimage])[0]\n",
3621
    "    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]\n",
3622
    "    augImgFolder = current_operation + \"Images\"\n",
3623
    "    augTxTFolder = current_operation + \"TXT\"\n",
3624
    "    createFolder(augImgFolder)\n",
3625
    "    createFolder(augTxTFolder)\n",
3626
    "    # Save aug images and bboxes\n",
3627
    "    skimage.io.imsave(augImgFolder + '/'+\n",
3628
    "                      img.rstrip('.jpg') + \n",
3629
    "                      '_' + current_operation +\n",
3630
    "                      '.jpg'\n",
3631
    "                      ,image_aug)\n",
3632
    "    saveAugbbox2TXT(augTxTFolder+ '/'+ \n",
3633
    "                    img.rstrip('.jpg') + \n",
3634
    "                    '_'+ current_operation +\n",
3635
    "                    '.txt',bbs_aug)\n",
3636
    "    # image with BBs before/after augmentation (shown below)\n",
3637
    "    # image_before = bbs.draw_on_image(currentimage, thickness=2)\n",
3638
    "    # image_after = bbs_aug.draw_on_image(image_aug, \n",
3639
    "    # thickness=2, color=[0, 0, 255])\n",
3640
    "    # image with BBs before/after augmentation (shown below)\n",
3641
    "    # plot and save figures before and after data augmentations\n",
3642
    "    #skimage.io.imshow(image_before)\n",
3643
    "    #skimage.io.imshow(image_after)\n",
3644
    "    # for i in range(len(bbs.bounding_boxes)):\n",
3645
    "    #     before = bbs.bounding_boxes[i]\n",
3646
    "    #     after = bbs_aug.bounding_boxes[i]\n",
3647
    "    #     print(\"BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)\" % (\n",
3648
    "    #         i,\n",
3649
    "    #         before.x1, before.y1, before.x2, before.y2,\n",
3650
    "    #         after.x1, after.y1, after.x2, after.y2)\n",
3651
    "    #     )"
3652
   ]
3653
  },
3654
  {
3655
   "cell_type": "heading",
3656
   "metadata": {},
3657
   "level": 2,
3658
   "source": [
3659
    "Flip left-right"
3660
   ]
3661
  },
3662
  {
3663
   "cell_type": "code",
3664
   "execution_count": 34,
3665
   "metadata": {},
3666
   "outputs": [
3667
    {
3668
     "name": "stdout",
3669
     "output_type": "stream",
3670
     "text": [
3671
      "0501_300kx_1nm_clhaadf3_0006.jpg\n"
3672
     ]
3673
    },
3674
    {
3675
     "name": "stdout",
3676
     "output_type": "stream",
3677
     "text": [
3678
      "0501_300kx_1nm_clhaadf3_0008.jpg\n"
3679
     ]
3680
    },
3681
    {
3682
     "name": "stdout",
3683
     "output_type": "stream",
3684
     "text": [
3685
      "0501_300kx_1nm_clhaadf3_0012.jpg\n"
3686
     ]
3687
    },
3688
    {
3689
     "name": "stdout",
3690
     "output_type": "stream",
3691
     "text": [
3692
      "0501_300kx_1nm_clhaadf3_0016.jpg\n"
3693
     ]
3694
    },
3695
    {
3696
     "name": "stdout",
3697
     "output_type": "stream",
3698
     "text": [
3699
      "0501_300kx_1nm_clhaadf3_0018.jpg\n"
3700
     ]
3701
    },
3702
    {
3703
     "name": "stdout",
3704
     "output_type": "stream",
3705
     "text": [
3706
      "0501_300kx_1nm_clhaadf3_0020.jpg\n"
3707
     ]
3708
    },
3709
    {
3710
     "name": "stdout",
3711
     "output_type": "stream",
3712
     "text": [
3713
      "0501_300kx_1nm_clhaadf3_0022.jpg\n"
3714
     ]
3715
    },
3716
    {
3717
     "name": "stdout",
3718
     "output_type": "stream",
3719
     "text": [
3720
      "0501_300kx_1nm_clhaadf3_0024.jpg\n"
3721
     ]
3722
    },
3723
    {
3724
     "name": "stdout",
3725
     "output_type": "stream",
3726
     "text": [
3727
      "0501_300kx_1nm_clhaadf3_0026.jpg\n"
3728
     ]
3729
    },
3730
    {
3731
     "name": "stdout",
3732
     "output_type": "stream",
3733
     "text": [
3734
      "0501_300kx_1nm_clhaadf3_0030.jpg\n0501_300kx_1nm_clhaadf3_0031.jpg\n"
3735
     ]
3736
    },
3737
    {
3738
     "name": "stdout",
3739
     "output_type": "stream",
3740
     "text": [
3741
      "0501_300kx_1nm_clhaadf3_0034.jpg\n0501_300kx_1nm_clhaadf3_0036.jpg\n"
3742
     ]
3743
    },
3744
    {
3745
     "name": "stdout",
3746
     "output_type": "stream",
3747
     "text": [
3748
      "0501_300kx_1nm_clhaadf3_0040.jpg\n"
3749
     ]
3750
    },
3751
    {
3752
     "name": "stdout",
3753
     "output_type": "stream",
3754
     "text": [
3755
      "0501_300kx_1nm_clhaadf3_0044.jpg\n"
3756
     ]
3757
    },
3758
    {
3759
     "name": "stdout",
3760
     "output_type": "stream",
3761
     "text": [
3762
      "0501_300kx_1nm_clhaadf3_0046.jpg\n0501_300kx_1nm_clhaadf3_0048.jpg\n"
3763
     ]
3764
    },
3765
    {
3766
     "name": "stdout",
3767
     "output_type": "stream",
3768
     "text": [
3769
      "0501_300kx_1nm_clhaadf3_0050.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0002.jpg\n"
3770
     ]
3771
    },
3772
    {
3773
     "name": "stdout",
3774
     "output_type": "stream",
3775
     "text": [
3776
      "200kV_500kx_p2nm_8cmCL_grain1_0002_n.jpg\n"
3777
     ]
3778
    },
3779
    {
3780
     "name": "stdout",
3781
     "output_type": "stream",
3782
     "text": [
3783
      "200kV_500kx_p2nm_8cmCL_grain1_0004_n.jpg\n"
3784
     ]
3785
    },
3786
    {
3787
     "name": "stdout",
3788
     "output_type": "stream",
3789
     "text": [
3790
      "200kV_500kx_p2nm_8cmCL_grain1_0048 - Copy.jpg\n"
3791
     ]
3792
    },
3793
    {
3794
     "name": "stdout",
3795
     "output_type": "stream",
3796
     "text": [
3797
      "200kV_500kx_p2nm_8cmCL_grain1_0050 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0052 - Copy.jpg\n"
3798
     ]
3799
    },
3800
    {
3801
     "name": "stdout",
3802
     "output_type": "stream",
3803
     "text": [
3804
      "200kV_500kx_p2nm_8cmCL_grain1_0054 - Copy.jpg\n"
3805
     ]
3806
    },
3807
    {
3808
     "name": "stdout",
3809
     "output_type": "stream",
3810
     "text": [
3811
      "200kV_500kx_p2nm_8cmCL_grain1_0058 - Copy.jpg\n"
3812
     ]
3813
    },
3814
    {
3815
     "name": "stdout",
3816
     "output_type": "stream",
3817
     "text": [
3818
      "200kV_500kx_p2nm_8cmCL_grain1_0060 - Copy.jpg\n"
3819
     ]
3820
    },
3821
    {
3822
     "name": "stdout",
3823
     "output_type": "stream",
3824
     "text": [
3825
      "200kV_500kx_p2nm_8cmCL_grain1_0062 - Copy.jpg\n"
3826
     ]
3827
    },
3828
    {
3829
     "name": "stdout",
3830
     "output_type": "stream",
3831
     "text": [
3832
      "200kV_500kx_p2nm_8cmCL_grain1_0064 - Copy.jpg\n"
3833
     ]
3834
    },
3835
    {
3836
     "name": "stdout",
3837
     "output_type": "stream",
3838
     "text": [
3839
      "200kV_500kx_p2nm_8cmCL_grain1_0066 - Copy.jpg\n"
3840
     ]
3841
    },
3842
    {
3843
     "name": "stdout",
3844
     "output_type": "stream",
3845
     "text": [
3846
      "200kV_500kx_p2nm_8cmCL_grain1_0068 - Copy.jpg\n"
3847
     ]
3848
    },
3849
    {
3850
     "name": "stdout",
3851
     "output_type": "stream",
3852
     "text": [
3853
      "200kV_500kx_p2nm_8cmCL_grain1_0070 - Copy.jpg\n"
3854
     ]
3855
    },
3856
    {
3857
     "name": "stdout",
3858
     "output_type": "stream",
3859
     "text": [
3860
      "200kV_500kx_p2nm_8cmCL_grain1_0072 - Copy.jpg\n"
3861
     ]
3862
    },
3863
    {
3864
     "name": "stdout",
3865
     "output_type": "stream",
3866
     "text": [
3867
      "200kV_500kx_p2nm_8cmCL_grain1_0074 - Copy.jpg\n"
3868
     ]
3869
    },
3870
    {
3871
     "name": "stdout",
3872
     "output_type": "stream",
3873
     "text": [
3874
      "200kV_500kx_p2nm_8cmCL_grain1_0076 - Copy.jpg\n"
3875
     ]
3876
    },
3877
    {
3878
     "name": "stdout",
3879
     "output_type": "stream",
3880
     "text": [
3881
      "200kV_500kx_p2nm_8cmCL_grain1_0078 - Copy.jpg\n"
3882
     ]
3883
    },
3884
    {
3885
     "name": "stdout",
3886
     "output_type": "stream",
3887
     "text": [
3888
      "200kV_500kx_p2nm_8cmCL_grain1_0080 - Copy.jpg\n"
3889
     ]
3890
    },
3891
    {
3892
     "name": "stdout",
3893
     "output_type": "stream",
3894
     "text": [
3895
      "200kV_500kx_p2nm_8cmCL_grain1_0084 - Copy.jpg\n"
3896
     ]
3897
    },
3898
    {
3899
     "name": "stdout",
3900
     "output_type": "stream",
3901
     "text": [
3902
      "200kV_500kx_p2nm_8cmCL_grain1_0086 - Copy.jpg\n"
3903
     ]
3904
    },
3905
    {
3906
     "name": "stdout",
3907
     "output_type": "stream",
3908
     "text": [
3909
      "200kV_500kx_p2nm_8cmCL_grain1_0090 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0092 - Copy.jpg\n"
3910
     ]
3911
    },
3912
    {
3913
     "name": "stdout",
3914
     "output_type": "stream",
3915
     "text": [
3916
      "200kV_500kx_p2nm_8cmCL_grain1_0109.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0111.jpg\n"
3917
     ]
3918
    },
3919
    {
3920
     "name": "stdout",
3921
     "output_type": "stream",
3922
     "text": [
3923
      "200kV_500kx_p2nm_8cmCL_grain1_0129.jpg\n"
3924
     ]
3925
    },
3926
    {
3927
     "name": "stdout",
3928
     "output_type": "stream",
3929
     "text": [
3930
      "200kV_500kx_p2nm_8cmCL_grain2_0010.jpg\n"
3931
     ]
3932
    },
3933
    {
3934
     "name": "stdout",
3935
     "output_type": "stream",
3936
     "text": [
3937
      "200kV_500kx_p2nm_8cmCL_grain2_0012.jpg\n"
3938
     ]
3939
    },
3940
    {
3941
     "name": "stdout",
3942
     "output_type": "stream",
3943
     "text": [
3944
      "200kV_500kx_p2nm_8cmCL_grain3_4_0258.jpg\n"
3945
     ]
3946
    },
3947
    {
3948
     "name": "stdout",
3949
     "output_type": "stream",
3950
     "text": [
3951
      "2501_300kx_1nm_clhaadf3_0032.jpg\n"
3952
     ]
3953
    },
3954
    {
3955
     "name": "stdout",
3956
     "output_type": "stream",
3957
     "text": [
3958
      "2501_300kx_1nm_clhaadf3_0034.jpg\n"
3959
     ]
3960
    },
3961
    {
3962
     "name": "stdout",
3963
     "output_type": "stream",
3964
     "text": [
3965
      "2501_300kx_1nm_clhaadf3_0036.jpg\n"
3966
     ]
3967
    },
3968
    {
3969
     "name": "stdout",
3970
     "output_type": "stream",
3971
     "text": [
3972
      "2501_300kx_1nm_clhaadf3_0038.jpg\n"
3973
     ]
3974
    },
3975
    {
3976
     "name": "stdout",
3977
     "output_type": "stream",
3978
     "text": [
3979
      "2501_300kx_1nm_clhaadf3_0040.jpg\n2501_300kx_1nm_clhaadf3_0042.jpg\n"
3980
     ]
3981
    },
3982
    {
3983
     "name": "stdout",
3984
     "output_type": "stream",
3985
     "text": [
3986
      "2501_300kx_1nm_clhaadf3_0044.jpg\n"
3987
     ]
3988
    },
3989
    {
3990
     "name": "stdout",
3991
     "output_type": "stream",
3992
     "text": [
3993
      "2501_300kx_1nm_clhaadf3_0046.jpg\n"
3994
     ]
3995
    },
3996
    {
3997
     "name": "stdout",
3998
     "output_type": "stream",
3999
     "text": [
4000
      "2501_300kx_1nm_clhaadf3_0048.jpg\n"
4001
     ]
4002
    },
4003
    {
4004
     "name": "stdout",
4005
     "output_type": "stream",
4006
     "text": [
4007
      "2501_300kx_1nm_clhaadf3_0050.jpg\n"
4008
     ]
4009
    },
4010
    {
4011
     "name": "stdout",
4012
     "output_type": "stream",
4013
     "text": [
4014
      "2501_300kx_1nm_clhaadf3_0052.jpg\n"
4015
     ]
4016
    },
4017
    {
4018
     "name": "stdout",
4019
     "output_type": "stream",
4020
     "text": [
4021
      "2501_300kx_1nm_clhaadf3_0054.jpg\n2ROI_100kx_4100CL_foil1.jpg\n"
4022
     ]
4023
    },
4024
    {
4025
     "name": "stdout",
4026
     "output_type": "stream",
4027
     "text": [
4028
      "3ROI_100kx_4100CL_foil1.jpg\n"
4029
     ]
4030
    },
4031
    {
4032
     "name": "stdout",
4033
     "output_type": "stream",
4034
     "text": [
4035
      "4ROI_100kx_4100CL_foil1.jpg\n"
4036
     ]
4037
    },
4038
    {
4039
     "name": "stdout",
4040
     "output_type": "stream",
4041
     "text": [
4042
      "5401_300kx_1nm_clhaadf3_0004.jpg\n"
4043
     ]
4044
    },
4045
    {
4046
     "name": "stdout",
4047
     "output_type": "stream",
4048
     "text": [
4049
      "5401_300kx_1nm_clhaadf3_0006.jpg\n"
4050
     ]
4051
    },
4052
    {
4053
     "name": "stdout",
4054
     "output_type": "stream",
4055
     "text": [
4056
      "5401_300kx_1nm_clhaadf3_0016.jpg\n5401_300kx_1nm_clhaadf3_0018.jpg\n"
4057
     ]
4058
    },
4059
    {
4060
     "name": "stdout",
4061
     "output_type": "stream",
4062
     "text": [
4063
      "5401_300kx_1nm_clhaadf3_0022.jpg\n5401_300kx_1nm_clhaadf3_0024.jpg\n"
4064
     ]
4065
    },
4066
    {
4067
     "name": "stdout",
4068
     "output_type": "stream",
4069
     "text": [
4070
      "5401_300kx_1nm_clhaadf3_0026.jpg\n5401_300kx_1nm_clhaadf3_0030.jpg\n"
4071
     ]
4072
    },
4073
    {
4074
     "name": "stdout",
4075
     "output_type": "stream",
4076
     "text": [
4077
      "5ROI_100kx_4100CL_foil1 copy.jpg\n"
4078
     ]
4079
    },
4080
    {
4081
     "name": "stdout",
4082
     "output_type": "stream",
4083
     "text": [
4084
      "6ROI_100kx_4100CL_foil1 copy.jpg\n7ROI_100kx_4100CL_foil1 copy.jpg\n"
4085
     ]
4086
    },
4087
    {
4088
     "name": "stdout",
4089
     "output_type": "stream",
4090
     "text": [
4091
      "9ROI_100kx_4100CL_foil1.jpg\nBF X500K, 01 (2).jpg\n"
4092
     ]
4093
    },
4094
    {
4095
     "name": "stdout",
4096
     "output_type": "stream",
4097
     "text": [
4098
      "BF X500K, 01 (3).jpg\n"
4099
     ]
4100
    },
4101
    {
4102
     "name": "stdout",
4103
     "output_type": "stream",
4104
     "text": [
4105
      "BF X500K, 02 (2).jpg\n"
4106
     ]
4107
    },
4108
    {
4109
     "name": "stdout",
4110
     "output_type": "stream",
4111
     "text": [
4112
      "BF X500K, 03 (2).jpg\nBF X500K, 04.jpg\n"
4113
     ]
4114
    },
4115
    {
4116
     "name": "stdout",
4117
     "output_type": "stream",
4118
     "text": [
4119
      "BF X500K, 05 (2).jpg\nBF X500K, 05.jpg\n"
4120
     ]
4121
    },
4122
    {
4123
     "name": "stdout",
4124
     "output_type": "stream",
4125
     "text": [
4126
      "BF X500K, 06 (3).jpg\nBF X500K, 06.jpg\n"
4127
     ]
4128
    },
4129
    {
4130
     "name": "stdout",
4131
     "output_type": "stream",
4132
     "text": [
4133
      "BF X500K, 07 (2).jpg\nBF X500K, 08 (2).jpg\n"
4134
     ]
4135
    },
4136
    {
4137
     "name": "stdout",
4138
     "output_type": "stream",
4139
     "text": [
4140
      "BF X500K, 08 (3).jpg\n"
4141
     ]
4142
    },
4143
    {
4144
     "name": "stdout",
4145
     "output_type": "stream",
4146
     "text": [
4147
      "BF X500K, 08.jpg\n"
4148
     ]
4149
    },
4150
    {
4151
     "name": "stdout",
4152
     "output_type": "stream",
4153
     "text": [
4154
      "BF X500K, 10 (2).jpg\n"
4155
     ]
4156
    },
4157
    {
4158
     "name": "stdout",
4159
     "output_type": "stream",
4160
     "text": [
4161
      "BF X500K, 10 (3).jpg\n"
4162
     ]
4163
    },
4164
    {
4165
     "name": "stdout",
4166
     "output_type": "stream",
4167
     "text": [
4168
      "BF X500K, 10.jpg\n"
4169
     ]
4170
    },
4171
    {
4172
     "name": "stdout",
4173
     "output_type": "stream",
4174
     "text": [
4175
      "BF X500K, 11 (2).jpg\n"
4176
     ]
4177
    },
4178
    {
4179
     "name": "stdout",
4180
     "output_type": "stream",
4181
     "text": [
4182
      "BF X500K, 11 (3).jpg\n"
4183
     ]
4184
    },
4185
    {
4186
     "name": "stdout",
4187
     "output_type": "stream",
4188
     "text": [
4189
      "BF X500K, 11.jpg\n"
4190
     ]
4191
    },
4192
    {
4193
     "name": "stdout",
4194
     "output_type": "stream",
4195
     "text": [
4196
      "BF X500K, 12 (2).jpg\n"
4197
     ]
4198
    },
4199
    {
4200
     "name": "stdout",
4201
     "output_type": "stream",
4202
     "text": [
4203
      "BF X500K, 12 (3).jpg\n"
4204
     ]
4205
    },
4206
    {
4207
     "name": "stdout",
4208
     "output_type": "stream",
4209
     "text": [
4210
      "BF X500K, 12.jpg\n"
4211
     ]
4212
    },
4213
    {
4214
     "name": "stdout",
4215
     "output_type": "stream",
4216
     "text": [
4217
      "BF X500K, 13 (3).jpg\n"
4218
     ]
4219
    },
4220
    {
4221
     "name": "stdout",
4222
     "output_type": "stream",
4223
     "text": [
4224
      "BF X500K, 14.jpg\n"
4225
     ]
4226
    },
4227
    {
4228
     "name": "stdout",
4229
     "output_type": "stream",
4230
     "text": [
4231
      "BF X500K, 15.jpg\n"
4232
     ]
4233
    },
4234
    {
4235
     "name": "stdout",
4236
     "output_type": "stream",
4237
     "text": [
4238
      "BF X500K, 16.jpg\n"
4239
     ]
4240
    },
4241
    {
4242
     "name": "stdout",
4243
     "output_type": "stream",
4244
     "text": [
4245
      "K713_300kx_store4_grid1_0005.jpg\n"
4246
     ]
4247
    },
4248
    {
4249
     "name": "stdout",
4250
     "output_type": "stream",
4251
     "text": [
4252
      "K713_300kx_store4_grid1_0007.jpg\n"
4253
     ]
4254
    },
4255
    {
4256
     "name": "stdout",
4257
     "output_type": "stream",
4258
     "text": [
4259
      "K713_300kx_store4_grid1_0009.jpg\n"
4260
     ]
4261
    },
4262
    {
4263
     "name": "stdout",
4264
     "output_type": "stream",
4265
     "text": [
4266
      "K713_300kx_store4_grid1_0011.jpg\n"
4267
     ]
4268
    },
4269
    {
4270
     "name": "stdout",
4271
     "output_type": "stream",
4272
     "text": [
4273
      "K713_300kx_store4_grid1_0013.jpg\n"
4274
     ]
4275
    },
4276
    {
4277
     "name": "stdout",
4278
     "output_type": "stream",
4279
     "text": [
4280
      "K713_300kx_store4_grid1_0015.jpg\n"
4281
     ]
4282
    },
4283
    {
4284
     "name": "stdout",
4285
     "output_type": "stream",
4286
     "text": [
4287
      "K713_300kx_store4_grid1_0017.jpg\n"
4288
     ]
4289
    },
4290
    {
4291
     "name": "stdout",
4292
     "output_type": "stream",
4293
     "text": [
4294
      "K713_300kx_store4_grid1_0019.jpg\n"
4295
     ]
4296
    },
4297
    {
4298
     "name": "stdout",
4299
     "output_type": "stream",
4300
     "text": [
4301
      "dalong2.jpg\ndalong3.jpg\n"
4302
     ]
4303
    },
4304
    {
4305
     "name": "stdout",
4306
     "output_type": "stream",
4307
     "text": [
4308
      "g1_backonzone_GBtowardsfrom_0005.jpg\n"
4309
     ]
4310
    },
4311
    {
4312
     "name": "stdout",
4313
     "output_type": "stream",
4314
     "text": [
4315
      "g1_frontonzone_GBtowardsback_0006.jpg\n"
4316
     ]
4317
    },
4318
    {
4319
     "name": "stdout",
4320
     "output_type": "stream",
4321
     "text": [
4322
      "g2_midonzone_GBtowardsfront_0005.jpg\n"
4323
     ]
4324
    },
4325
    {
4326
     "name": "stdout",
4327
     "output_type": "stream",
4328
     "text": [
4329
      "g2_midonzone_GBtowardsfront_0034.jpg\n"
4330
     ]
4331
    },
4332
    {
4333
     "name": "stdout",
4334
     "output_type": "stream",
4335
     "text": [
4336
      "g2_midonzone_GBtowardsmmiddle_0003.jpg\n"
4337
     ]
4338
    },
4339
    {
4340
     "name": "stdout",
4341
     "output_type": "stream",
4342
     "text": [
4343
      "g2_midonzone_GBtowardsmmiddle_0005.jpg\n"
4344
     ]
4345
    },
4346
    {
4347
     "name": "stdout",
4348
     "output_type": "stream",
4349
     "text": [
4350
      "grid1_roi1_500kx_0p5nm_haadf1_0001.jpg\n"
4351
     ]
4352
    },
4353
    {
4354
     "name": "stdout",
4355
     "output_type": "stream",
4356
     "text": [
4357
      "grid1_roi1_500kx_0p5nm_haadf1_0003.jpg\n"
4358
     ]
4359
    },
4360
    {
4361
     "name": "stdout",
4362
     "output_type": "stream",
4363
     "text": [
4364
      "grid1_roi1_500kx_0p5nm_haadf1_0005.jpg\n"
4365
     ]
4366
    },
4367
    {
4368
     "name": "stdout",
4369
     "output_type": "stream",
4370
     "text": [
4371
      "grid1_roi1_500kx_0p5nm_haadf1_0007.jpg\n"
4372
     ]
4373
    },
4374
    {
4375
     "name": "stdout",
4376
     "output_type": "stream",
4377
     "text": [
4378
      "grid1_roi1_500kx_0p5nm_haadf1_0009.jpg\n"
4379
     ]
4380
    },
4381
    {
4382
     "name": "stdout",
4383
     "output_type": "stream",
4384
     "text": [
4385
      "grid1_roi1_500kx_0p5nm_haadf1_0011.jpg\n"
4386
     ]
4387
    },
4388
    {
4389
     "name": "stdout",
4390
     "output_type": "stream",
4391
     "text": [
4392
      "grid1_roi1_500kx_0p5nm_haadf1_0013.jpg\n"
4393
     ]
4394
    },
4395
    {
4396
     "name": "stdout",
4397
     "output_type": "stream",
4398
     "text": [
4399
      "grid1_roi1_500kx_0p5nm_haadf1_0015.jpg\n"
4400
     ]
4401
    },
4402
    {
4403
     "name": "stdout",
4404
     "output_type": "stream",
4405
     "text": [
4406
      "grid1_roi1_500kx_0p5nm_haadf1_0017.jpg\n"
4407
     ]
4408
    },
4409
    {
4410
     "name": "stdout",
4411
     "output_type": "stream",
4412
     "text": [
4413
      "grid1_roi1_500kx_0p5nm_haadf1_0019.jpg\n"
4414
     ]
4415
    },
4416
    {
4417
     "name": "stdout",
4418
     "output_type": "stream",
4419
     "text": [
4420
      "grid1_roi1_500kx_0p5nm_haadf1_0021.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0023.jpg\n"
4421
     ]
4422
    },
4423
    {
4424
     "name": "stdout",
4425
     "output_type": "stream",
4426
     "text": [
4427
      "grid1_roi1_500kx_0p5nm_haadf1_0027.jpg\n"
4428
     ]
4429
    },
4430
    {
4431
     "name": "stdout",
4432
     "output_type": "stream",
4433
     "text": [
4434
      "grid1_roi1_500kx_0p5nm_haadf1_0029.jpg\n"
4435
     ]
4436
    },
4437
    {
4438
     "name": "stdout",
4439
     "output_type": "stream",
4440
     "text": [
4441
      "grid1_roi1_500kx_0p5nm_haadf1_0031.jpg\n"
4442
     ]
4443
    },
4444
    {
4445
     "name": "stdout",
4446
     "output_type": "stream",
4447
     "text": [
4448
      "grid1_roi2_500kx_0p5nm_haadf1_0035.jpg\n"
4449
     ]
4450
    },
4451
    {
4452
     "name": "stdout",
4453
     "output_type": "stream",
4454
     "text": [
4455
      "grid1_roi2_500kx_0p5nm_haadf1_0037.jpg\n"
4456
     ]
4457
    },
4458
    {
4459
     "name": "stdout",
4460
     "output_type": "stream",
4461
     "text": [
4462
      "grid1_roi2_500kx_0p5nm_haadf1_0039.jpg\n"
4463
     ]
4464
    },
4465
    {
4466
     "name": "stdout",
4467
     "output_type": "stream",
4468
     "text": [
4469
      "grid1_roi2_500kx_0p5nm_haadf1_0041.jpg\n"
4470
     ]
4471
    },
4472
    {
4473
     "name": "stdout",
4474
     "output_type": "stream",
4475
     "text": [
4476
      "grid1_roi2_500kx_0p5nm_haadf1_0044.jpg\n"
4477
     ]
4478
    },
4479
    {
4480
     "name": "stdout",
4481
     "output_type": "stream",
4482
     "text": [
4483
      "grid1_roi2_500kx_0p5nm_haadf1_0045.jpg\n"
4484
     ]
4485
    },
4486
    {
4487
     "name": "stdout",
4488
     "output_type": "stream",
4489
     "text": [
4490
      "grid1_roi2_500kx_0p5nm_haadf1_0049.jpg\n"
4491
     ]
4492
    },
4493
    {
4494
     "name": "stdout",
4495
     "output_type": "stream",
4496
     "text": [
4497
      "grid1_roi2_500kx_0p5nm_haadf1_0051.jpg\n"
4498
     ]
4499
    },
4500
    {
4501
     "name": "stdout",
4502
     "output_type": "stream",
4503
     "text": [
4504
      "grid1_roi2_500kx_0p5nm_haadf1_0053.jpg\n"
4505
     ]
4506
    },
4507
    {
4508
     "name": "stdout",
4509
     "output_type": "stream",
4510
     "text": [
4511
      "grid1_roi2_500kx_0p5nm_haadf1_0055.jpg\n"
4512
     ]
4513
    },
4514
    {
4515
     "name": "stdout",
4516
     "output_type": "stream",
4517
     "text": [
4518
      "grid1_roi2_500kx_0p5nm_haadf1_0057.jpg\n"
4519
     ]
4520
    },
4521
    {
4522
     "name": "stdout",
4523
     "output_type": "stream",
4524
     "text": [
4525
      "grid1_roi2_500kx_0p5nm_haadf1_0059.jpg\n"
4526
     ]
4527
    },
4528
    {
4529
     "name": "stdout",
4530
     "output_type": "stream",
4531
     "text": [
4532
      "grid1_roi2_500kx_0p5nm_haadf1_0061.jpg\n"
4533
     ]
4534
    },
4535
    {
4536
     "name": "stdout",
4537
     "output_type": "stream",
4538
     "text": [
4539
      "grid1_roi2_500kx_0p5nm_haadf1_0063.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0065.jpg\n"
4540
     ]
4541
    },
4542
    {
4543
     "name": "stdout",
4544
     "output_type": "stream",
4545
     "text": [
4546
      "grid1_roi2_500kx_0p5nm_haadf1_0067.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0069.jpg\n"
4547
     ]
4548
    },
4549
    {
4550
     "name": "stdout",
4551
     "output_type": "stream",
4552
     "text": [
4553
      "BF X500K, 05 (3).jpg\n"
4554
     ]
4555
    },
4556
    {
4557
     "name": "stdout",
4558
     "output_type": "stream",
4559
     "text": [
4560
      "0501_300kx_1nm_clhaadf3_0038.jpg\n"
4561
     ]
4562
    }
4563
   ],
4564
   "source": [
4565
    "##################################################\n",
4566
    "# 1. Define data augmentation operations\n",
4567
    "##################################################\n",
4568
    "\n",
4569
    "trainImageTxtFile = dataPath + \"trainimages.txt\"\n",
4570
    "imageList = getImageList(trainImageTxtFile)\n",
4571
    "\n",
4572
    "current_operation = \"Fliplr\"\n",
4573
    "\n",
4574
    "# Flip/mirror input images horizontally.\n",
4575
    "   \n",
4576
    "from imgaug import augmenters as iaa\n",
4577
    "ia.seed(1)\n",
4578
    "seq = iaa.Sequential([\n",
4579
    "        iaa.Fliplr(1.0)\n",
4580
    "])\n",
4581
    "\n",
4582
    "# seq = iaa.Sequential([ \n",
4583
    "#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.\n",
4584
    "#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.\n",
4585
    "#     iaa.GammaContrast((0.5, 1.5))\n",
4586
    "# ])\n",
4587
    "\n",
4588
    "# Make our sequence deterministic.\n",
4589
    "# We can now apply it to the image and then to the BBs and it will\n",
4590
    "# lead to the same augmentations.\n",
4591
    "# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!\n",
4592
    "seq_det = seq.to_deterministic()\n",
4593
    "\n",
4594
    "##################################################\n",
4595
    "# 2. loop through images\n",
4596
    "##################################################\n",
4597
    "\n",
4598
    "for img in imageList:\n",
4599
    "    print(img)\n",
4600
    "    # Grayscale images must have shape (height, width, 1) each.\n",
4601
    "    #print(os.listdir(dataPath+'images/'))\n",
4602
    "    currentimage = skimage.io.imread(dataPath+'images/'+img).astype(np.uint8)\n",
4603
    "    # gray2rgb() simply duplicates the gray values over the three color channels.\n",
4604
    "    currentimage = color.gray2rgb(currentimage)\n",
4605
    "    bbs = bboxSetupInImage(dataPath , img.rstrip('.jpg') + '.txt',currentimage)\n",
4606
    "    # Augment BBs and images.\n",
4607
    "    # As we only have one image and list of BBs, we use\n",
4608
    "    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your\n",
4609
    "    # variables would likely already be lists.\n",
4610
    "    image_aug = seq_det.augment_images([currentimage])[0]\n",
4611
    "    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]\n",
4612
    "    augImgFolder = current_operation + \"Images\"\n",
4613
    "    augTxTFolder = current_operation + \"TXT\"\n",
4614
    "    createFolder(augImgFolder)\n",
4615
    "    createFolder(augTxTFolder)\n",
4616
    "    # Save aug images and bboxes\n",
4617
    "    skimage.io.imsave(augImgFolder + '/'+\n",
4618
    "                      img.rstrip('.jpg') + \n",
4619
    "                      '_' + current_operation +\n",
4620
    "                      '.jpg'\n",
4621
    "                      ,image_aug)\n",
4622
    "    saveAugbbox2TXT(augTxTFolder+ '/'+ \n",
4623
    "                    img.rstrip('.jpg') + \n",
4624
    "                    '_'+ current_operation +\n",
4625
    "                    '.txt',bbs_aug)\n",
4626
    "    # image with BBs before/after augmentation (shown below)\n",
4627
    "    # image_before = bbs.draw_on_image(currentimage, thickness=2)\n",
4628
    "    # image_after = bbs_aug.draw_on_image(image_aug, \n",
4629
    "    # thickness=2, color=[0, 0, 255])\n",
4630
    "    # image with BBs before/after augmentation (shown below)\n",
4631
    "    # plot and save figures before and after data augmentations\n",
4632
    "    #skimage.io.imshow(image_before)\n",
4633
    "    #skimage.io.imshow(image_after)\n",
4634
    "    # for i in range(len(bbs.bounding_boxes)):\n",
4635
    "    #     before = bbs.bounding_boxes[i]\n",
4636
    "    #     after = bbs_aug.bounding_boxes[i]\n",
4637
    "    #     print(\"BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)\" % (\n",
4638
    "    #         i,\n",
4639
    "    #         before.x1, before.y1, before.x2, before.y2,\n",
4640
    "    #         after.x1, after.y1, after.x2, after.y2)\n",
4641
    "    #     )"
4642
   ]
4643
  },
4644
  {
4645
   "cell_type": "heading",
4646
   "metadata": {},
4647
   "level": 2,
4648
   "source": [
4649
    "Flip up-down"
4650
   ]
4651
  },
4652
  {
4653
   "cell_type": "code",
4654
   "execution_count": 35,
4655
   "metadata": {},
4656
   "outputs": [
4657
    {
4658
     "name": "stdout",
4659
     "output_type": "stream",
4660
     "text": [
4661
      "0501_300kx_1nm_clhaadf3_0006.jpg\n0501_300kx_1nm_clhaadf3_0008.jpg\n"
4662
     ]
4663
    },
4664
    {
4665
     "name": "stdout",
4666
     "output_type": "stream",
4667
     "text": [
4668
      "0501_300kx_1nm_clhaadf3_0012.jpg\n"
4669
     ]
4670
    },
4671
    {
4672
     "name": "stdout",
4673
     "output_type": "stream",
4674
     "text": [
4675
      "0501_300kx_1nm_clhaadf3_0016.jpg\n"
4676
     ]
4677
    },
4678
    {
4679
     "name": "stdout",
4680
     "output_type": "stream",
4681
     "text": [
4682
      "0501_300kx_1nm_clhaadf3_0018.jpg\n0501_300kx_1nm_clhaadf3_0020.jpg"
4683
     ]
4684
    },
4685
    {
4686
     "name": "stdout",
4687
     "output_type": "stream",
4688
     "text": [
4689
      "\n"
4690
     ]
4691
    },
4692
    {
4693
     "name": "stdout",
4694
     "output_type": "stream",
4695
     "text": [
4696
      "0501_300kx_1nm_clhaadf3_0022.jpg\n"
4697
     ]
4698
    },
4699
    {
4700
     "name": "stdout",
4701
     "output_type": "stream",
4702
     "text": [
4703
      "0501_300kx_1nm_clhaadf3_0024.jpg\n"
4704
     ]
4705
    },
4706
    {
4707
     "name": "stdout",
4708
     "output_type": "stream",
4709
     "text": [
4710
      "0501_300kx_1nm_clhaadf3_0026.jpg\n"
4711
     ]
4712
    },
4713
    {
4714
     "name": "stdout",
4715
     "output_type": "stream",
4716
     "text": [
4717
      "0501_300kx_1nm_clhaadf3_0030.jpg\n0501_300kx_1nm_clhaadf3_0031.jpg\n"
4718
     ]
4719
    },
4720
    {
4721
     "name": "stdout",
4722
     "output_type": "stream",
4723
     "text": [
4724
      "0501_300kx_1nm_clhaadf3_0034.jpg\n0501_300kx_1nm_clhaadf3_0036.jpg\n"
4725
     ]
4726
    },
4727
    {
4728
     "name": "stdout",
4729
     "output_type": "stream",
4730
     "text": [
4731
      "0501_300kx_1nm_clhaadf3_0040.jpg\n"
4732
     ]
4733
    },
4734
    {
4735
     "name": "stdout",
4736
     "output_type": "stream",
4737
     "text": [
4738
      "0501_300kx_1nm_clhaadf3_0044.jpg\n"
4739
     ]
4740
    },
4741
    {
4742
     "name": "stdout",
4743
     "output_type": "stream",
4744
     "text": [
4745
      "0501_300kx_1nm_clhaadf3_0046.jpg\n0501_300kx_1nm_clhaadf3_0048.jpg\n"
4746
     ]
4747
    },
4748
    {
4749
     "name": "stdout",
4750
     "output_type": "stream",
4751
     "text": [
4752
      "0501_300kx_1nm_clhaadf3_0050.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0002.jpg\n"
4753
     ]
4754
    },
4755
    {
4756
     "name": "stdout",
4757
     "output_type": "stream",
4758
     "text": [
4759
      "200kV_500kx_p2nm_8cmCL_grain1_0002_n.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0004_n.jpg\n"
4760
     ]
4761
    },
4762
    {
4763
     "name": "stdout",
4764
     "output_type": "stream",
4765
     "text": [
4766
      "200kV_500kx_p2nm_8cmCL_grain1_0048 - Copy.jpg\n"
4767
     ]
4768
    },
4769
    {
4770
     "name": "stdout",
4771
     "output_type": "stream",
4772
     "text": [
4773
      "200kV_500kx_p2nm_8cmCL_grain1_0050 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0052 - Copy.jpg\n"
4774
     ]
4775
    },
4776
    {
4777
     "name": "stdout",
4778
     "output_type": "stream",
4779
     "text": [
4780
      "200kV_500kx_p2nm_8cmCL_grain1_0054 - Copy.jpg\n"
4781
     ]
4782
    },
4783
    {
4784
     "name": "stdout",
4785
     "output_type": "stream",
4786
     "text": [
4787
      "200kV_500kx_p2nm_8cmCL_grain1_0058 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0060 - Copy.jpg\n"
4788
     ]
4789
    },
4790
    {
4791
     "name": "stdout",
4792
     "output_type": "stream",
4793
     "text": [
4794
      "200kV_500kx_p2nm_8cmCL_grain1_0062 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0064 - Copy.jpg\n"
4795
     ]
4796
    },
4797
    {
4798
     "name": "stdout",
4799
     "output_type": "stream",
4800
     "text": [
4801
      "200kV_500kx_p2nm_8cmCL_grain1_0066 - Copy.jpg\n"
4802
     ]
4803
    },
4804
    {
4805
     "name": "stdout",
4806
     "output_type": "stream",
4807
     "text": [
4808
      "200kV_500kx_p2nm_8cmCL_grain1_0068 - Copy.jpg\n"
4809
     ]
4810
    },
4811
    {
4812
     "name": "stdout",
4813
     "output_type": "stream",
4814
     "text": [
4815
      "200kV_500kx_p2nm_8cmCL_grain1_0070 - Copy.jpg\n"
4816
     ]
4817
    },
4818
    {
4819
     "name": "stdout",
4820
     "output_type": "stream",
4821
     "text": [
4822
      "200kV_500kx_p2nm_8cmCL_grain1_0072 - Copy.jpg\n"
4823
     ]
4824
    },
4825
    {
4826
     "name": "stdout",
4827
     "output_type": "stream",
4828
     "text": [
4829
      "200kV_500kx_p2nm_8cmCL_grain1_0074 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0076 - Copy.jpg\n"
4830
     ]
4831
    },
4832
    {
4833
     "name": "stdout",
4834
     "output_type": "stream",
4835
     "text": [
4836
      "200kV_500kx_p2nm_8cmCL_grain1_0078 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0080 - Copy.jpg\n"
4837
     ]
4838
    },
4839
    {
4840
     "name": "stdout",
4841
     "output_type": "stream",
4842
     "text": [
4843
      "200kV_500kx_p2nm_8cmCL_grain1_0084 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0086 - Copy.jpg\n"
4844
     ]
4845
    },
4846
    {
4847
     "name": "stdout",
4848
     "output_type": "stream",
4849
     "text": [
4850
      "200kV_500kx_p2nm_8cmCL_grain1_0090 - Copy.jpg\n"
4851
     ]
4852
    },
4853
    {
4854
     "name": "stdout",
4855
     "output_type": "stream",
4856
     "text": [
4857
      "200kV_500kx_p2nm_8cmCL_grain1_0092 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0109.jpg\n"
4858
     ]
4859
    },
4860
    {
4861
     "name": "stdout",
4862
     "output_type": "stream",
4863
     "text": [
4864
      "200kV_500kx_p2nm_8cmCL_grain1_0111.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0129.jpg\n"
4865
     ]
4866
    },
4867
    {
4868
     "name": "stdout",
4869
     "output_type": "stream",
4870
     "text": [
4871
      "200kV_500kx_p2nm_8cmCL_grain2_0010.jpg\n200kV_500kx_p2nm_8cmCL_grain2_0012.jpg\n"
4872
     ]
4873
    },
4874
    {
4875
     "name": "stdout",
4876
     "output_type": "stream",
4877
     "text": [
4878
      "200kV_500kx_p2nm_8cmCL_grain3_4_0258.jpg\n2501_300kx_1nm_clhaadf3_0032.jpg\n"
4879
     ]
4880
    },
4881
    {
4882
     "name": "stdout",
4883
     "output_type": "stream",
4884
     "text": [
4885
      "2501_300kx_1nm_clhaadf3_0034.jpg\n2501_300kx_1nm_clhaadf3_0036.jpg\n"
4886
     ]
4887
    },
4888
    {
4889
     "name": "stdout",
4890
     "output_type": "stream",
4891
     "text": [
4892
      "2501_300kx_1nm_clhaadf3_0038.jpg\n2501_300kx_1nm_clhaadf3_0040.jpg\n"
4893
     ]
4894
    },
4895
    {
4896
     "name": "stdout",
4897
     "output_type": "stream",
4898
     "text": [
4899
      "2501_300kx_1nm_clhaadf3_0042.jpg\n"
4900
     ]
4901
    },
4902
    {
4903
     "name": "stdout",
4904
     "output_type": "stream",
4905
     "text": [
4906
      "2501_300kx_1nm_clhaadf3_0044.jpg\n2501_300kx_1nm_clhaadf3_0046.jpg\n"
4907
     ]
4908
    },
4909
    {
4910
     "name": "stdout",
4911
     "output_type": "stream",
4912
     "text": [
4913
      "2501_300kx_1nm_clhaadf3_0048.jpg\n2501_300kx_1nm_clhaadf3_0050.jpg\n"
4914
     ]
4915
    },
4916
    {
4917
     "name": "stdout",
4918
     "output_type": "stream",
4919
     "text": [
4920
      "2501_300kx_1nm_clhaadf3_0052.jpg\n"
4921
     ]
4922
    },
4923
    {
4924
     "name": "stdout",
4925
     "output_type": "stream",
4926
     "text": [
4927
      "2501_300kx_1nm_clhaadf3_0054.jpg\n2ROI_100kx_4100CL_foil1.jpg\n"
4928
     ]
4929
    },
4930
    {
4931
     "name": "stdout",
4932
     "output_type": "stream",
4933
     "text": [
4934
      "3ROI_100kx_4100CL_foil1.jpg\n4ROI_100kx_4100CL_foil1.jpg\n"
4935
     ]
4936
    },
4937
    {
4938
     "name": "stdout",
4939
     "output_type": "stream",
4940
     "text": [
4941
      "5401_300kx_1nm_clhaadf3_0004.jpg\n5401_300kx_1nm_clhaadf3_0006.jpg\n"
4942
     ]
4943
    },
4944
    {
4945
     "name": "stdout",
4946
     "output_type": "stream",
4947
     "text": [
4948
      "5401_300kx_1nm_clhaadf3_0016.jpg\n5401_300kx_1nm_clhaadf3_0018.jpg\n"
4949
     ]
4950
    },
4951
    {
4952
     "name": "stdout",
4953
     "output_type": "stream",
4954
     "text": [
4955
      "5401_300kx_1nm_clhaadf3_0022.jpg\n"
4956
     ]
4957
    },
4958
    {
4959
     "name": "stdout",
4960
     "output_type": "stream",
4961
     "text": [
4962
      "5401_300kx_1nm_clhaadf3_0024.jpg\n5401_300kx_1nm_clhaadf3_0026.jpg\n"
4963
     ]
4964
    },
4965
    {
4966
     "name": "stdout",
4967
     "output_type": "stream",
4968
     "text": [
4969
      "5401_300kx_1nm_clhaadf3_0030.jpg\n5ROI_100kx_4100CL_foil1 copy.jpg\n"
4970
     ]
4971
    },
4972
    {
4973
     "name": "stdout",
4974
     "output_type": "stream",
4975
     "text": [
4976
      "6ROI_100kx_4100CL_foil1 copy.jpg\n7ROI_100kx_4100CL_foil1 copy.jpg\n"
4977
     ]
4978
    },
4979
    {
4980
     "name": "stdout",
4981
     "output_type": "stream",
4982
     "text": [
4983
      "9ROI_100kx_4100CL_foil1.jpg\nBF X500K, 01 (2).jpg\n"
4984
     ]
4985
    },
4986
    {
4987
     "name": "stdout",
4988
     "output_type": "stream",
4989
     "text": [
4990
      "BF X500K, 01 (3).jpg\nBF X500K, 02 (2).jpg\n"
4991
     ]
4992
    },
4993
    {
4994
     "name": "stdout",
4995
     "output_type": "stream",
4996
     "text": [
4997
      "BF X500K, 03 (2).jpg\n"
4998
     ]
4999
    },
5000
    {
5001
     "name": "stdout",
5002
     "output_type": "stream",
5003
     "text": [
5004
      "BF X500K, 04.jpg\nBF X500K, 05 (2).jpg\n"
5005
     ]
5006
    },
5007
    {
5008
     "name": "stdout",
5009
     "output_type": "stream",
5010
     "text": [
5011
      "BF X500K, 05.jpg\nBF X500K, 06 (3).jpg\n"
5012
     ]
5013
    },
5014
    {
5015
     "name": "stdout",
5016
     "output_type": "stream",
5017
     "text": [
5018
      "BF X500K, 06.jpg\nBF X500K, 07 (2).jpg\n"
5019
     ]
5020
    },
5021
    {
5022
     "name": "stdout",
5023
     "output_type": "stream",
5024
     "text": [
5025
      "BF X500K, 08 (2).jpg\n"
5026
     ]
5027
    },
5028
    {
5029
     "name": "stdout",
5030
     "output_type": "stream",
5031
     "text": [
5032
      "BF X500K, 08 (3).jpg\n"
5033
     ]
5034
    },
5035
    {
5036
     "name": "stdout",
5037
     "output_type": "stream",
5038
     "text": [
5039
      "BF X500K, 08.jpg\nBF X500K, 10 (2).jpg\n"
5040
     ]
5041
    },
5042
    {
5043
     "name": "stdout",
5044
     "output_type": "stream",
5045
     "text": [
5046
      "BF X500K, 10 (3).jpg\nBF X500K, 10.jpg\n"
5047
     ]
5048
    },
5049
    {
5050
     "name": "stdout",
5051
     "output_type": "stream",
5052
     "text": [
5053
      "BF X500K, 11 (2).jpg\nBF X500K, 11 (3).jpg\n"
5054
     ]
5055
    },
5056
    {
5057
     "name": "stdout",
5058
     "output_type": "stream",
5059
     "text": [
5060
      "BF X500K, 11.jpg\nBF X500K, 12 (2).jpg\n"
5061
     ]
5062
    },
5063
    {
5064
     "name": "stdout",
5065
     "output_type": "stream",
5066
     "text": [
5067
      "BF X500K, 12 (3).jpg\nBF X500K, 12.jpg\n"
5068
     ]
5069
    },
5070
    {
5071
     "name": "stdout",
5072
     "output_type": "stream",
5073
     "text": [
5074
      "BF X500K, 13 (3).jpg\nBF X500K, 14.jpg\n"
5075
     ]
5076
    },
5077
    {
5078
     "name": "stdout",
5079
     "output_type": "stream",
5080
     "text": [
5081
      "BF X500K, 15.jpg\nBF X500K, 16.jpg\n"
5082
     ]
5083
    },
5084
    {
5085
     "name": "stdout",
5086
     "output_type": "stream",
5087
     "text": [
5088
      "K713_300kx_store4_grid1_0005.jpg\nK713_300kx_store4_grid1_0007.jpg\n"
5089
     ]
5090
    },
5091
    {
5092
     "name": "stdout",
5093
     "output_type": "stream",
5094
     "text": [
5095
      "K713_300kx_store4_grid1_0009.jpg\n"
5096
     ]
5097
    },
5098
    {
5099
     "name": "stdout",
5100
     "output_type": "stream",
5101
     "text": [
5102
      "K713_300kx_store4_grid1_0011.jpg\n"
5103
     ]
5104
    },
5105
    {
5106
     "name": "stdout",
5107
     "output_type": "stream",
5108
     "text": [
5109
      "K713_300kx_store4_grid1_0013.jpg\nK713_300kx_store4_grid1_0015.jpg\n"
5110
     ]
5111
    },
5112
    {
5113
     "name": "stdout",
5114
     "output_type": "stream",
5115
     "text": [
5116
      "K713_300kx_store4_grid1_0017.jpg\nK713_300kx_store4_grid1_0019.jpg\n"
5117
     ]
5118
    },
5119
    {
5120
     "name": "stdout",
5121
     "output_type": "stream",
5122
     "text": [
5123
      "dalong2.jpg\ndalong3.jpg\n"
5124
     ]
5125
    },
5126
    {
5127
     "name": "stdout",
5128
     "output_type": "stream",
5129
     "text": [
5130
      "g1_backonzone_GBtowardsfrom_0005.jpg\n"
5131
     ]
5132
    },
5133
    {
5134
     "name": "stdout",
5135
     "output_type": "stream",
5136
     "text": [
5137
      "g1_frontonzone_GBtowardsback_0006.jpg\n"
5138
     ]
5139
    },
5140
    {
5141
     "name": "stdout",
5142
     "output_type": "stream",
5143
     "text": [
5144
      "g2_midonzone_GBtowardsfront_0005.jpg\n"
5145
     ]
5146
    },
5147
    {
5148
     "name": "stdout",
5149
     "output_type": "stream",
5150
     "text": [
5151
      "g2_midonzone_GBtowardsfront_0034.jpg\n"
5152
     ]
5153
    },
5154
    {
5155
     "name": "stdout",
5156
     "output_type": "stream",
5157
     "text": [
5158
      "g2_midonzone_GBtowardsmmiddle_0003.jpg\n"
5159
     ]
5160
    },
5161
    {
5162
     "name": "stdout",
5163
     "output_type": "stream",
5164
     "text": [
5165
      "g2_midonzone_GBtowardsmmiddle_0005.jpg\n"
5166
     ]
5167
    },
5168
    {
5169
     "name": "stdout",
5170
     "output_type": "stream",
5171
     "text": [
5172
      "grid1_roi1_500kx_0p5nm_haadf1_0001.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0003.jpg\n"
5173
     ]
5174
    },
5175
    {
5176
     "name": "stdout",
5177
     "output_type": "stream",
5178
     "text": [
5179
      "grid1_roi1_500kx_0p5nm_haadf1_0005.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0007.jpg\n"
5180
     ]
5181
    },
5182
    {
5183
     "name": "stdout",
5184
     "output_type": "stream",
5185
     "text": [
5186
      "grid1_roi1_500kx_0p5nm_haadf1_0009.jpg\n"
5187
     ]
5188
    },
5189
    {
5190
     "name": "stdout",
5191
     "output_type": "stream",
5192
     "text": [
5193
      "grid1_roi1_500kx_0p5nm_haadf1_0011.jpg\n"
5194
     ]
5195
    },
5196
    {
5197
     "name": "stdout",
5198
     "output_type": "stream",
5199
     "text": [
5200
      "grid1_roi1_500kx_0p5nm_haadf1_0013.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0015.jpg\n"
5201
     ]
5202
    },
5203
    {
5204
     "name": "stdout",
5205
     "output_type": "stream",
5206
     "text": [
5207
      "grid1_roi1_500kx_0p5nm_haadf1_0017.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0019.jpg\n"
5208
     ]
5209
    },
5210
    {
5211
     "name": "stdout",
5212
     "output_type": "stream",
5213
     "text": [
5214
      "grid1_roi1_500kx_0p5nm_haadf1_0021.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0023.jpg\n"
5215
     ]
5216
    },
5217
    {
5218
     "name": "stdout",
5219
     "output_type": "stream",
5220
     "text": [
5221
      "grid1_roi1_500kx_0p5nm_haadf1_0027.jpg\n"
5222
     ]
5223
    },
5224
    {
5225
     "name": "stdout",
5226
     "output_type": "stream",
5227
     "text": [
5228
      "grid1_roi1_500kx_0p5nm_haadf1_0029.jpg\n"
5229
     ]
5230
    },
5231
    {
5232
     "name": "stdout",
5233
     "output_type": "stream",
5234
     "text": [
5235
      "grid1_roi1_500kx_0p5nm_haadf1_0031.jpg\n"
5236
     ]
5237
    },
5238
    {
5239
     "name": "stdout",
5240
     "output_type": "stream",
5241
     "text": [
5242
      "grid1_roi2_500kx_0p5nm_haadf1_0035.jpg\n"
5243
     ]
5244
    },
5245
    {
5246
     "name": "stdout",
5247
     "output_type": "stream",
5248
     "text": [
5249
      "grid1_roi2_500kx_0p5nm_haadf1_0037.jpg\n"
5250
     ]
5251
    },
5252
    {
5253
     "name": "stdout",
5254
     "output_type": "stream",
5255
     "text": [
5256
      "grid1_roi2_500kx_0p5nm_haadf1_0039.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0041.jpg\n"
5257
     ]
5258
    },
5259
    {
5260
     "name": "stdout",
5261
     "output_type": "stream",
5262
     "text": [
5263
      "grid1_roi2_500kx_0p5nm_haadf1_0044.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0045.jpg\n"
5264
     ]
5265
    },
5266
    {
5267
     "name": "stdout",
5268
     "output_type": "stream",
5269
     "text": [
5270
      "grid1_roi2_500kx_0p5nm_haadf1_0049.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0051.jpg\n"
5271
     ]
5272
    },
5273
    {
5274
     "name": "stdout",
5275
     "output_type": "stream",
5276
     "text": [
5277
      "grid1_roi2_500kx_0p5nm_haadf1_0053.jpg\n"
5278
     ]
5279
    },
5280
    {
5281
     "name": "stdout",
5282
     "output_type": "stream",
5283
     "text": [
5284
      "grid1_roi2_500kx_0p5nm_haadf1_0055.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0057.jpg\n"
5285
     ]
5286
    },
5287
    {
5288
     "name": "stdout",
5289
     "output_type": "stream",
5290
     "text": [
5291
      "grid1_roi2_500kx_0p5nm_haadf1_0059.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0061.jpg\n"
5292
     ]
5293
    },
5294
    {
5295
     "name": "stdout",
5296
     "output_type": "stream",
5297
     "text": [
5298
      "grid1_roi2_500kx_0p5nm_haadf1_0063.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0065.jpg\n"
5299
     ]
5300
    },
5301
    {
5302
     "name": "stdout",
5303
     "output_type": "stream",
5304
     "text": [
5305
      "grid1_roi2_500kx_0p5nm_haadf1_0067.jpg\n"
5306
     ]
5307
    },
5308
    {
5309
     "name": "stdout",
5310
     "output_type": "stream",
5311
     "text": [
5312
      "grid1_roi2_500kx_0p5nm_haadf1_0069.jpg\nBF X500K, 05 (3).jpg\n"
5313
     ]
5314
    },
5315
    {
5316
     "name": "stdout",
5317
     "output_type": "stream",
5318
     "text": [
5319
      "0501_300kx_1nm_clhaadf3_0038.jpg\n"
5320
     ]
5321
    }
5322
   ],
5323
   "source": [
5324
    "##################################################\n",
5325
    "# 1. Define data augmentation operations\n",
5326
    "##################################################\n",
5327
    "\n",
5328
    "trainImageTxtFile = dataPath + \"trainimages.txt\"\n",
5329
    "imageList = getImageList(trainImageTxtFile)\n",
5330
    "\n",
5331
    "current_operation = \"Flipud\"\n",
5332
    "\n",
5333
    "# Flip/mirror input images vertically.\n",
5334
    "   \n",
5335
    "from imgaug import augmenters as iaa\n",
5336
    "ia.seed(1)\n",
5337
    "seq = iaa.Sequential([\n",
5338
    "        iaa.Flipud(1.0)\n",
5339
    "])\n",
5340
    "\n",
5341
    "# seq = iaa.Sequential([ \n",
5342
    "#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.\n",
5343
    "#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.\n",
5344
    "#     iaa.GammaContrast((0.5, 1.5))\n",
5345
    "# ])\n",
5346
    "\n",
5347
    "# Make our sequence deterministic.\n",
5348
    "# We can now apply it to the image and then to the BBs and it will\n",
5349
    "# lead to the same augmentations.\n",
5350
    "# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!\n",
5351
    "seq_det = seq.to_deterministic()\n",
5352
    "\n",
5353
    "##################################################\n",
5354
    "# 2. loop through images\n",
5355
    "##################################################\n",
5356
    "\n",
5357
    "for img in imageList:\n",
5358
    "    print(img)\n",
5359
    "    # Grayscale images must have shape (height, width, 1) each.\n",
5360
    "    #print(os.listdir(dataPath+'images/'))\n",
5361
    "    currentimage = skimage.io.imread(dataPath+'images/'+img).astype(np.uint8)\n",
5362
    "    # gray2rgb() simply duplicates the gray values over the three color channels.\n",
5363
    "    currentimage = color.gray2rgb(currentimage)\n",
5364
    "    bbs = bboxSetupInImage(dataPath , img.rstrip('.jpg') + '.txt',currentimage)\n",
5365
    "    # Augment BBs and images.\n",
5366
    "    # As we only have one image and list of BBs, we use\n",
5367
    "    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your\n",
5368
    "    # variables would likely already be lists.\n",
5369
    "    image_aug = seq_det.augment_images([currentimage])[0]\n",
5370
    "    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]\n",
5371
    "    augImgFolder = current_operation + \"Images\"\n",
5372
    "    augTxTFolder = current_operation + \"TXT\"\n",
5373
    "    createFolder(augImgFolder)\n",
5374
    "    createFolder(augTxTFolder)\n",
5375
    "    # Save aug images and bboxes\n",
5376
    "    skimage.io.imsave(augImgFolder + '/'+\n",
5377
    "                      img.rstrip('.jpg') + \n",
5378
    "                      '_' + current_operation +\n",
5379
    "                      '.jpg'\n",
5380
    "                      ,image_aug)\n",
5381
    "    saveAugbbox2TXT(augTxTFolder+ '/'+ \n",
5382
    "                    img.rstrip('.jpg') + \n",
5383
    "                    '_'+ current_operation +\n",
5384
    "                    '.txt',bbs_aug)\n",
5385
    "    # image with BBs before/after augmentation (shown below)\n",
5386
    "    # image_before = bbs.draw_on_image(currentimage, thickness=2)\n",
5387
    "    # image_after = bbs_aug.draw_on_image(image_aug, \n",
5388
    "    # thickness=2, color=[0, 0, 255])\n",
5389
    "    # image with BBs before/after augmentation (shown below)\n",
5390
    "    # plot and save figures before and after data augmentations\n",
5391
    "    #skimage.io.imshow(image_before)\n",
5392
    "    #skimage.io.imshow(image_after)\n",
5393
    "    # for i in range(len(bbs.bounding_boxes)):\n",
5394
    "    #     before = bbs.bounding_boxes[i]\n",
5395
    "    #     after = bbs_aug.bounding_boxes[i]\n",
5396
    "    #     print(\"BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)\" % (\n",
5397
    "    #         i,\n",
5398
    "    #         before.x1, before.y1, before.x2, before.y2,\n",
5399
    "    #         after.x1, after.y1, after.x2, after.y2)\n",
5400
    "    #     )"
5401
   ]
5402
  },
5403
  {
5404
   "cell_type": "heading",
5405
   "metadata": {},
5406
   "level": 2,
5407
   "source": [
5408
    "Rotate 90 or 270 degree"
5409
   ]
5410
  },
5411
  {
5412
   "cell_type": "code",
5413
   "execution_count": 38,
5414
   "metadata": {},
5415
   "outputs": [
5416
    {
5417
     "name": "stdout",
5418
     "output_type": "stream",
5419
     "text": [
5420
      "0501_300kx_1nm_clhaadf3_0006.jpg\n0501_300kx_1nm_clhaadf3_0008.jpg\n"
5421
     ]
5422
    },
5423
    {
5424
     "name": "stdout",
5425
     "output_type": "stream",
5426
     "text": [
5427
      "0501_300kx_1nm_clhaadf3_0012.jpg\n0501_300kx_1nm_clhaadf3_0016.jpg\n"
5428
     ]
5429
    },
5430
    {
5431
     "name": "stdout",
5432
     "output_type": "stream",
5433
     "text": [
5434
      "0501_300kx_1nm_clhaadf3_0018.jpg\n0501_300kx_1nm_clhaadf3_0020.jpg\n"
5435
     ]
5436
    },
5437
    {
5438
     "name": "stdout",
5439
     "output_type": "stream",
5440
     "text": [
5441
      "0501_300kx_1nm_clhaadf3_0022.jpg\n0501_300kx_1nm_clhaadf3_0024.jpg\n"
5442
     ]
5443
    },
5444
    {
5445
     "name": "stdout",
5446
     "output_type": "stream",
5447
     "text": [
5448
      "0501_300kx_1nm_clhaadf3_0026.jpg\n0501_300kx_1nm_clhaadf3_0030.jpg\n"
5449
     ]
5450
    },
5451
    {
5452
     "name": "stdout",
5453
     "output_type": "stream",
5454
     "text": [
5455
      "0501_300kx_1nm_clhaadf3_0031.jpg\n0501_300kx_1nm_clhaadf3_0034.jpg\n"
5456
     ]
5457
    },
5458
    {
5459
     "name": "stdout",
5460
     "output_type": "stream",
5461
     "text": [
5462
      "0501_300kx_1nm_clhaadf3_0036.jpg\n0501_300kx_1nm_clhaadf3_0040.jpg\n"
5463
     ]
5464
    },
5465
    {
5466
     "name": "stdout",
5467
     "output_type": "stream",
5468
     "text": [
5469
      "0501_300kx_1nm_clhaadf3_0044.jpg\n0501_300kx_1nm_clhaadf3_0046.jpg\n"
5470
     ]
5471
    },
5472
    {
5473
     "name": "stdout",
5474
     "output_type": "stream",
5475
     "text": [
5476
      "0501_300kx_1nm_clhaadf3_0048.jpg\n"
5477
     ]
5478
    },
5479
    {
5480
     "name": "stdout",
5481
     "output_type": "stream",
5482
     "text": [
5483
      "0501_300kx_1nm_clhaadf3_0050.jpg\n"
5484
     ]
5485
    },
5486
    {
5487
     "name": "stdout",
5488
     "output_type": "stream",
5489
     "text": [
5490
      "200kV_500kx_p2nm_8cmCL_grain1_0002.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0002_n.jpg\n"
5491
     ]
5492
    },
5493
    {
5494
     "name": "stdout",
5495
     "output_type": "stream",
5496
     "text": [
5497
      "200kV_500kx_p2nm_8cmCL_grain1_0004_n.jpg\n"
5498
     ]
5499
    },
5500
    {
5501
     "name": "stdout",
5502
     "output_type": "stream",
5503
     "text": [
5504
      "200kV_500kx_p2nm_8cmCL_grain1_0048 - Copy.jpg\n"
5505
     ]
5506
    },
5507
    {
5508
     "name": "stdout",
5509
     "output_type": "stream",
5510
     "text": [
5511
      "200kV_500kx_p2nm_8cmCL_grain1_0050 - Copy.jpg\n"
5512
     ]
5513
    },
5514
    {
5515
     "name": "stdout",
5516
     "output_type": "stream",
5517
     "text": [
5518
      "200kV_500kx_p2nm_8cmCL_grain1_0052 - Copy.jpg\n"
5519
     ]
5520
    },
5521
    {
5522
     "name": "stdout",
5523
     "output_type": "stream",
5524
     "text": [
5525
      "200kV_500kx_p2nm_8cmCL_grain1_0054 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0058 - Copy.jpg\n"
5526
     ]
5527
    },
5528
    {
5529
     "name": "stdout",
5530
     "output_type": "stream",
5531
     "text": [
5532
      "200kV_500kx_p2nm_8cmCL_grain1_0060 - Copy.jpg\n"
5533
     ]
5534
    },
5535
    {
5536
     "name": "stdout",
5537
     "output_type": "stream",
5538
     "text": [
5539
      "200kV_500kx_p2nm_8cmCL_grain1_0062 - Copy.jpg\n"
5540
     ]
5541
    },
5542
    {
5543
     "name": "stdout",
5544
     "output_type": "stream",
5545
     "text": [
5546
      "200kV_500kx_p2nm_8cmCL_grain1_0064 - Copy.jpg\n"
5547
     ]
5548
    },
5549
    {
5550
     "name": "stdout",
5551
     "output_type": "stream",
5552
     "text": [
5553
      "200kV_500kx_p2nm_8cmCL_grain1_0066 - Copy.jpg\n"
5554
     ]
5555
    },
5556
    {
5557
     "name": "stdout",
5558
     "output_type": "stream",
5559
     "text": [
5560
      "200kV_500kx_p2nm_8cmCL_grain1_0068 - Copy.jpg\n"
5561
     ]
5562
    },
5563
    {
5564
     "name": "stdout",
5565
     "output_type": "stream",
5566
     "text": [
5567
      "200kV_500kx_p2nm_8cmCL_grain1_0070 - Copy.jpg\n"
5568
     ]
5569
    },
5570
    {
5571
     "name": "stdout",
5572
     "output_type": "stream",
5573
     "text": [
5574
      "200kV_500kx_p2nm_8cmCL_grain1_0072 - Copy.jpg\n"
5575
     ]
5576
    },
5577
    {
5578
     "name": "stdout",
5579
     "output_type": "stream",
5580
     "text": [
5581
      "200kV_500kx_p2nm_8cmCL_grain1_0074 - Copy.jpg\n"
5582
     ]
5583
    },
5584
    {
5585
     "name": "stdout",
5586
     "output_type": "stream",
5587
     "text": [
5588
      "200kV_500kx_p2nm_8cmCL_grain1_0076 - Copy.jpg\n"
5589
     ]
5590
    },
5591
    {
5592
     "name": "stdout",
5593
     "output_type": "stream",
5594
     "text": [
5595
      "200kV_500kx_p2nm_8cmCL_grain1_0078 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0080 - Copy.jpg\n"
5596
     ]
5597
    },
5598
    {
5599
     "name": "stdout",
5600
     "output_type": "stream",
5601
     "text": [
5602
      "200kV_500kx_p2nm_8cmCL_grain1_0084 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0086 - Copy.jpg\n"
5603
     ]
5604
    },
5605
    {
5606
     "name": "stdout",
5607
     "output_type": "stream",
5608
     "text": [
5609
      "200kV_500kx_p2nm_8cmCL_grain1_0090 - Copy.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0092 - Copy.jpg\n"
5610
     ]
5611
    },
5612
    {
5613
     "name": "stdout",
5614
     "output_type": "stream",
5615
     "text": [
5616
      "200kV_500kx_p2nm_8cmCL_grain1_0109.jpg\n"
5617
     ]
5618
    },
5619
    {
5620
     "name": "stdout",
5621
     "output_type": "stream",
5622
     "text": [
5623
      "200kV_500kx_p2nm_8cmCL_grain1_0111.jpg\n200kV_500kx_p2nm_8cmCL_grain1_0129.jpg\n"
5624
     ]
5625
    },
5626
    {
5627
     "name": "stdout",
5628
     "output_type": "stream",
5629
     "text": [
5630
      "200kV_500kx_p2nm_8cmCL_grain2_0010.jpg\n200kV_500kx_p2nm_8cmCL_grain2_0012.jpg\n"
5631
     ]
5632
    },
5633
    {
5634
     "name": "stdout",
5635
     "output_type": "stream",
5636
     "text": [
5637
      "200kV_500kx_p2nm_8cmCL_grain3_4_0258.jpg\n"
5638
     ]
5639
    },
5640
    {
5641
     "name": "stdout",
5642
     "output_type": "stream",
5643
     "text": [
5644
      "2501_300kx_1nm_clhaadf3_0032.jpg\n2501_300kx_1nm_clhaadf3_0034.jpg\n"
5645
     ]
5646
    },
5647
    {
5648
     "name": "stdout",
5649
     "output_type": "stream",
5650
     "text": [
5651
      "2501_300kx_1nm_clhaadf3_0036.jpg\n2501_300kx_1nm_clhaadf3_0038.jpg\n"
5652
     ]
5653
    },
5654
    {
5655
     "name": "stdout",
5656
     "output_type": "stream",
5657
     "text": [
5658
      "2501_300kx_1nm_clhaadf3_0040.jpg\n2501_300kx_1nm_clhaadf3_0042.jpg\n"
5659
     ]
5660
    },
5661
    {
5662
     "name": "stdout",
5663
     "output_type": "stream",
5664
     "text": [
5665
      "2501_300kx_1nm_clhaadf3_0044.jpg\n2501_300kx_1nm_clhaadf3_0046.jpg\n"
5666
     ]
5667
    },
5668
    {
5669
     "name": "stdout",
5670
     "output_type": "stream",
5671
     "text": [
5672
      "2501_300kx_1nm_clhaadf3_0048.jpg\n2501_300kx_1nm_clhaadf3_0050.jpg\n"
5673
     ]
5674
    },
5675
    {
5676
     "name": "stdout",
5677
     "output_type": "stream",
5678
     "text": [
5679
      "2501_300kx_1nm_clhaadf3_0052.jpg\n2501_300kx_1nm_clhaadf3_0054.jpg\n"
5680
     ]
5681
    },
5682
    {
5683
     "name": "stdout",
5684
     "output_type": "stream",
5685
     "text": [
5686
      "2ROI_100kx_4100CL_foil1.jpg\n3ROI_100kx_4100CL_foil1.jpg\n"
5687
     ]
5688
    },
5689
    {
5690
     "name": "stdout",
5691
     "output_type": "stream",
5692
     "text": [
5693
      "4ROI_100kx_4100CL_foil1.jpg\n5401_300kx_1nm_clhaadf3_0004.jpg\n"
5694
     ]
5695
    },
5696
    {
5697
     "name": "stdout",
5698
     "output_type": "stream",
5699
     "text": [
5700
      "5401_300kx_1nm_clhaadf3_0006.jpg\n5401_300kx_1nm_clhaadf3_0016.jpg\n"
5701
     ]
5702
    },
5703
    {
5704
     "name": "stdout",
5705
     "output_type": "stream",
5706
     "text": [
5707
      "5401_300kx_1nm_clhaadf3_0018.jpg\n5401_300kx_1nm_clhaadf3_0022.jpg\n"
5708
     ]
5709
    },
5710
    {
5711
     "name": "stdout",
5712
     "output_type": "stream",
5713
     "text": [
5714
      "5401_300kx_1nm_clhaadf3_0024.jpg\n5401_300kx_1nm_clhaadf3_0026.jpg\n"
5715
     ]
5716
    },
5717
    {
5718
     "name": "stdout",
5719
     "output_type": "stream",
5720
     "text": [
5721
      "5401_300kx_1nm_clhaadf3_0030.jpg\n5ROI_100kx_4100CL_foil1 copy.jpg\n"
5722
     ]
5723
    },
5724
    {
5725
     "name": "stdout",
5726
     "output_type": "stream",
5727
     "text": [
5728
      "6ROI_100kx_4100CL_foil1 copy.jpg\n7ROI_100kx_4100CL_foil1 copy.jpg\n"
5729
     ]
5730
    },
5731
    {
5732
     "name": "stdout",
5733
     "output_type": "stream",
5734
     "text": [
5735
      "9ROI_100kx_4100CL_foil1.jpg\nBF X500K, 01 (2).jpg\n"
5736
     ]
5737
    },
5738
    {
5739
     "name": "stdout",
5740
     "output_type": "stream",
5741
     "text": [
5742
      "BF X500K, 01 (3).jpg\nBF X500K, 02 (2).jpg\n"
5743
     ]
5744
    },
5745
    {
5746
     "name": "stdout",
5747
     "output_type": "stream",
5748
     "text": [
5749
      "BF X500K, 03 (2).jpg\nBF X500K, 04.jpg\n"
5750
     ]
5751
    },
5752
    {
5753
     "name": "stdout",
5754
     "output_type": "stream",
5755
     "text": [
5756
      "BF X500K, 05 (2).jpg\nBF X500K, 05.jpg\n"
5757
     ]
5758
    },
5759
    {
5760
     "name": "stdout",
5761
     "output_type": "stream",
5762
     "text": [
5763
      "BF X500K, 06 (3).jpg\nBF X500K, 06.jpg\n"
5764
     ]
5765
    },
5766
    {
5767
     "name": "stdout",
5768
     "output_type": "stream",
5769
     "text": [
5770
      "BF X500K, 07 (2).jpg\nBF X500K, 08 (2).jpg\n"
5771
     ]
5772
    },
5773
    {
5774
     "name": "stdout",
5775
     "output_type": "stream",
5776
     "text": [
5777
      "BF X500K, 08 (3).jpg\nBF X500K, 08.jpg\n"
5778
     ]
5779
    },
5780
    {
5781
     "name": "stdout",
5782
     "output_type": "stream",
5783
     "text": [
5784
      "BF X500K, 10 (2).jpg\nBF X500K, 10 (3).jpg\n"
5785
     ]
5786
    },
5787
    {
5788
     "name": "stdout",
5789
     "output_type": "stream",
5790
     "text": [
5791
      "BF X500K, 10.jpg\nBF X500K, 11 (2).jpg\n"
5792
     ]
5793
    },
5794
    {
5795
     "name": "stdout",
5796
     "output_type": "stream",
5797
     "text": [
5798
      "BF X500K, 11 (3).jpg\nBF X500K, 11.jpg\n"
5799
     ]
5800
    },
5801
    {
5802
     "name": "stdout",
5803
     "output_type": "stream",
5804
     "text": [
5805
      "BF X500K, 12 (2).jpg\nBF X500K, 12 (3).jpg\n"
5806
     ]
5807
    },
5808
    {
5809
     "name": "stdout",
5810
     "output_type": "stream",
5811
     "text": [
5812
      "BF X500K, 12.jpg\nBF X500K, 13 (3).jpg\n"
5813
     ]
5814
    },
5815
    {
5816
     "name": "stdout",
5817
     "output_type": "stream",
5818
     "text": [
5819
      "BF X500K, 14.jpg\nBF X500K, 15.jpg\n"
5820
     ]
5821
    },
5822
    {
5823
     "name": "stdout",
5824
     "output_type": "stream",
5825
     "text": [
5826
      "BF X500K, 16.jpg\nK713_300kx_store4_grid1_0005.jpg\n"
5827
     ]
5828
    },
5829
    {
5830
     "name": "stdout",
5831
     "output_type": "stream",
5832
     "text": [
5833
      "K713_300kx_store4_grid1_0007.jpg\nK713_300kx_store4_grid1_0009.jpg\n"
5834
     ]
5835
    },
5836
    {
5837
     "name": "stdout",
5838
     "output_type": "stream",
5839
     "text": [
5840
      "K713_300kx_store4_grid1_0011.jpg\nK713_300kx_store4_grid1_0013.jpg\n"
5841
     ]
5842
    },
5843
    {
5844
     "name": "stdout",
5845
     "output_type": "stream",
5846
     "text": [
5847
      "K713_300kx_store4_grid1_0015.jpg\nK713_300kx_store4_grid1_0017.jpg\n"
5848
     ]
5849
    },
5850
    {
5851
     "name": "stdout",
5852
     "output_type": "stream",
5853
     "text": [
5854
      "K713_300kx_store4_grid1_0019.jpg\ndalong2.jpg\n"
5855
     ]
5856
    },
5857
    {
5858
     "name": "stdout",
5859
     "output_type": "stream",
5860
     "text": [
5861
      "dalong3.jpg\ng1_backonzone_GBtowardsfrom_0005.jpg\n"
5862
     ]
5863
    },
5864
    {
5865
     "name": "stdout",
5866
     "output_type": "stream",
5867
     "text": [
5868
      "g1_frontonzone_GBtowardsback_0006.jpg\n"
5869
     ]
5870
    },
5871
    {
5872
     "name": "stdout",
5873
     "output_type": "stream",
5874
     "text": [
5875
      "g2_midonzone_GBtowardsfront_0005.jpg\n"
5876
     ]
5877
    },
5878
    {
5879
     "name": "stdout",
5880
     "output_type": "stream",
5881
     "text": [
5882
      "g2_midonzone_GBtowardsfront_0034.jpg\n"
5883
     ]
5884
    },
5885
    {
5886
     "name": "stdout",
5887
     "output_type": "stream",
5888
     "text": [
5889
      "g2_midonzone_GBtowardsmmiddle_0003.jpg\n"
5890
     ]
5891
    },
5892
    {
5893
     "name": "stdout",
5894
     "output_type": "stream",
5895
     "text": [
5896
      "g2_midonzone_GBtowardsmmiddle_0005.jpg\n"
5897
     ]
5898
    },
5899
    {
5900
     "name": "stdout",
5901
     "output_type": "stream",
5902
     "text": [
5903
      "grid1_roi1_500kx_0p5nm_haadf1_0001.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0003.jpg\n"
5904
     ]
5905
    },
5906
    {
5907
     "name": "stdout",
5908
     "output_type": "stream",
5909
     "text": [
5910
      "grid1_roi1_500kx_0p5nm_haadf1_0005.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0007.jpg\n"
5911
     ]
5912
    },
5913
    {
5914
     "name": "stdout",
5915
     "output_type": "stream",
5916
     "text": [
5917
      "grid1_roi1_500kx_0p5nm_haadf1_0009.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0011.jpg\n"
5918
     ]
5919
    },
5920
    {
5921
     "name": "stdout",
5922
     "output_type": "stream",
5923
     "text": [
5924
      "grid1_roi1_500kx_0p5nm_haadf1_0013.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0015.jpg\n"
5925
     ]
5926
    },
5927
    {
5928
     "name": "stdout",
5929
     "output_type": "stream",
5930
     "text": [
5931
      "grid1_roi1_500kx_0p5nm_haadf1_0017.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0019.jpg\n"
5932
     ]
5933
    },
5934
    {
5935
     "name": "stdout",
5936
     "output_type": "stream",
5937
     "text": [
5938
      "grid1_roi1_500kx_0p5nm_haadf1_0021.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0023.jpg\n"
5939
     ]
5940
    },
5941
    {
5942
     "name": "stdout",
5943
     "output_type": "stream",
5944
     "text": [
5945
      "grid1_roi1_500kx_0p5nm_haadf1_0027.jpg\ngrid1_roi1_500kx_0p5nm_haadf1_0029.jpg\n"
5946
     ]
5947
    },
5948
    {
5949
     "name": "stdout",
5950
     "output_type": "stream",
5951
     "text": [
5952
      "grid1_roi1_500kx_0p5nm_haadf1_0031.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0035.jpg\n"
5953
     ]
5954
    },
5955
    {
5956
     "name": "stdout",
5957
     "output_type": "stream",
5958
     "text": [
5959
      "grid1_roi2_500kx_0p5nm_haadf1_0037.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0039.jpg\n"
5960
     ]
5961
    },
5962
    {
5963
     "name": "stdout",
5964
     "output_type": "stream",
5965
     "text": [
5966
      "grid1_roi2_500kx_0p5nm_haadf1_0041.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0044.jpg\n"
5967
     ]
5968
    },
5969
    {
5970
     "name": "stdout",
5971
     "output_type": "stream",
5972
     "text": [
5973
      "grid1_roi2_500kx_0p5nm_haadf1_0045.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0049.jpg\n"
5974
     ]
5975
    },
5976
    {
5977
     "name": "stdout",
5978
     "output_type": "stream",
5979
     "text": [
5980
      "grid1_roi2_500kx_0p5nm_haadf1_0051.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0053.jpg\n"
5981
     ]
5982
    },
5983
    {
5984
     "name": "stdout",
5985
     "output_type": "stream",
5986
     "text": [
5987
      "grid1_roi2_500kx_0p5nm_haadf1_0055.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0057.jpg\n"
5988
     ]
5989
    },
5990
    {
5991
     "name": "stdout",
5992
     "output_type": "stream",
5993
     "text": [
5994
      "grid1_roi2_500kx_0p5nm_haadf1_0059.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0061.jpg\n"
5995
     ]
5996
    },
5997
    {
5998
     "name": "stdout",
5999
     "output_type": "stream",
6000
     "text": [
6001
      "grid1_roi2_500kx_0p5nm_haadf1_0063.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0065.jpg\n"
6002
     ]
6003
    },
6004
    {
6005
     "name": "stdout",
6006
     "output_type": "stream",
6007
     "text": [
6008
      "grid1_roi2_500kx_0p5nm_haadf1_0067.jpg\ngrid1_roi2_500kx_0p5nm_haadf1_0069.jpg\n"
6009
     ]
6010
    },
6011
    {
6012
     "name": "stdout",
6013
     "output_type": "stream",
6014
     "text": [
6015
      "BF X500K, 05 (3).jpg\n"
6016
     ]
6017
    },
6018
    {
6019
     "name": "stdout",
6020
     "output_type": "stream",
6021
     "text": [
6022
      "0501_300kx_1nm_clhaadf3_0038.jpg\n"
6023
     ]
6024
    }
6025
   ],
6026
   "source": [
6027
    "##################################################\n",
6028
    "# 1. Define data augmentation operations\n",
6029
    "##################################################\n",
6030
    "\n",
6031
    "trainImageTxtFile = dataPath + \"trainimages.txt\"\n",
6032
    "imageList = getImageList(trainImageTxtFile)\n",
6033
    "\n",
6034
    "current_operation = \"Rot90or270Degree\"\n",
6035
    "\n",
6036
    "# Rotates all images by 90 or 270 degrees. \n",
6037
    "   \n",
6038
    "from imgaug import augmenters as iaa\n",
6039
    "ia.seed(1)\n",
6040
    "seq = iaa.Sequential([\n",
6041
    "    iaa.Rot90([1, 3])\n",
6042
    "])\n",
6043
    "\n",
6044
    "# seq = iaa.Sequential([ \n",
6045
    "#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.\n",
6046
    "#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.\n",
6047
    "#     iaa.GammaContrast((0.5, 1.5))\n",
6048
    "# ])\n",
6049
    "\n",
6050
    "# Make our sequence deterministic.\n",
6051
    "# We can now apply it to the image and then to the BBs and it will\n",
6052
    "# lead to the same augmentations.\n",
6053
    "# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!\n",
6054
    "seq_det = seq.to_deterministic()\n",
6055
    "\n",
6056
    "##################################################\n",
6057
    "# 2. loop through images\n",
6058
    "##################################################\n",
6059
    "\n",
6060
    "for img in imageList:\n",
6061
    "    print(img)\n",
6062
    "    # Grayscale images must have shape (height, width, 1) each.\n",
6063
    "    #print(os.listdir(dataPath+'images/'))\n",
6064
    "    currentimage = skimage.io.imread(dataPath+'images/'+img).astype(np.uint8)\n",
6065
    "    # gray2rgb() simply duplicates the gray values over the three color channels.\n",
6066
    "    currentimage = color.gray2rgb(currentimage)\n",
6067
    "    bbs = bboxSetupInImage(dataPath , img.rstrip('.jpg') + '.txt',currentimage)\n",
6068
    "    # Augment BBs and images.\n",
6069
    "    # As we only have one image and list of BBs, we use\n",
6070
    "    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your\n",
6071
    "    # variables would likely already be lists.\n",
6072
    "    image_aug = seq_det.augment_images([currentimage])[0]\n",
6073
    "    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]\n",
6074
    "    augImgFolder = current_operation + \"Images\"\n",
6075
    "    augTxTFolder = current_operation + \"TXT\"\n",
6076
    "    createFolder(augImgFolder)\n",
6077
    "    createFolder(augTxTFolder)\n",
6078
    "    # Save aug images and bboxes\n",
6079
    "    skimage.io.imsave(augImgFolder + '/'+\n",
6080
    "                      img.rstrip('.jpg') + \n",
6081
    "                      '_' + current_operation +\n",
6082
    "                      '.jpg'\n",
6083
    "                      ,image_aug)\n",
6084
    "    saveAugbbox2TXT(augTxTFolder+ '/'+ \n",
6085
    "                    img.rstrip('.jpg') + \n",
6086
    "                    '_'+ current_operation +\n",
6087
    "                    '.txt',bbs_aug)\n",
6088
    "    # image with BBs before/after augmentation (shown below)\n",
6089
    "    # image_before = bbs.draw_on_image(currentimage, thickness=2)\n",
6090
    "    # image_after = bbs_aug.draw_on_image(image_aug, \n",
6091
    "    # thickness=2, color=[0, 0, 255])\n",
6092
    "    # image with BBs before/after augmentation (shown below)\n",
6093
    "    # plot and save figures before and after data augmentations\n",
6094
    "    #skimage.io.imshow(image_before)\n",
6095
    "    #skimage.io.imshow(image_after)\n",
6096
    "    # for i in range(len(bbs.bounding_boxes)):\n",
6097
    "    #     before = bbs.bounding_boxes[i]\n",
6098
    "    #     after = bbs_aug.bounding_boxes[i]\n",
6099
    "    #     print(\"BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)\" % (\n",
6100
    "    #         i,\n",
6101
    "    #         before.x1, before.y1, before.x2, before.y2,\n",
6102
    "    #         after.x1, after.y1, after.x2, after.y2)\n",
6103
    "    #     )\n",
6104
    "\n"
6105
   ]
6106
  },
6107
  {
6108
   "cell_type": "heading",
6109
   "metadata": {},
6110
   "level": 1,
6111
   "source": [
6112
    "## End"
6113
   ]
6114
  },
6115
  {
6116
   "cell_type": "code",
6117
   "execution_count": 38,
6118
   "metadata": {},
6119
   "outputs": [],
6120
   "source": [
6121
    ""
6122
   ]
6123
  }
6124
 ],
6125
 "metadata": {
6126
  "kernelspec": {
6127
   "display_name": "Python 2",
6128
   "language": "python",
6129
   "name": "python2"
6130
  },
6131
  "language_info": {
6132
   "codemirror_mode": {
6133
    "name": "ipython",
6134
    "version": 2
6135
   },
6136
   "file_extension": ".py",
6137
   "mimetype": "text/x-python",
6138
   "name": "python",
6139
   "nbconvert_exporter": "python",
6140
   "pygments_lexer": "ipython2",
6141
   "version": "2.7.6"
6142
  }
6143
 },
6144
 "nbformat": 4,
6145
 "nbformat_minor": 0
6146
}