Skip to content

Instantly share code, notes, and snippets.

@trhura
Created October 12, 2014 03:40
Show Gist options
  • Save trhura/e69fca88bbd941c7024b to your computer and use it in GitHub Desktop.
Save trhura/e69fca88bbd941c7024b to your computer and use it in GitHub Desktop.
opencv color picker
#! /usr/bin/env python2
import cv2
import numpy as np
colors = []
def on_mouse_click (event, x, y, flags, frame):
if event == cv2.EVENT_LBUTTONUP:
colors.append(frame[y,x].tolist())
def main():
capture = cv2.VideoCapture(0)
while True:
_, frame = capture.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS_FULL)
if colors:
cv2.putText(hsv, str(colors[-1]), (10, 50), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 0), 2)
cv2.imshow('frame', hsv)
cv2.setMouseCallback('frame', on_mouse_click, hsv)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
# avgb = int(sum(c[0] for c in colors) / len(colors))
# avgg = int(sum(c[0] for c in colors) / len(colors))
# avgr = int(sum(c[0] for c in colors) / len(colors))
# print avgb, avgg, avgr
minb = min(c[0] for c in colors)
ming = min(c[1] for c in colors)
minr = min(c[2] for c in colors)
maxb = max(c[0] for c in colors)
maxg = max(c[1] for c in colors)
maxr = max(c[2] for c in colors)
print minr, ming, minb, maxr, maxg, maxb
lb = [minb,ming,minr]
ub = [maxb,maxg,maxr]
print lb, ub
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment