forked from sniklaus/teaching-vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-colorize.py
31 lines (15 loc) · 1.08 KB
/
09-colorize.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
import numpy
import cv2
numpyInput = cv2.imread(filename='./samples/prokudin.png', flags=cv2.IMREAD_GRAYSCALE).astype(numpy.float32) / 255.0
# align the individual images such that their combination results in a proper color image
# splitting the input into the three individual channels while slightly cropping the boundary
intFirst, intSecond = int(1.0 * numpyInput.shape[0] / 3.0), int(2.0 * numpyInput.shape[0] / 3.0)
numpyB = numpyInput[:intFirst, :][50:-50, 50:-50]
numpyG = numpyInput[intFirst:intSecond, :][50:-50, 50:-50]
numpyR = numpyInput[intSecond:, :][50:-50, 50:-50]
# find the homography matrices between the images using cv2.findTransformECC
# based on these matrices, warp numpyG and numpyR towards numpyB using cv2.warpPerspective
# make sure to use inverse warping as stated in the documentation of cv2.findTransformECC
# once the channels are aligned, they can simply be stacked to create the color image
numpyOutput = numpy.stack([ numpyB, numpyG, numpyR ], 2)
cv2.imwrite(filename='./09-colorize.png', img=(numpyOutput * 255.0).clip(0.0, 255.0).astype(numpy.uint8))