-
Notifications
You must be signed in to change notification settings - Fork 141
/
init.lua
2519 lines (2328 loc) · 82.3 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
----------------------------------------------------------------------
--
-- Copyright (c) 2011 Ronan Collobert, Clement Farabet
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
----------------------------------------------------------------------
-- description:
-- image - an image toolBox, for Torch
--
-- history:
-- July 1, 2011, 7:42PM - import from Torch5 - Clement Farabet
----------------------------------------------------------------------
require 'torch'
require 'xlua'
require 'dok'
require 'libimage'
local fpath = require 'sys.fpath'
local startswith = function(str, prefix)
return string.find(str, prefix, 1, true) == 1
end
local magicJPG = string.char(0xff, 0xd8, 0xff)
local magicPNG = string.char(0x89, 0x50, 0x4e, 0x47)
----------------------------------------------------------------------
-- include unit test function
--
require 'image.test'
----------------------------------------------------------------------
-- types lookups
--
local type2tensor = {
float = torch.FloatTensor(),
double = torch.DoubleTensor(),
byte = torch.ByteTensor(),
}
local template = function(type)
if type then
return type2tensor[type]
else
return torch.Tensor()
end
end
----------------------------------------------------------------------
-- save/load in multiple formats
--
-- depth convertion helper
local function todepth(img, depth)
if depth and depth == 1 then
if img:nDimension() == 2 then
-- all good
elseif img:size(1) == 3 or img:size(1) == 4 then
img = image.rgb2y(img:narrow(1,1,3))[1]
elseif img:size(1) == 2 then
img = img:narrow(1,1,1)
elseif img:size(1) ~= 1 then
dok.error('image loaded has wrong #channels', 'image.todepth')
end
elseif depth and depth == 3 then
local chan = img:size(1)
if chan == 3 then
-- all good
elseif img:nDimension() == 2 then
local imgrgb = img.new(3, img:size(1), img:size(2))
imgrgb:select(1, 1):copy(img)
imgrgb:select(1, 2):copy(img)
imgrgb:select(1, 3):copy(img)
img = imgrgb
elseif chan == 4 then
img = img:narrow(1,1,3)
elseif chan == 1 then
local imgrgb = img.new(3, img:size(2), img:size(3))
imgrgb:select(1, 1):copy(img)
imgrgb:select(1, 2):copy(img)
imgrgb:select(1, 3):copy(img)
img = imgrgb
else
dok.error('image loaded has wrong #channels', 'image.todepth')
end
end
return img
end
local function isPNG(magicTensor)
local pngMagic = torch.ByteTensor({0x89,0x50,0x4e,0x47})
return torch.all(torch.eq(magicTensor, pngMagic))
end
local function isJPG(magicTensor)
-- There are many valid 4th bytes, so only check the first 3 bytes.
-- libjpeg should support most if not all of these:
-- source: http://filesignatures.net/?page=all&order=SIGNATURE&alpha=J
local jpgMagic = torch.ByteTensor({0xff, 0xd8, 0xff})
return torch.all(torch.eq(magicTensor, jpgMagic))
end
local function decompress(tensor, depth, tensortype)
if torch.typename(tensor) ~= 'torch.ByteTensor' then
dok.error('Input tensor must be a byte tensor',
'image.decompress')
end
if tensor:nElement() < 4 then
dok.error('Input must be either jpg or png format',
'image.decompress')
end
if isJPG(tensor[{{1,3}}]) then
return image.decompressJPG(tensor, depth, tensortype)
elseif isPNG(tensor[{{1,4}}]) then
return image.decompressPNG(tensor, depth, tensortype)
else
dok.error('Input must be either jpg or png format',
'image.decompress')
end
end
rawset(image, 'decompress', decompress)
local function processPNG(img, depth, bit_depth, tensortype)
local MAXVAL = 255
if bit_depth == 16 then MAXVAL = 65535 end
if tensortype ~= 'byte' then
img:mul(1/MAXVAL)
end
img = todepth(img, depth)
return img
end
local function loadPNG(filename, depth, tensortype)
if not xlua.require 'liblua_png' then
dok.error('libpng package not found, please install libpng','image.loadPNG')
end
local load_from_file = 1
local a, bit_depth = template(tensortype).libpng.load(load_from_file, filename)
return processPNG(a, depth, bit_depth, tensortype)
end
rawset(image, 'loadPNG', loadPNG)
local function clampImage(tensor)
if tensor:type() == 'torch.ByteTensor' then
return tensor
end
local a = torch.Tensor():resize(tensor:size()):copy(tensor)
a.image.saturate(a) -- bound btwn 0 and 1
a:mul(255) -- remap to [0..255]
return a
end
local function savePNG(filename, tensor)
if not xlua.require 'liblua_png' then
dok.error('libpng package not found, please install libpng','image.savePNG')
end
tensor = clampImage(tensor)
local save_to_file = 1
tensor.libpng.save(filename, tensor, save_to_file)
end
rawset(image, 'savePNG', savePNG)
local function decompressPNG(tensor, depth, tensortype)
if not xlua.require 'liblua_png' then
dok.error('libpng package not found, please install libpng',
'image.decompressPNG')
end
if torch.typename(tensor) ~= 'torch.ByteTensor' then
dok.error('Input tensor (with compressed png) must be a byte tensor',
'image.decompressPNG')
end
local load_from_file = 0
local a, bit_depth = template(tensortype).libpng.load(load_from_file, tensor)
if a == nil then
return nil
else
return processPNG(a, depth, bit_depth, tensortype)
end
end
rawset(image, 'decompressPNG', decompressPNG)
function image.getPNGsize(filename)
if not xlua.require 'liblua_png' then
dok.error('libpng package not found, please install libpng','image.getPNGsize')
end
return torch.Tensor().libpng.size(filename)
end
local function compressPNG(tensor)
if not xlua.require 'liblua_png' then
dok.error('libpng package not found, please install libpng',
'image.compressPNG')
end
tensor = clampImage(tensor)
local b = torch.ByteTensor()
local save_to_file = 0
tensor.libpng.save("", tensor, save_to_file, b)
return b
end
rawset(image, 'compressPNG', compressPNG)
local function processJPG(img, depth, tensortype)
local MAXVAL = 255
if tensortype ~= 'byte' then
img:mul(1/MAXVAL)
end
img = todepth(img, depth)
return img
end
local function loadJPG(filename, depth, tensortype)
if not xlua.require 'libjpeg' then
dok.error('libjpeg package not found, please install libjpeg','image.loadJPG')
end
local load_from_file = 1
local a = template(tensortype).libjpeg.load(load_from_file, filename)
if a == nil then
return nil
else
return processJPG(a, depth, tensortype)
end
end
rawset(image, 'loadJPG', loadJPG)
local function decompressJPG(tensor, depth, tensortype)
if not xlua.require 'libjpeg' then
dok.error('libjpeg package not found, please install libjpeg',
'image.decompressJPG')
end
if torch.typename(tensor) ~= 'torch.ByteTensor' then
dok.error('Input tensor (with compressed jpeg) must be a byte tensor',
'image.decompressJPG')
end
local load_from_file = 0
local a = template(tensortype).libjpeg.load(load_from_file, tensor)
if a == nil then
return nil
else
return processJPG(a, depth, tensortype)
end
end
rawset(image, 'decompressJPG', decompressJPG)
local function saveJPG(filename, tensor)
if not xlua.require 'libjpeg' then
dok.error('libjpeg package not found, please install libjpeg','image.saveJPG')
end
tensor = clampImage(tensor)
local save_to_file = 1
local quality = 75
tensor.libjpeg.save(filename, tensor, save_to_file, quality)
end
rawset(image, 'saveJPG', saveJPG)
function image.getJPGsize(filename)
if not xlua.require 'libjpeg' then
dok.error('libjpeg package not found, please install libjpeg','image.getJPGsize')
end
return torch.Tensor().libjpeg.size(filename)
end
local function compressJPG(tensor, quality)
if not xlua.require 'libjpeg' then
dok.error('libjpeg package not found, please install libjpeg',
'image.compressJPG')
end
tensor = clampImage(tensor)
local b = torch.ByteTensor()
local save_to_file = 0
quality = quality or 75
tensor.libjpeg.save("", tensor, save_to_file, quality, b)
return b
end
rawset(image, 'compressJPG', compressJPG)
local function loadPPM(filename, depth, tensortype)
require 'libppm'
local MAXVAL = 255
local a = template(tensortype).libppm.load(filename)
if tensortype ~= 'byte' then
a:mul(1/MAXVAL)
end
a = todepth(a, depth)
return a
end
rawset(image, 'loadPPM', loadPPM)
local function savePPM(filename, tensor)
require 'libppm'
if tensor:nDimension() ~= 3 or tensor:size(1) ~= 3 then
dok.error('can only save 3xHxW images as PPM', 'image.savePPM')
end
tensor = clampImage(tensor)
tensor.libppm.save(filename, tensor)
end
rawset(image, 'savePPM', savePPM)
local function savePGM(filename, tensor)
require 'libppm'
if tensor:nDimension() == 3 and tensor:size(1) ~= 1 then
dok.error('can only save 1xHxW or HxW images as PGM', 'image.savePGM')
end
tensor = clampImage(tensor)
tensor.libppm.save(filename, tensor)
end
rawset(image, 'savePGM', savePGM)
function image.getPPMsize(filename)
require 'libppm'
return torch.Tensor().libppm.size(filename)
end
local filetypes = {
jpg = {loader = image.loadJPG, saver = image.saveJPG},
png = {loader = image.loadPNG, saver = image.savePNG},
ppm = {loader = image.loadPPM, saver = image.savePPM},
-- yes, loadPPM not loadPGM
pgm = {loader = image.loadPPM, saver = image.savePGM}
}
filetypes['JPG'] = filetypes['jpg']
filetypes['JPEG'] = filetypes['jpg']
filetypes['jpeg'] = filetypes['jpg']
filetypes['PNG'] = filetypes['png']
filetypes['PPM'] = filetypes['ppm']
filetypes['PGM'] = filetypes['pgm']
rawset(image, 'supported_filetypes', filetypes)
local function is_supported(suffix)
return filetypes[suffix] ~= nil
end
rawset(image, 'is_supported', is_supported)
local function load(filename, depth, tensortype)
if not filename then
print(dok.usage('image.load',
'loads an image into a torch.Tensor', nil,
{type='string', help='path to file', req=true},
{type='number', help='force destination depth: 1 | 3'},
{type='string', help='type: byte | float | double'}))
dok.error('missing file name', 'image.load')
end
local ext
local f, err = io.open(filename, 'rb')
if not f then
error(err)
end
local hdr = f:read(4) or ''
f:close()
if startswith(hdr, magicJPG) then
ext = 'jpg'
elseif startswith(hdr, magicPNG) then
ext = 'png'
elseif hdr:match('^P[25]') then
ext = 'pgm'
elseif hdr:match('^P[36]') then
ext = 'ppm'
end
if not ext then
ext = string.match(filename,'%.(%a+)$')
end
local tensor
if image.is_supported(ext) then
tensor = filetypes[ext].loader(filename, depth, tensortype)
elseif not ext then
dok.error('unable to determine image type for file: ' .. filename, 'image.load')
else
dok.error('unknown image type: ' .. ext, 'image.load')
end
return tensor
end
rawset(image, 'load', load)
filetypes.jpg.sizer = image.getJPGsize
filetypes.png.sizer = image.getPNGsize
filetypes.ppm.sizer = image.getPPMsize
filetypes.pgm.sizer = image.getPPMsize -- sim. to loadPPM not loadPGM
local function getSize(filename)
if not filename then
print(dok.usage('image.getSize',
'returns size of image without loading', nil,
{type='string', help='path to file', req=true}))
dok.error('missing file name', 'image.getSize')
end
local ext
local f, err = io.open(filename, 'rb')
if not f then
error(err)
end
local hdr = f:read(4) or ''
f:close()
if startswith(hdr, magicJPG) then
ext = 'jpg'
elseif startswith(hdr, magicPNG) then
ext = 'png'
elseif hdr:match('^P[25]') then
ext = 'pgm'
elseif hdr:match('^P[36]') then
ext = 'ppm'
end
if not ext then
ext = string.match(filename,'%.(%a+)$')
end
local size
if image.is_supported(ext) then
size = {filetypes[ext].sizer(filename)}
elseif not ext then
dok.error('unable to determine image type for file: ' .. filename, 'image.getSize')
else
dok.error('unknown image type: ' .. ext, 'image.load')
end
return torch.LongTensor(size)
end
rawset(image, 'getSize', getSize)
local function save(filename, tensor)
if not filename or not tensor then
print(dok.usage('image.save',
'saves a torch.Tensor to a disk', nil,
{type='string', help='path to file', req=true},
{type='torch.Tensor', help='tensor to save (NxHxW, N = 1 | 3)'}))
dok.error('missing file name | tensor to save', 'image.save')
end
local ext = string.match(filename,'%.(%a+)$')
if image.is_supported(ext) then
tensor = filetypes[ext].saver(filename, tensor)
else
dok.error('unknown image type: ' .. ext, 'image.save')
end
end
rawset(image, 'save', save)
----------------------------------------------------------------------
-- crop
--
local function crop(...)
local dst,src,startx,starty,endx,endy
local format,width,height
local args = {...}
if select('#',...) == 6 then
dst = args[1]
src = args[2]
startx = args[3]
starty = args[4]
endx = args[5]
endy = args[6]
elseif select('#',...) == 5 then
if type(args[3]) == 'string' then
dst = args[1]
src = args[2]
format = args[3]
width = args[4]
height = args[5]
else
src = args[1]
startx = args[2]
starty = args[3]
endx = args[4]
endy = args[5]
end
elseif select('#',...) == 4 then
if type(args[2]) == 'string' then
src = args[1]
format = args[2]
width = args[3]
height = args[4]
else
dst = args[1]
src = args[2]
startx = args[3]
starty = args[4]
end
elseif select('#',...) == 3 then
src = args[1]
startx = args[2]
starty = args[3]
else
print(dok.usage('image.crop',
'crop an image', nil,
{type='torch.Tensor', help='input image', req=true},
{type='number', help='start x', req=true},
{type='number', help='start y', req=true},
{type='number', help='end x'},
{type='number', help='end y'},
'',
{type='torch.Tensor', help='destination', req=true},
{type='torch.Tensor', help='input image', req=true},
{type='number', help='start x', req=true},
{type='number', help='start y', req=true},
{type='number', help='end x'},
{type='number', help='end y'},
'',
{type='torch.Tensor', help='input image', req=true},
{type='string', help='format: "c" or "tl" or "tr" or "bl" or "br"', req=true},
{type='number', help='width', req=true},
{type='number', help='height', req=true},
'',
{type='torch.Tensor', help='destination', req=true},
{type='torch.Tensor', help='input image', req=true},
{type='string', help='format: "c" or "tl" or "tr" or "bl" or "br"', req=true},
{type='number', help='width', req=true},
{type='number', help='height', req=true}))
dok.error('incorrect arguments', 'image.crop')
end
if format then
local iwidth,iheight
if src:nDimension() == 3 then
iwidth,iheight = src:size(3),src:size(2)
else
iwidth,iheight = src:size(2),src:size(1)
end
local x1, y1
if format == 'c' then
x1, y1 = math.floor((iwidth-width)/2), math.floor((iheight-height)/2)
elseif format == 'tl' then
x1, y1 = 0, 0
elseif format == 'tr' then
x1, y1 = iwidth-width, 0
elseif format == 'bl' then
x1, y1 = 0, iheight-height
elseif format == 'br' then
x1, y1 = iwidth-width, iheight-height
else
error('crop format must be "c"|"tl"|"tr"|"bl"|"br"')
end
return crop(dst, src, x1, y1, x1+width, y1+height)
end
if endx==nil then
return src.image.cropNoScale(src,dst,startx,starty)
else
local depth=1
local x
if src:nDimension() > 2 then
x = src.new(src:size(1),endy-starty,endx-startx)
else
x = src.new(endy-starty,endx-startx)
end
src.image.cropNoScale(src,x,startx,starty)
if dst then
image.scale(dst, x)
else
dst = x
end
end
return dst
end
rawset(image, 'crop', crop)
----------------------------------------------------------------------
-- translate
--
local function translate(...)
local dst,src,x,y
local args = {...}
if select('#',...) == 4 then
dst = args[1]
src = args[2]
x = args[3]
y = args[4]
elseif select('#',...) == 3 then
src = args[1]
x = args[2]
y = args[3]
else
print(dok.usage('image.translate',
'translate an image', nil,
{type='torch.Tensor', help='input image', req=true},
{type='number', help='horizontal translation', req=true},
{type='number', help='vertical translation', req=true},
'',
{type='torch.Tensor', help='destination', req=true},
{type='torch.Tensor', help='input image', req=true},
{type='number', help='horizontal translation', req=true},
{type='number', help='vertical translation', req=true}))
dok.error('incorrect arguments', 'image.translate')
end
dst = dst or src.new()
dst:resizeAs(src)
dst:zero()
src.image.translate(src,dst,x,y)
return dst
end
rawset(image, 'translate', translate)
----------------------------------------------------------------------
-- scale
--
local function scale(...)
local dst,src,width,height,mode,size
local args = {...}
if select('#',...) == 4 then
src = args[1]
width = args[2]
height = args[3]
mode = args[4]
elseif select('#',...) == 3 then
if type(args[3]) == 'string' then
if type(args[2]) == 'string' or type(args[2]) == 'number' then
src = args[1]
size = args[2]
mode = args[3]
else
dst = args[1]
src = args[2]
mode = args[3]
end
else
src = args[1]
width = args[2]
height = args[3]
end
elseif select('#',...) == 2 then
if type(args[2]) == 'string' or type(args[2]) == 'number' then
src = args[1]
size = args[2]
else
dst = args[1]
src = args[2]
end
else
print(dok.usage('image.scale',
'rescale an image (geometry)', nil,
{type='torch.Tensor', help='input image', req=true},
{type='number', help='destination width', req=true},
{type='number', help='destination height', req=true},
{type='string', help='mode: bilinear | bicubic |simple', default='bilinear'},
'',
{type='torch.Tensor', help='input image', req=true},
{type='string | number', help='destination size: "WxH" or "MAX" or "^MIN" or "*SC" or "*SCd/SCn" or MAX', req=true},
{type='string', help='mode: bilinear | bicubic | simple', default='bilinear'},
'',
{type='torch.Tensor', help='destination image', req=true},
{type='torch.Tensor', help='input image', req=true},
{type='string', help='mode: bilinear | bicubic | simple', default='bilinear'}))
dok.error('incorrect arguments', 'image.scale')
end
if size then
local iwidth, iheight
if src:nDimension() == 3 then
iwidth, iheight = src:size(3),src:size(2)
else
iwidth, iheight = src:size(2),src:size(1)
end
-- MAX?
local imax = math.max(iwidth, iheight)
local omax = tonumber(size)
if omax then
height = iheight*omax/imax
width = iwidth*omax/imax
end
-- WxH?
if not width or not height then
width, height = size:match('(%d+)x(%d+)')
end
-- ^MIN?
if not width or not height then
local imin = math.min(iwidth, iheight)
local omin = tonumber(size:match('%^(%d+)'))
if omin then
height = iheight*omin/imin
width = iwidth*omin/imin
end
end
-- *SCn/SCd?
if not width or not height then
local scn, scd = size:match('%*(%d+)%/(%d+)')
if scn and scd then
height = iheight*scn/scd
width = iwidth*scn/scd
end
end
-- *SC?
if not width or not height then
local sc = tonumber(size:match('%*(.+)'))
if sc then
height = iheight*sc
width = iwidth*sc
end
end
end
if not dst and (not width or not height) then
dok.error('could not find valid dest size', 'image.scale')
end
if not dst then
height = math.max(height, 1)
width = math.max(width, 1)
if src:nDimension() == 3 then
dst = src.new(src:size(1), height, width)
else
dst = src.new(height, width)
end
end
mode = mode or 'bilinear'
if mode=='bilinear' then
src.image.scaleBilinear(src,dst)
elseif mode=='bicubic' then
src.image.scaleBicubic(src,dst)
elseif mode=='simple' then
src.image.scaleSimple(src,dst)
else
dok.error('mode must be one of: simple | bicubic | bilinear', 'image.scale')
end
return dst
end
rawset(image, 'scale', scale)
----------------------------------------------------------------------
-- rotate
--
local function rotate(...)
local dst,src,theta, mode
local args = {...}
if select('#',...) == 4 then
dst = args[1]
src = args[2]
theta = args[3]
mode = args[4]
elseif select('#',...) == 3 then
if type(args[2]) == 'number' then
src = args[1]
theta = args[2]
mode = args[3]
else
dst = args[1]
src = args[2]
theta = args[3]
end
elseif select('#',...) == 2 then
src = args[1]
theta = args[2]
else
print(dok.usage('image.rotate',
'rotate an image by theta radians', nil,
{type='torch.Tensor', help='input image', req=true},
{type='number', help='rotation angle (in radians)', req=true},
{type='string', help='mode: simple | bilinear', default='simple'},
'',
{type='torch.Tensor', help='destination', req=true},
{type='torch.Tensor', help='input image', req=true},
{type='number', help='rotation angle (in radians)', req=true},
{type='string', help='mode: simple | bilinear', default='simple'}))
dok.error('incorrect arguments', 'image.rotate')
end
dst = dst or src.new()
dst:resizeAs(src)
mode = mode or 'simple'
if mode == 'simple' then
src.image.rotate(src,dst,theta)
elseif mode == 'bilinear' then
src.image.rotateBilinear(src,dst,theta)
else
dok.error('mode must be one of: simple | bilinear', 'image.rotate')
end
return dst
end
rawset(image, 'rotate', rotate)
----------------------------------------------------------------------
-- polar
--
local function polar(...)
local dst,src,interp,mode
local args = {...}
if select('#',...) == 4 then
dst = args[1]
src = args[2]
interp = args[3]
mode = args[4]
elseif select('#',...) == 3 then
if type(args[2]) == 'string' then
src = args[1]
interp = args[2]
mode = args[3]
else
dst = args[1]
src = args[2]
interp = args[3]
end
elseif select('#',...) == 2 then
if type(args[2]) == 'string' then
src = args[1]
interp = args[2]
else
dst = args[1]
src = args[2]
end
elseif select('#',...) == 1 then
src = args[1]
else
print(dok.usage('image.polar',
'convert an image to polar coordinates', nil,
{type='torch.Tensor', help='input image', req=true},
{type='string', help='interpolation: simple | bilinear', default='simple'},
{type='string', help='mode: valid | full', default='valid'},
'',
{type='torch.Tensor', help='destination', req=true},
{type='torch.Tensor', help='input image', req=true},
{type='string', help='interpolation: simple | bilinear', default='simple'},
{type='string', help='mode: valid | full', default='valid'}))
dok.error('incorrect arguments', 'image.polar')
end
interp = interp or 'valid'
mode = mode or 'simple'
if dst == nil then
local maxDist = math.floor(math.max(src:size(2), src:size(3)))
dst = src.new()
dst:resize(src:size(1), maxDist, maxDist)
end
if interp == 'simple' then
if mode == 'full' then
src.image.polar(src,dst,1)
elseif mode == 'valid' then
src.image.polar(src,dst,0)
else
dok.error('mode must be one of: valid | full', 'image.polar')
end
elseif interp == 'bilinear' then
if mode == 'full' then
src.image.polarBilinear(src,dst,1)
elseif mode == 'valid' then
src.image.polarBilinear(src,dst,0)
else
dok.error('mode must be one of: valid | full', 'image.polar')
end
else
dok.error('interpolation must be one of: simple | bilinear', 'image.polar')
end
return dst
end
rawset(image, 'polar', polar)
----------------------------------------------------------------------
-- logpolar
--
local function logpolar(...)
local dst,src,interp,mode
local args = {...}
if select('#',...) == 4 then
dst = args[1]
src = args[2]
interp = args[3]
mode = args[4]
elseif select('#',...) == 3 then
if type(args[2]) == 'string' then
src = args[1]
interp = args[2]
mode = args[3]
else
dst = args[1]
src = args[2]
interp = args[3]
end
elseif select('#',...) == 2 then
if type(args[2]) == 'string' then
src = args[1]
interp = args[2]
else
dst = args[1]
src = args[2]
end
elseif select('#',...) == 1 then
src = args[1]
else
print(dok.usage('image.logpolar',
'convert an image to log-polar coordinates', nil,
{type='torch.Tensor', help='input image', req=true},
{type='string', help='interpolation: simple | bilinear', default='simple'},
{type='string', help='mode: valid | full', default='valid'},
'',
{type='torch.Tensor', help='destination', req=true},
{type='torch.Tensor', help='input image', req=true},
{type='string', help='interpolation: simple | bilinear', default='simple'},
{type='string', help='mode: valid | full', default='valid'}))
dok.error('incorrect arguments', 'image.polar')
end
interp = interp or 'valid'
mode = mode or 'simple'
if dst == nil then
local maxDist = math.floor(math.max(src:size(2), src:size(3)))
dst = src.new()
dst:resize(src:size(1), maxDist, maxDist)
end
if interp == 'simple' then
if mode == 'full' then
src.image.logPolar(src,dst,1)
elseif mode == 'valid' then
src.image.logPolar(src,dst,0)
else
dok.error('mode must be one of: valid | full', 'image.logpolar')
end
elseif interp == 'bilinear' then
if mode == 'full' then
src.image.logPolarBilinear(src,dst,1)
elseif mode == 'valid' then
src.image.logPolarBilinear(src,dst,0)
else
dok.error('mode must be one of: valid | full', 'image.logpolar')
end
else
dok.error('interpolation must be one of: simple | bilinear', 'image.logpolar')
end
return dst
end
rawset(image, 'logpolar', logpolar)
----------------------------------------------------------------------
-- warp
--
local function warp(...)
local dst,src,field
local mode = 'bilinear'
local offset_mode = true
local clamp_mode = 'clamp'
local pad_value = 0
local args = {...}
local nargs = select('#',...)
local bad_args = false
if nargs == 2 then
src = args[1]
field = args[2]
elseif nargs >= 3 then
if type(args[3]) == 'string' then
-- No destination tensor
src = args[1]
field = args[2]
mode = args[3]
if nargs >= 4 then offset_mode = args[4] end
if nargs >= 5 then clamp_mode = args[5] end
if nargs >= 6 then
assert(clamp_mode == 'pad', 'pad_value can only be specified if' ..
' clamp_mode = "pad"')
pad_value = args[6]
end
if nargs >= 7 then bad_args = true end
else
-- With Destination tensor
dst = args[1]
src = args[2]
field = args[3]
if nargs >= 4 then mode = args[4] end
if nargs >= 5 then offset_mode = args[5] end
if nargs >= 6 then clamp_mode = args[6] end
if nargs >= 7 then
assert(clamp_mode == 'pad', 'pad_value can only be specified if' ..
' clamp_mode = "pad"')
pad_value = args[7]
end
if nargs >= 8 then bad_args = true end
end
end
if bad_args then
print(dok.usage('image.warp',
'warp an image, according to a flow field', nil,
{type='torch.Tensor', help='input image (KxHxW)', req=true},
{type='torch.Tensor', help='(y,x) flow field (2xHxW)', req=true},
{type='string', help='mode: lanczos | bicubic | bilinear | simple', default='bilinear'},
{type='string', help='offset mode (add (x,y) to flow field)', default=true},
{type='string', help='clamp mode: how to handle interp of samples off the input image (clamp | pad)', default='clamp'},
'',
{type='torch.Tensor', help='destination', req=true},
{type='torch.Tensor', help='input image (KxHxW)', req=true},
{type='torch.Tensor', help='(y,x) flow field (2xHxW)', req=true},
{type='string', help='mode: lanczos | bicubic | bilinear | simple', default='bilinear'},
{type='string', help='offset mode (add (x,y) to flow field)', default=true},