Skip to content

Instantly share code, notes, and snippets.

@yingminc
Last active July 31, 2017 08:57
Show Gist options
  • Save yingminc/912d18f07aac753d39a5d7f2cde06a6a to your computer and use it in GitHub Desktop.
Save yingminc/912d18f07aac753d39a5d7f2cde06a6a to your computer and use it in GitHub Desktop.
opencv colormap by trackbar
import cv2
import argparse
# COLORMAP_AUTUMN = 0,
# COLORMAP_BONE = 1,
# X COLORMAP_JET = 2,
# COLORMAP_WINTER = 3,
# X COLORMAP_RAINBOW = 4,
# COLORMAP_OCEAN = 5,
# COLORMAP_SUMMER = 6,
# COLORMAP_SPRING = 7,
# COLORMAP_COOL = 8,
# X COLORMAP_HSV = 9,
# COLORMAP_PINK = 10,
# COLORMAP_HOT = 11
# GRAY_SCALE = 12
# 0RIGINAL = 13
# NEGATIVE EFFECT AFTER COLORMAP
def nothing(x):
pass
def colormap_bar(filepath):
filename = filepath.split('/')[-1]
cv2.namedWindow(filename, 1)
cm ='colormap'
cv2.createTrackbar(cm, filename, 13, 13, nothing)
ng = 'negative'
cv2.createTrackbar(ng, filename, 0, 1, nothing)
im = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
im_o = cv2.imread(filepath)
while True:
cmv= cv2.getTrackbarPos(cm,filename)
ngv= cv2.getTrackbarPos(ng,filename)
if cmv == 12:
im_c = im
elif cmv == 13:
im_c = im_o
else:
im_c = cv2.applyColorMap(im,cmv)
if ngv == 1:
im_c= (255-im_c)
cv2.imshow(filename, im_c)
ch = cv2.waitKey(5)
if ch== 27:
break
cv2.destroyAllWindows()
return im_c
parser =argparse.ArgumentParser()
parser.add_argument('filepath', help = 'the img file for processing')
args = parser.parse_args()
colormap_bar(args.filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment