Skip to content
This repository was archived by the owner on Jan 2, 2021. It is now read-only.

Commit a69d2ae

Browse files
committed
Add histogram color matching, enabled with --rendering-histogram.
1 parent 965bd55 commit a69d2ae

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ A list of example command lines you can use with the pre-trained models provided
5151
Here's a list of currently supported models, image types, and zoom levels in one table.
5252

5353
================== ===================== ==================== ===================== ====================
54-
``--model=default`` ``--model=repair`` ``--model=denoise`` ``--model=deblur``
54+
FEATURES ``--model=default`` ``--model=repair`` ``--model=denoise`` ``--model=deblur``
5555
================== ===================== ==================== ===================== ====================
5656
``--type=photo`` 2x 1x … …
5757
================== ===================== ==================== ===================== ====================

enhance.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
add_arg('--zoom', default=1, type=int, help='Resolution increase factor for inference.')
4040
add_arg('--rendering-tile', default=128, type=int, help='Size of tiles used for rendering images.')
4141
add_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.')
4243
add_arg('--type', default='photo', type=str, help='Name of the neural network to load/save.')
4344
add_arg('--model', default='default', type=str, help='Specific trained version of the model.')
4445
add_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

555571
if __name__ == "__main__":

0 commit comments

Comments
 (0)