Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maycuatroi1/7995c8cbaaa19ebe81a9c5f2efa94e86 to your computer and use it in GitHub Desktop.
Save maycuatroi1/7995c8cbaaa19ebe81a9c5f2efa94e86 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
def lookup_table_transform(image, table):
if len(table) != 256:
raise ValueError("Lookup table must have exactly 256 values")
lut = np.array(table, dtype=np.uint8)
if len(image.shape) == 2:
return cv2.LUT(image, lut)
elif len(image.shape) == 3:
result = image.copy()
for i in range(image.shape[2]):
result[:, :, i] = cv2.LUT(image[:, :, i], lut)
return result
else:
raise ValueError("Unsupported image format")
def main():
output_image = 'output.png'
image = cv2.imread('image.png',0)
table = np.arange(256)
table[0:100] = 0
table[100:200] = 100
table[200:255] = 200
table[255] = 255
transformed_image = lookup_table_transform(image, table)
cv2.imwrite(output_image, transformed_image)
final_image = np.hstack((image, transformed_image))
cv2.imshow('Final Image', final_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment