Last active
April 3, 2020 16:33
-
-
Save ByteArts/e0aec3eed18bd3e894a240f18af69ca6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import cv2 | |
import numpy as np | |
def make_custom_lut(src_img): | |
""" | |
Given a color source image that is 256 pixels tall, this will extract the color | |
values from the first pixel on each row of the image and use those pixels to | |
make a color LUT. | |
Args: | |
src_image (opencv image object) | |
Returns: | |
LUT (list) - array of BGR values | |
""" | |
lut = np.zeros((256, 1, 3), dtype=np.uint8) | |
blue_values = range(256) | |
green_values = range(256) | |
red_values = range(256) | |
for row in range(0, 256): | |
pixel = src_img[row, 0] | |
blue_values[row] = pixel[0] | |
green_values[row] = pixel[1] | |
red_values[row] = pixel[2] | |
#end for | |
lut[:, 0, 0] = blue_values | |
lut[:, 0, 1] = green_values | |
lut[:, 0, 2] = red_values | |
return lut | |
#end def | |
if __name__ == '__main__' : | |
# first load an image to use as a color LUT | |
lut_img = cv2.imread('customcolormap.png', cv2.IMREAD_COLOR) | |
color_table = make_custom_lut(lut_img) | |
# read in the image to be processed as a grayscale image | |
img_gray = cv2.imread('img256x256.png', cv2.IMREAD_GRAYSCALE) | |
# convert to a color image and apply the LUT | |
img_temp = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR) | |
img_color = cv2.LUT(img_temp, color_table) | |
# save colorized image | |
cv2.imwrite('colormapped.png', img_color) | |
#end if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment