3939add_arg ('--zoom' , default = 1 , type = int , help = 'Resolution increase factor for inference.' )
4040add_arg ('--rendering-tile' , default = 128 , type = int , help = 'Size of tiles used for rendering images.' )
4141add_arg ('--rendering-overlap' , default = 32 , type = int , help = 'Number of pixels padding around each tile.' )
42+ add_arg ('--rendering-histogram' ,default = False , action = 'store_true' , help = 'Match color histogram of output to input.' )
4243add_arg ('--type' , default = 'photo' , type = str , help = 'Name of the neural network to load/save.' )
4344add_arg ('--model' , default = 'default' , type = str , help = 'Specific trained version of the model.' )
4445add_arg ('--train' , default = False , type = str , help = 'File pattern to load for training.' )
@@ -532,6 +533,14 @@ def train(self):
532533 self .model .save_generator ()
533534 print (ansi .ENDC )
534535
536+ def match_histograms (self , A , B , rng = (0.0 , 255.0 ), bins = 64 ):
537+ (Ha , Xa ), (Hb , Xb ) = [np .histogram (i , bins = bins , range = rng , density = True ) for i in [A , B ]]
538+ X = np .linspace (rng [0 ], rng [1 ], bins , endpoint = True )
539+ Hpa , Hpb = [np .cumsum (i ) * (rng [1 ] - rng [0 ]) ** 2 / float (bins ) for i in [Ha , Hb ]]
540+ inv_Ha = scipy .interpolate .interp1d (X , Hpa , bounds_error = False )
541+ map_Hb = scipy .interpolate .interp1d (Hpb , X , bounds_error = False )
542+ return map_Hb (inv_Ha (A ))
543+
535544 def process (self , original ):
536545 # Snap the image to a shape that's compatible with the generator (2x, 4x)
537546 s = 2 ** max (args .generator_upscale , args .generator_downscale )
@@ -549,7 +558,14 @@ def process(self, original):
549558 * _ , repro = self .model .predict (img )
550559 output [y * z :(y + s )* z ,x * z :(x + s )* z ,:] = np .transpose (repro [0 ] + 0.5 , (1 , 2 , 0 ))[p * z :- p * z ,p * z :- p * z ,:]
551560 print ('.' , end = '' , flush = True )
552- return scipy .misc .toimage (output .clip (0.0 , 1.0 ) * 255.0 , cmin = 0 , cmax = 255 )
561+ output = output .clip (0.0 , 1.0 ) * 255.0
562+
563+ # Match color histograms if the user specified this option.
564+ if args .rendering_histogram :
565+ for i in range (3 ):
566+ output [:,:,i ] = self .match_histograms (output [:,:,i ], original [:,:,i ])
567+
568+ return scipy .misc .toimage (output , cmin = 0 , cmax = 255 )
553569
554570
555571if __name__ == "__main__" :
0 commit comments