-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtex_synthesize.py
1487 lines (1278 loc) · 58.5 KB
/
tex_synthesize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 5 13:39:03 2020
################################################################################
Copyright (C) 2020 Thomas Meschede a.k.a. yeus
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
################################################################################
@author: Thomas Meschede a.k.a. yeus ([email protected])
This texture synthesis algorithm takes inspiration from three papers and
combines their papers into a new algorithm:
- Image Quilting for Texture Synthesis and Transfer [Efros, Freeman]
- taking the optimal-patch seam strategy and
- Fast Texture Synthesis using Tree-structured Vector Quantization [Wei, Levoy]
- iterations, non-causal buildup local neighbourhood search
- Real-Time Texture Synthesis by Patch-Based Sampling [Liang et al.]
- building a gaussian image pyramid combined with KD-Trees for
fast searches
"""
import random
import numpy as np
import skimage
import skimage.io
import skimage.transform
import itertools
from functools import wraps
import time
#from pynndescent import NNDescent
#import gc
import math
import os, sys
import functools
sign = functools.partial(math.copysign, 1) # either of these
#import scipy
import shapely
import shapely.geometry
import logging
#import psutil #TODO: not easy in blender, because of a lacking Python.h
logger = logging.getLogger(__name__)
#ann_library = "pynndescent"
ann_library = "sklearn"
use_pynnd, use_sklearn=False,False
if ann_library=="pynndescent":
import pynndescent as pynnd
use_pynnd=True
elif ann_library=='sklearn':
import sklearn
import sklearn.neighbors
use_sklearn=True
#def norm(x): return np.sqrt(x.dot(x))
def norm(x): return np.sqrt((x*x).sum(-1))
#need to be transposed for correct ultiplcation along axis 1
def normalized(x): return (x.T /norm(x)).T
def calc_angle_vec(u, v):
"""
>>> u = vec((1.0,1.0,0.0))
>>> v = vec((1.0,0.0,0.0))
>>> calc_angle_vec(u,v)*rad
45.00000000000001
>>> u = vec((1.0,0.0,0.0))
>>> v = vec((-1.0,0.0,0.0))
>>> calc_angle_vec(u,v)*rad
180.0
>>> u = vec([-9.38963669e-01, 3.44016319e-01, 1.38777878e-17])
>>> v = vec([-0.93896367, 0.34401632, 0.])
>>> u @ v / (norm(v)*norm(u))
1.0000000000000002
>>> calc_angle_vec(u,v)*rad
0.0
"""
#angle = np.arctan2(norm(np.cross(u,v)), np.dot(u,v))
res = np.sum(u*v) / (norm(u) * norm(v))
t = np.clip(res,-1.0,1.0)
angle = np.arccos(t)
return angle
#consider replacing sklearn KDTree with scipy KDTree
#https://jakevdp.github.io/blog/2013/04/29/benchmarking-nearest-neighbor-searches-in-python/
#from tqdm import tqdm
def tqdm(iterator, *args, **kwargs):
return iterator
GB = 1.0/1024**3 #GB factor
def timing(f):
@wraps(f)
def wrap(*args, **kw):
ts = time.time()
result = f(*args, **kw)
te = time.time()
logger.info(f'func:{f.__name__} took: {te-ts:2.4f} sec')
return result
return wrap
@timing
def init_ann_index(data):
"""
TODO: parameterize quality of ANN search
"""
#metric='euclidean'
metric='manhattan'
if use_pynnd:
index = pynnd.NNDescent(data,metric,
n_neighbors=3,#steers the quality of the algorithm
n_jobs=-1 #-1: use all processors
)
elif use_sklearn:
index = sklearn.neighbors.KDTree(data, metric=metric)#'l2'
return index
def query_index(index, data, k):
if use_pynnd:
idx, e = index.query([data], k=k)
return e,idx[0]
elif sklearn:
e, idx = index.query([data], k=k)
e, idx = e.flatten(), idx.flatten()
return e,idx
def get_mem_limit(factor=0.5):
#stats = psutil.virtual_memory() # returns a named tuple
#avilable_memory = getattr(stats, 'available')/1024**3 # available memory in GB
#mem_limit = avilable_memory*factor
#tot_m, used_m, free_m = map(int, os.popen('free -t -m').readlines()[-1].split()[1:])
if sys.platform=='linux':
free_m = int(os.popen('free -t -m').readlines()[1].split()[-1])
else: #TODO: include windows/mac
free_m = 2000 #MB
return free_m * factor / 1024 #in GB
def copy_img(target, src, pos=(0,0), mask=None):
"""
copy image src to target at pos
careful! x & y are switched around here (normal order) in contrast to other
functions of this library. order: pos=(x,y)
"""
#TODO: handle border clipping problems
# - when copying images that exten over "left" and "top" edges
sh,sw,sch = src.shape
th,tw,tch = target.shape
i0x = np.clip(pos[0],0,tw)
i0y = np.clip(pos[1],0,th)
i1x = np.clip(pos[0]+sw,0,tw)
i1y = np.clip(pos[1]+sh,0,th)
#cut source patch to right size
pw, ph = max(i1x - i0x,0), max(i1y - i0y,0)
if mask is None:
tch = sch
#print(pos)
#import ipdb; ipdb.set_trace() # BREAKPOINT
target[i0y:i1y, i0x:i1x, 0:tch] = src[0:ph, 0:pw]
else:
m = mask
target[i0y:i1y, i0x:i1x, 0:tch][m] = src[m]
return target
def mask_blend(m, img1, img2):
"""
this blends two images by blending them using a mask
"""
mT = np.expand_dims(m,axis=2)
b1 = img1 * (1 - mT)
b2 = img2 * mT
new_img = b1+b2
return new_img
@timing
def transform_patch_grid_to_tex(res, res_patch, pg, example,
overlap,
use_quilting=True):
"""
synthesize texture from pre-calculated grid
overlap = (horizontal_ovelap, vertical_overlap)
"""
#TODO: create a generate- "info" function so that information doesn't have
# to be exchanged so often
#TODO: adaptive overlap (wih larger patch sizes it might not
#make enough sense anymore, because textures become too repetetive ...)
#overlap = np.ceil((res_patch/6)).astype(int)
#TODO: the overlap can actually be different from the overlap when searching
# for the images. This might make some things more efficient
# or we can just generate a standad overlap in all functions
ch_num = example.shape[-1]
#def draw_patch_grid():
rpg = np.array(res_patch) - overlap
if res is None: res = rpg * pg.shape[:2] + overlap
target = np.zeros((res[0],res[1],ch_num), dtype = example.dtype)
ta_map = np.zeros((res[0],res[1],3))
target_map_patch_base = gen_coordinate_map(res_patch)
onemask = np.ones(res_patch)
for iy,ix in np.ndindex(pg.shape[:2]):
#if (ix, iy) == (3,2): break
x = ix * rpg[1]
y = iy * rpg[0]
y0,x0 = pa_coords = pg[iy,ix] #get coords of patch in example texture
#TODO: searching new patches based on the already existing image also
# helps when having all sorts of constraints
#TODO: maybe pass a "constraint-error-function" for the search function?
#TODO: get rid of resolution parameters to make dynamic sized patches
# possible --> this doesn#t work with a "grid" function
if all(pa_coords == (-1,-1)): # --> a part of the grid that is not assigned
pa = np.zeros((res_patch[0],res_patch[1],4))
pa[:,:,:]=(0, 0.31, 0.22, 1) #TODO: make different fill colors possible
#get corresponding overlaps:
if iy==0: ovs=(overlap[1],0,0,0) #first row
elif ix==0: ovs=(0,overlap[0],0,0) #first column
else: ovs=(overlap[1],overlap[0],0,0) #rest of the grid
if (iy==0 and ix==0) or (not use_quilting):
pa = example[y0:y0+res_patch[0],x0:x0+res_patch[1]].copy()
mask = onemask
else:
ta_patch = target[y:y+res_patch[0],x:x+res_patch[1]]
pa, _, _, mask = optimal_patch(ta_patch, example,
res_patch, ovs, (y0,x0), (y,x))
#skimage.io.imshow_collection([pa, ov_h[0], b_h, ov_v[0], b_v])
copy_img(target, pa, (x,y))
#print((ix,iy),pg[iy,ix])
ta_map_patch = target_map_patch_base + (x0,y0,0)
#TODO: find a better method how to use "partial" coordinate transfer
#or in other words: "mixes" which appear for example at smoothed
#out and blended optimal borders
copy_img(ta_map, ta_map_patch, (x,y), mask=mask>0)
#copy_img(ta_map, ta_map_patch, (x,y))
return target, ta_map
def overlap_slices(overlap):
"""define slices for overlaps"""
ovs = [np.s_[:,:overlap[0]],#left
np.s_[:overlap[1],:],#top
np.s_[:,-overlap[2]:],#right
np.s_[-overlap[3]:,:]]#bottom
return ovs
def create_optimal_patch(pa, ta, overlaps):
"""
this function creates an optimal patch out of 1 tile with
given number of border tiles where two overlapping regions
are replaced by an optimal boundary from the other patches
"""
mask = np.ones(pa.shape[:2])
ovs = overlap_slices(overlaps)
for ov, orient, sl, mirrored in zip(overlaps,["h","v","h","v"], ovs,
[False,False,True,True]):
if ov>0: #if this boundary is used
m = minimum_error_boundary_cut((pa[sl],ta[sl]), orient)
if mirrored: m = 1-m
mask[sl] = np.minimum(m,mask[sl])
new_pa = mask_blend(mask, ta, pa)
#TODO: only return mask
return new_pa, mask
def optimal_patch(ta_patch, example, res_patch, overlap, pos_ex, pos_ta):
"""
this creates optimal patches for reacangular patches with overlaps
given in "overlap" with indics as follows:
2
-----
1| |3
-----
4
the number of in "overlap" specifies the size of the overlap in pixels
TODO: expand to arbitrarily sized boundaries
TODO: make it possible to "move" the source patch a couple pixels
to find a better fit
"""
#TODO use np.s_ as indices
y,x = pos_ta
y0,x0 = pos_ex
pa = example[y0:y0+res_patch[0],x0:x0+res_patch[1]].copy()
optimpatch, mask = create_optimal_patch(pa, ta_patch, overlap)
return optimpatch, pa, ta_patch, mask
def find_match(data, index, tol = 0.1, k=5):
#get a horizonal overlap match
e,idx = query_index(index,data,k)
#e,idx = query_index(index,data,k)
#TODO: keep an index with "weights" to make sure
# that patches are selected equally oftern OR according
# to a predefined distribution
# TODO: make sure patches can get selected with an additional error-term
# TODO: for very large textures it might make sense
# to only query a close radius
# TODO: implement theshold function
#or choose ithin a certain tolerance
if k>1:
min_err = e[e>0].min() #find minimum error except for the exact matching pic
#i2 = err.argmin()
th = min_err * (1+tol) #threshold
idx = [i for i,e in zip(idx,e) if e<th]
return random.choice(idx)
else:
return idx[0]
#if len(idx)>1: print(f"{len(idx)},",)
@timing
def synthesize_grid(example, res_patch, res_grid, overlap, tol = 0.1, k=5):
"""
synthesize a grid of patch indices
by searching match pairs & triples in the
overlaps database
tolerance -> this basically controls the randomness of
the algorithm
"""
res_patch = np.array(res_patch).astype(int)
res_ex = example.shape[:2]
#TODO: decrease overlap to mitigate memory_problems
max_co = res_ex - res_patch
ch = example.shape[-1]
cellsize = example.itemsize
#TODO: replace by a better KD Tree implementation or a Ball Tree
cellsize = 8 #bytes (this is because of scikit-learns KDTree implementation
# which will convert the supplied data into a float
# representation)
#ex_img_ratio = res_ex[0]/res_ex[1]
#calculate maximum possible patch size for given number of patches and
rp = res_patch
pnum = max_co.prod()
mem_limit = get_mem_limit()
#check memory consumption for overlap database:
#TODO: check memory consumption instructions from sklearn webpage
#single_overlap_memory_horizontal = \
# overlap[0] * res_patch[1] * ch * cellsize #in byte
#single_overlap_memory_vertical = \
# overlap[1] * res_patch[0] * ch * cellsize #in byte
#factor 2 comes in here, because we have a 3rd database with the combined overlaps
#totalmemoryGB = pnum * GB \
# * (2 * single_overlap_memory_vertical + 2 * single_overlap_memory_horizontal)
#augmentation_multiplicator = 2 #(for mirroring, and rotating, the data gets
#) multiplied
data_memoryGB = check_memory_requirements(example,res_patch, maxmem=mem_limit,
disable_safety_check=True)
logger.info(f"using approx. {3*data_memoryGB:2f} GB in RAM.")
#TODO: build my own search algorithm which doesn't consume as much memory
# and can find things in an image much quicker
#TODO: the "overlaps" are the same for left & right and top & bottom
# patch generation can be completly left out as they can be
# taken directly from the texture. For the search, only the
# overlaps are important.
#TODO: maybe save the coordinates of the overlaps in a second database
#TODO: augmented patches(mirrored, )
#TODO: check memory consumption of patch overlaps
#TODO: if memory consumption reaches a certain thresold, lower resolution
# of overlap databases (this can be done in multiple ways:
# - only take patches from every nth pixel,
# - take overlaps with 1/k the resolution of the original in the database)
# - use smaller patch sizes
# - lower the reoslution of the original image but later keep the
# original rsolution when stitching it back together
# - take a random sample of patches from the original source image
# TODO: add patch augmentation mirroring, rotaton
# the problem here is: when augmenting horiz. or vert.
# images, the "combined" would becom very large
# basically squared. so if we mirror vert. overlaps,
# only horizontally, we also have twice as many combined.
# If we mirror vertically & horozontally we have 3 times as
# many (all data).
# If we also mirror horiz. overlaps in th same way we have
# 3x3 = 9x as much data in combined and 3+3=6 times as much in
# vert. or horiz.
# we can find out whether something is mirrored, rotated or
# whatever by analyzing the index module by the number of
# augentations
#lm = []
try:
logger.info("init kdtree1")
ld = create_patch_data(example, (rp[0], overlap[1]), max_co)
l = init_ann_index(ld)
logger.info("init kdtree2")
td = create_patch_data(example, (overlap[0],rp[1]), max_co)
t = init_ann_index(td)
logger.info("init kdtree3")
lt = init_ann_index(np.hstack((ld,td)))
#TODO: check memory consumption of KDTrees
#sklearn.neighbors.KDTree.valid_metrics
#ov_db = [sklearn.neighbors.KDTree(i, metric='euclidean') for i in (ov_l, ov_t, ov_lt)]
logger.info("KD-Tree initialization done")
except MemoryError as err:
logger.info(err)
logger.info("example texture too large, algorithm needs "
"too much RAM: {totalmemoryGB:.2f}GB")
raise
pg = np.full(shape=(*res_grid,2), fill_value=-1, dtype=int)
pg[0,0] = idx2co(random.randrange(pnum), max_co) #get first patch
#pg[0,0]=(0,0)
#TODO: synthesize an arbitrary pic with the overlap database generated from example
# but using a different source texture to "mix" two textures
for i in tqdm(range(1,res_grid[1]),"find matches: first row"):
y,x = pg[0,i-1]
#patch = example[y:y+rp[1],x:x+rp[0]]
#((patches[idx] - patch)**2).sum() == 0 #they have to be the same!
ovl = example[y:y+rp[0],x:x+rp[1]][:,-overlap[0]:].flatten()
#ov_idx = idx + (res_patch[0] - overlap[0])*max_co[1]#move towards the right by res_grid
#(ovl-ov_l[ov_idx])
new_idx = find_match(ovl, l , tol=tol, k=k)
#if new_idx
pg[0,i] = idx2co(new_idx, max_co)
for i in tqdm(range(1,res_grid[0]),"find matches: first column"):
y,x = pg[i-1,0]
ovt = example[y:y+rp[1],x:x+rp[0]][-overlap[1]:,:].flatten()
pg[i,0] = idx2co(find_match(ovt, t, tol=tol, k=k), max_co)
for ix,iy in tqdm(itertools.product(range(1,res_grid[1]),
range(1,res_grid[0])),
total = (res_grid[1]-1)*(res_grid[0]-1),
desc = "find matches: complete grid"):
y,x = pg[iy,ix-1]
ovl = example[y:y+rp[0],x:x+rp[1]][:,-overlap[1]:].flatten()
y,x = pg[iy-1,ix]
ovt = example[y:y+rp[0],x:x+rp[1]][-overlap[0]:,:].flatten()
ovlt = np.hstack((ovl, ovt))
pg[iy,ix] = idx2co(find_match(ovlt, lt, tol = tol, k=k), max_co)
return pg
def minimum_error_boundary_cut(overlaps, direction):
"""
create an optimal boundary cut from
an error matrix calculated from overlaps
"""
#TODO: create minimum boundary for very small overlaps (for example just 1 pixel)
ol1, ol2 = overlaps
#calculate error and convert to grayscale
err = ((ol1 - ol2)**2).mean(2)
if direction == "v": err = err.T
minIndex = []
E = [list(err[0])]
for i in range(1, err.shape[0]):
# Get min values and args, -1 = left, 0 = middle, 1 = right
e = [np.inf] + E[-1] + [np.inf]
e = np.array([e[:-2], e[1:-1], e[2:]])
# Get minIndex
minArr = e.min(0)
minArg = e.argmin(0) - 1
minIndex.append(minArg)
# Set Eij = e_ij + min_
Eij = err[i] + minArr
E.append(list(Eij))
# Check the last element and backtrack to find path
path = []
minArg = np.argmin(E[-1])
path.append(minArg)
# Backtrack to min path
for idx in minIndex[::-1]:
minArg = minArg + idx[minArg]
path.append(minArg)
# Reverse to find full path
path = path[::-1]
m = np.zeros(err.shape, dtype=float)#define mask
for i,pi in enumerate(path):
#p1[i,-overlap[0] + pi,0]*=0.5
m[i,pi:]=True
m[i,pi]=0.5 #set a "smooth" boundary
#err[i,pi]+=0.05
if direction=="v": return m.T
else: return m
def create_patch_data(example, res_patch, max_co=None):
"""max_co is needed in the case where only overlap
areas of patches are of interest. In this case we want
the overlap areas to not extend beyond the area of
a contiguos patch
"""
if max_co is None: max_co = np.array(example.shape[:2]) - res_patch
rp = res_patch
data = np.ascontiguousarray([example[y:y+rp[0],x:x+rp[1]].flatten()
for y,x in tqdm(np.ndindex(*max_co), "create_patch_data")])
return data
def idx2co(idx, max_co):
yp = int(idx/max_co[1])
xp = idx - yp * max_co[1]
return yp,xp
#create patches
def gen_patches(image, res_patch):
res_img = np.array(image.shape[:2])
max_co = res_img - res_patch
patches = np.array([image[y:y+res_patch[0],x:x+res_patch[1]]
for y,x in np.ndindex(*max_co)])
return patches
#create patches
def gen_patches_from_mask(image, mask):
res_img = np.array(image.shape[:2])
m_res = mask.shape[:2]
max_co = res_img - m_res + (1,1)
patches = np.array([image[y:y+m_res[0],x:x+m_res[1]][mask].flatten()
for y,x in np.ndindex(*max_co)])
def idx2co(idx):
yp = int(idx/max_co[1])
xp = idx - yp * max_co[1]
return xp,yp
return patches, max_co, idx2co
#TODO: checkout the quality of the results for small image resolutions.
# - it might make sense toreduce the resolution for images based
# on certain criteria (for example patch size) anyways
def build_gaussian_pyramid(example0, min_res=8):
#build a gaussian image pyramid
py = [im for im in skimage.transform.pyramid_gaussian(example0, multichannel=True)]
#filter for minimum resolution
#min_res = 8
#py = [im for im in py if min(im.shape[:2]) >= min_res]
return list(reversed(py))
def create_mask_tree(img, kind="causal5x3"):
if kind == "causal5x3": #generate causal mask
mask_res = (3,5)
mask = np.ones(mask_res, dtype=bool)
mask[2,2:]=False
mask_center = (2,2) #starting index from 0
elif kind == "noncausal5x5": #generate non-causal mask:
mask_res = (5,5)
mask = np.ones(mask_res, dtype=bool)
mask_center = (2,2) #starting index from 0
mask[mask_center]=False
elif kind == "noncausal3x3": #generate non-causal mask:
mask_res = (3,3)
mask = np.ones(mask_res, dtype=bool)
mask_center = (1,1) #starting index from 0
else: raise ValueError(f"kind: {kind} is unknown")
logger.info("generating patches")
m_patches, max_co, idx2co = gen_patches_from_mask(img,mask)
#build local neighbourdhood KDTree for pyramid level
logger.info("generating tree from patches")
index = init_ann_index(m_patches)
return index, mask, mask_center, idx2co
#generate image with noisy borders which later be cut off
def local_neighbourhood_enhance(target, ex, mask, tree,
mask_center, idx2co, initial=False,
seed = 0):
#TODO: make tileable texture by copying the right sections from the
# texture to the other sides
np.random.seed(seed)
target_res=np.array(target.shape[:2])
m_res = np.array(mask.shape)
image_range=((mask_center[0], mask_center[0] + target_res[0]),
(mask_center[1], mask_center[0] + target_res[1]))
mc = mask_center
if initial:
t = np.random.random((target_res[0] + m_res[0]-1,
target_res[1] + m_res[1]-1,4))
t[...,3] = 1.0
t[image_range[0][0]: image_range[0][1],
image_range[1][0]: image_range[1][1]] = target
else:
t = np.random.random((target_res[0] + m_res[0]-1,
target_res[1] + m_res[1]-1,4))
#target_tmp[:target_cut[0],:target_cut[1]//2] = target[]
t[...,3] = 1.0
t[image_range[0][0]: image_range[0][1],
image_range[1][0]: image_range[1][1]] = target
#skimage.io.imshow_collection([target, t])
#return
if not initial: t2 = t.copy()
else: t2 = t
coords = itertools.product(range(image_range[0][0], image_range[0][1]),
range(image_range[1][0], image_range[1][1]))
#TODO: do some caching of similar mask queries to speed up the
# process quiet a bit
for y,x in tqdm(coords, "iterating over image",
total = target_res.prod(),
smoothing=0.01):
p = t[y-mc[0]:y-mc[0]+m_res[0],x-mc[1]:x-mc[1]+m_res[1]]
pf = p[mask].flatten()
[[err]], [[idx]] = tree.query([pf], k=1)
xp,yp = idx2co(idx)
pixel = ex[yp+mc[0],xp+mc[1]]
t2[y,x] = pixel
if False: #if debugging
if (x,y)==(4,4):
#py[-1][yp:yp+3,xp:xp+5]
p2 = ex[-1][yp:yp+m_res[0],xp:xp+m_res[1]].copy()
p2[~mask]=(0,0,0,1)
p3 = np.zeros((*mask.shape,4))
p3[mask] = m_patches[idx].reshape(-1,4)
#skimage.io.imshow_collection([t])
skimage.io.imshow_collection([p,target, p2,p3, t])
break
return t2[image_range[0][0]: image_range[0][1],
image_range[1][0]: image_range[1][1]]
def mask_synthesize_pixel(level, example, seed, pyramid,
final_res, target = None):
img = pyramid[level]
levelscale = img.shape[:2]/np.array(example.shape[:2])
target_res = np.rint(final_res*levelscale).astype(int)
if target is not None:
target = skimage.transform.resize(target, target_res, anti_aliasing=False,order=0)
kind = "noncausal5x5"
else:
target = np.zeros((*target_res,4), dtype=float)
target[...,3] = 1.0
kind = "causal5x3"
tree, mask, mask_center, idx2co = create_mask_tree(img,kind)
target = local_neighbourhood_enhance(target, img, mask,
tree, mask_center, idx2co,
initial=True, seed=seed)
return target
def pixel_synthesize_texture(final_res, scale = 1/2**3, seed = 15):
#TODO: choose output resolution level with a dedicated (2D-)scale-factor
# or a given resolution parameter (original texture will be
# up/down-scaled)
example = skimage.transform.rescale(example0, scale, preserve_range=False,
multichannel=True, anti_aliasing=True)
py = build_gaussian_pyramid(example)
start_level=5
tas=[] #save images for debug information
target = mask_synthesize_pixel(start_level, example, seed, py, final_res)
tas.append(target)
for level in range(start_level+1,len(py)):
logger.info(f"\nstarting next level calculation: {level}\n")
target = mask_synthesize_pixel(level, example, seed, py, final_res, target)
tas.append(target)
return target, tas
def calc_lib_scaling(res_ex, max_pixels):
ex_pixels = np.prod(res_ex)
px_ratio = ex_pixels/max_pixels
scaling = 1/math.sqrt(px_ratio)
if scaling<1.0: return scaling
else: return 1.0
def normalize_picture(example0, max_pixels = 256*256):
"""
scale picture down to a maximum number of pixels
"""
scaling = 1.0
res_ex = example0.shape[:2]
if max_pixels < np.prod(res_ex):
#max_pixels basically defines whats possible with the avialable
# memory & CPU power 256x256 has proved to be effective on modern systems
scaling = calc_lib_scaling(res_ex, max_pixels)
logger.info(f"resizing with scaling {scaling}")
example = skimage.transform.rescale(example0, scaling,
#example = skimage.transform.resize(example0, (256,256),
anti_aliasing=True,
multichannel=True,
preserve_range=True)#.astype(np.uint8)
#search_res = example.shape[:2]
else: example = example0
return example, scaling
def create_patch_params2(res_ex, scaling,
overlap_ratio, patch_ratio):
"""patch_ratio = #size of patches in comparison with original
"""
#TODO: define minimum size for patches
res_patch0 = int(min(res_ex)*patch_ratio)
res_patch0 = np.array([res_patch0]*2)
res_patch = np.round(res_patch0*scaling).astype(int)
overlap = np.ceil(res_patch*overlap_ratio).astype(int)
#res_patch2 = np.round(np.array(res_patch)/scaling).astype(int)
overlap0 = np.ceil(res_patch0*overlap_ratio).astype(int)
return res_patch, res_patch0, overlap, overlap0
def create_patch_params(example0, scaling,
overlap_ratio = 1/6, patch_ratio = 0.05):
return create_patch_params2(example0.shape[:2], scaling,
overlap_ratio, patch_ratio)
#create test-target to fill with mask:
def generate_test_target_with_fill_mask(example):
target = example.copy()
target[...,3]=1.0
verts = [np.array(((0.1,0.1),(0.4,0.15),(0.41,0.4),(0.2,0.38))),
np.array(((0.5,0.55),(0.85,0.53),(0.8,0.7),(0.51,0.71)))]
masks = []
pxverts = []
for v in verts:
pxverts.append(v * target.shape[:2])
rr,cc = skimage.draw.polygon(*v.T)
mask = np.zeros(target.shape[:2])
mask[rr,cc]=1.0
target[rr,cc]=(1,0,0,1)
masks.append(mask)
return target, masks, pxverts
def draw_polygon_mask(verts, size):
rr,cc = skimage.draw.polygon(*verts.T)
mask = np.zeros(size)
mask[rr,cc]=1.0
return mask
def edge_distance(poly, x,y):
d = poly.boundary.distance(shapely.geometry.Point(x,y))
if poly.contains(shapely.geometry.Point(x,y)): return d
else: return -d
def get_poly_levelset(verts, width=10):
poly = shapely.geometry.Polygon(verts)
poly_box = poly.buffer(+width) #add two pixels on the container
bbox = poly_box.bounds
miny, minx, maxy, maxx = bbox_px = np.round(np.array(bbox)).astype(int)
w,h = maxx - minx, maxy-miny
bbcoords = itertools.product(range(miny,maxy), range(minx, maxx))
levelset = np.array([edge_distance(poly,y,x) for y,x in bbcoords]).reshape(h,w)
#normalize levelset:
#levelset = np.maximum(levelset/levelset.max(),0.0)
return levelset, bbox_px
@timing
def fill_area_with_texture(target, example0, ta_map_final=None,
patch_ratio=0.1, libsize = 128*128,
verts=None, mask = None, bounding_box = None):
if bounding_box is None:
area = shapely.geometry.Polygon(verts)
ov = 1 #overlap
y0,x0,y1,x1 = np.array(area.bounds).astype(int) + (-ov,-ov,ov,ov)
else:
y0,x0,y1,x1 = bounding_box
#print("create levelset")
#levelset, (minx, miny, maxx, maxy) = get_poly_levelset(verts, width=ov)
bbox = target[y0:y1,x0:x1]
#bmask = levelset>0
if mask is None:
mask = draw_polygon_mask(verts,target.shape[:2])
bmask = mask[y0:y1,x0:x1]>0
else:
bmask = mask
#bmask2 = mask[y0:y1,x0:x1]>0
#area.boundary.buffer(100)
logger.info("synthesize texture")
fill1, ta_map = synth_patch_tex(bbox, example0, k=1,
patch_ratio=patch_ratio,
libsize=libsize)
copy_img(target, fill1, (x0,y0), bmask)
#ta_map_final = np.full((*target.shape[:2],3),[0,0,0])
#ta_map_final = np.full([*target.shape[:2],3],0)
if ta_map_final is None: ta_map_final = np.zeros([*target.shape[:2],3])
#copy_img(ta_map_final, ta_map, (x0,y0), bmask)
copy_img(ta_map_final, ta_map, (x0,y0), bmask)
#TODO: somehow the copy operation doesnt work here
#import ipdb; ipdb.set_trace() # BREAKPOINT
return target, ta_map_final
def calculate_memory_consumption(res_ex, res_patch,
ch_num, itemsize):
patch_num=np.product(np.array(res_ex) - res_patch)
#import ipdb; ipdb.set_trace() # BREAKPOINT
data_memoryGB = patch_num*ch_num*itemsize*np.product(res_patch)*GB
#print(f"using approx. {data_memoryGB:2f} GB in RAM.")
return data_memoryGB
def check_memory_requirements(example, res_patch, maxmem = 1.0,
disable_safety_check=False):
data_memoryGB = calculate_memory_consumption(
np.array(example.shape[:2]),res_patch,
ch_num = example.shape[2],
itemsize = example.itemsize)
logger.info(f"using {data_memoryGB:.4f} of {maxmem} GB for synthesis")
if not disable_safety_check:
if data_memoryGB > maxmem:
raise MemoryError("the algorithm would exceed the "
"maximum amount of Memory: "
f"{data_memoryGB:2f} GB,; max: {maxmem}")
return data_memoryGB
@timing
def prepare_tree(example0, lib_size, overlap_ratio, patch_ratio,
mode=None):
example, scaling = normalize_picture(example0, lib_size)
res_patch, res_patch0, overlap, overlap0 = create_patch_params(example0, scaling,
overlap_ratio,
patch_ratio)
max_co = np.array(example.shape[:2]) - res_patch
data_memoryGB = check_memory_requirements(example,res_patch,
maxmem=get_mem_limit(),
disable_safety_check=True)
logger.info(f"using approx. {data_memoryGB:2f} GB in RAM.")
try:
if mode==None:
data = create_patch_data(example, res_patch, max_co)
index = init_ann_index(data)
else:
if ('both' in mode) or ('horizontal' in mode):
logger.info("init kdtree1")
ld = create_patch_data(example, (res_patch[0], overlap[1]), max_co)
l = init_ann_index(ld)
if ('both' in mode) or ('vertical' in mode):
logger.info("init kdtree2")
td = create_patch_data(example, (overlap[0],res_patch[1]), max_co)
t = init_ann_index(td)
if 'both:':
logger.info("init kdtree3")
lt = init_ann_index(np.hstack((ld,td)))
index = [l,t,lt]
except MemoryError as err:
logger.info(err)
logger.info("example texture too large, algorithm needs "
"too much RAM: {totalmemoryGB:.2f}GB")
raise
return index, res_patch, res_patch0, overlap, overlap0, max_co, scaling
def gen_coordinate_grid(shape, flatten=False):
x = np.arange(0,shape[1])
y = np.arange(0,shape[0])
#get coordinate grid
#TODO: this might be more elegant using np.dstack
grid = np.stack(np.meshgrid(y,x),axis = 2)
if flatten: grid = grid.reshape(-1,2)
return grid
def gen_coordinate_map(shape):
grid2ch=gen_coordinate_grid(shape)
#TODO: this might be more elegant using np.dstack
grid3ch=np.stack((grid2ch[...,0],
grid2ch[...,1],
np.zeros(shape)),axis=2)#,axis=2)
return grid3ch
"""
#TODO: high quality optimization
This function doesn't work yet...
the main problem is that when changing the resolution of textures
only for single patches, we get a different result for that area
then if we would change it for the entire texture at once. The reason
for this are the rounding errors when going to smaller resolution
pictures: some of the pixels of the smaller sized image will include
the values of multiple pixels also from the neighbouring patches. if
we decrease the resolution of only a patch, this does not happen.
This
makes it impossible to incoporate
@timing
def synth_patch_tex(target0, example0,
lib_size = 10000,
k=1,
patch_ratio=0.1,
overlap_ratio = 1/6,
tol=0.1
):
'''
lib_size = 10000
k=1
patch_ratio=0.1
overlap_ratio = 1/6
tol=0.1
'''
#TODO: merge this function with the "search" and the "optimal patch"
# functionality to make it similar to the "synthesize_tex_patches" function
chan = 3
#target = target.copy()
example0 = example0[...,:chan]
target_map = target0.copy()
(trees, res_patch,
res_patch0, overlap,
overlap0, max_co,
scaling) = prepare_tree(example0, lib_size, overlap_ratio, patch_ratio,
mode=["horizontal","vertical","both"])
logger.info(f"patch_size: {res_patch0}; initial scaling: {scaling}, ")
#define search grid
res_target0 = target0.shape[:2]
rpg0 = np.array(res_patch0) - overlap0
rpg = np.array(res_patch) - overlap
res_grid0 = np.ceil((res_target0-overlap0)/rpg0).astype(int)
co_map_base = gen_coordinate_map(res_patch0)
#resize target to the same scale as the scaled example
#this is actually important and can NOT be done "on the fly" for
#each
target = skimage.transform.rescale(target0, scaling,
anti_aliasing=True,
multichannel=True,
preserve_range=True)#.astype(np.uint8)
example = skimage.transform.rescale(example0, scaling,
anti_aliasing=True,
multichannel=True,
preserve_range=True)#.astype(np.uint8)
#left corner (0,0)
for coords in tqdm(np.ndindex(*res_grid0),"iterate over image"):
yp,xp=np.array(coords) * rpg
search_area0 = target[yp:yp+res_patch[0],
xp:xp+res_patch[1]].copy()
if coords==(0,0):
pa_coords_idx = pa_y,pa_x = np.array(idx2co(
random.randrange(np.product(max_co)),
max_co)) #get first patch
#skimage.io.imshow_collection([patch, co_map/(*example.shape[:2],1)])
elif coords[0]==0: #first row
ovl = search_area0[:,:overlap[1]]
#ovl = skimage.transform.resize(ovl,
# (res_patch[0],overlap[1]),
# preserve_range=True)
pa_idx = find_match(ovl.flatten(), trees[0] , tol=tol, k=k)
pa_coords_idx = pa_y,pa_x = np.array(idx2co(pa_idx, max_co))
pa = example[pa_y:pa_y+res_patch[0],pa_x:pa_x+res_patch[1]]
copy_img(target,pa,(xp,yp))
pa_y0,pa_x0 = np.round(pa_coords_idx / scaling).astype(int)
pa0 = example0[pa_y0:pa_y0+res_patch0[0],pa_x0:pa_x0+res_patch0[1]]
copy_img(target0,pa0,(xp,yp))
co_map = co_map_base + (pa_y0,pa_x0,0)
copy_img(target_map,co_map,(xp,yp))
#ovl = example[y:y+rp[0],x:x+rp[1]][:,-overlap[0]:].flatten()
#if False:
if coords==(0,10):
#patch_idx = trees[0].get_arrays()[0][pa_idx].reshape(res_patch[0],-1,3)
patch_idx = trees[0]._raw_data[pa_idx].reshape(res_patch[0],-1,3)
skimage.io.imshow_collection([ovl,search_area0, pa0, patch_idx])
skimage.io.imshow_collection([pa0, target_map/(*example0.shape[:2],1), target])
break
return target_map, target