2013-03-06 07:41:02 +01:00
|
|
|
#!/usr/bin/env python
|
2012-11-23 19:57:22 +01:00
|
|
|
|
2015-12-15 00:33:55 +01:00
|
|
|
'''
|
|
|
|
Video histogram sample to show live histogram of video
|
|
|
|
|
|
|
|
Keys:
|
|
|
|
ESC - exit
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2012-10-17 01:18:30 +02:00
|
|
|
import numpy as np
|
|
|
|
import cv2
|
2013-03-06 07:41:02 +01:00
|
|
|
|
|
|
|
# built-in modules
|
2012-10-17 01:18:30 +02:00
|
|
|
import sys
|
|
|
|
|
2013-03-06 07:41:02 +01:00
|
|
|
# local modules
|
2012-10-17 01:18:30 +02:00
|
|
|
import video
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
hsv_map = np.zeros((180, 256, 3), np.uint8)
|
|
|
|
h, s = np.indices(hsv_map.shape[:2])
|
|
|
|
hsv_map[:,:,0] = h
|
|
|
|
hsv_map[:,:,1] = s
|
|
|
|
hsv_map[:,:,2] = 255
|
|
|
|
hsv_map = cv2.cvtColor(hsv_map, cv2.COLOR_HSV2BGR)
|
|
|
|
cv2.imshow('hsv_map', hsv_map)
|
|
|
|
|
|
|
|
cv2.namedWindow('hist', 0)
|
|
|
|
hist_scale = 10
|
2015-12-15 00:33:55 +01:00
|
|
|
|
2012-10-17 01:18:30 +02:00
|
|
|
def set_scale(val):
|
|
|
|
global hist_scale
|
|
|
|
hist_scale = val
|
|
|
|
cv2.createTrackbar('scale', 'hist', hist_scale, 32, set_scale)
|
|
|
|
|
2013-03-06 07:41:02 +01:00
|
|
|
try:
|
|
|
|
fn = sys.argv[1]
|
|
|
|
except:
|
|
|
|
fn = 0
|
2014-09-13 16:28:41 +02:00
|
|
|
cam = video.create_capture(fn, fallback='synth:bg=../data/baboon.jpg:class=chess:noise=0.05')
|
2012-10-17 01:18:30 +02:00
|
|
|
|
|
|
|
while True:
|
|
|
|
flag, frame = cam.read()
|
|
|
|
cv2.imshow('camera', frame)
|
|
|
|
|
|
|
|
small = cv2.pyrDown(frame)
|
|
|
|
|
|
|
|
hsv = cv2.cvtColor(small, cv2.COLOR_BGR2HSV)
|
|
|
|
dark = hsv[...,2] < 32
|
|
|
|
hsv[dark] = 0
|
2015-12-15 00:33:55 +01:00
|
|
|
h = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
|
2012-10-17 01:18:30 +02:00
|
|
|
|
|
|
|
h = np.clip(h*0.005*hist_scale, 0, 1)
|
|
|
|
vis = hsv_map*h[:,:,np.newaxis] / 255.0
|
|
|
|
cv2.imshow('hist', vis)
|
|
|
|
|
|
|
|
ch = 0xFF & cv2.waitKey(1)
|
|
|
|
if ch == 27:
|
|
|
|
break
|
|
|
|
cv2.destroyAllWindows()
|