Skip to content

Instantly share code, notes, and snippets.

@hirocarma
Created April 18, 2021 09:27
Show Gist options
  • Save hirocarma/1f44269c52a3689554adda7c63fd06d5 to your computer and use it in GitHub Desktop.
Save hirocarma/1f44269c52a3689554adda7c63fd06d5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import sys
import cv2
import numpy as np
import colorsys
import matplotlib.pyplot as plt
def hsv_av(img):
r = int(np.mean(img[:,:,0]))
g = int(np.mean(img[:,:,1]))
b = int(np.mean(img[:,:,2]))
hsv = colorsys.rgb_to_hsv(r/255.0,g/255.0,b/255.0)
(h, s, v) = (int(hsv[0]*255), int(hsv[1]*255),int(hsv[2]*255))
return (h, s, v)
def hsv_plt(IMG_DIR):
files = os.listdir(IMG_DIR)
files = sorted(files)
fps = 60
Lt=[];Lh=[];Ls=[];Lv=[]
for i, file in enumerate(files):
if not i % (fps / 8) == 0:
continue
img_path = IMG_DIR + '/' + file
img = cv2.imread(img_path)
(h, s, v) = hsv_av(img)
Lt.append(i/fps/60)
Lh.append(h)
Ls.append(s)
Lv.append(v)
num = fps
b = np.ones(num)/num
Lh2 = np.convolve(Lh, b, mode='same')
Ls2 = np.convolve(Ls, b, mode='same')
Lv2 = np.convolve(Lv, b, mode='same')
h_mean = np.mean(Lh)
s_mean = np.mean(Ls)
v_mean = np.mean(Lv)
p_title ="ゾンビランドサガ リベンジ 1話"
p_fname ='saga1'
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = ['IPAPGothic', 'VL PGothic']
fig = plt.figure(figsize=(16, 8), dpi=100, facecolor='lightgray', tight_layout=True)
ax = fig.add_subplot(111, fc='w', xlabel='分(min)', ylabel='色相')
ax.set_title(p_title +" 色相推移")
ax.plot(Lt, Lh, label='Hue')
ax.plot(Lt, Lh2 , 'r', label='Hue:Moving average')
ax.axhline(y=h_mean , color='g',linestyle='dashed', linewidth=1)
ax.text(-2, h_mean, "average:" + str(round(h_mean,1)) , size=10)
ax.grid()
ax.legend()
fig.savefig(p_fname +'-h.png', facecolor=fig.get_facecolor())
plt.show()
fig1 = plt.figure(figsize=(16, 8), dpi=100, facecolor='lightgray', tight_layout=True)
ax1 = fig1.add_subplot(111, fc='w', xlabel='分(min)', ylabel='彩度')
ax1.set_title(p_title +" 彩度推移")
ax1.plot(Lt, Ls, label='Saturation')
ax1.plot(Lt, Ls2 , 'r', label='Saturation:Moving average')
ax1.axhline(y=s_mean , color='g',linestyle='dashed', linewidth=1)
ax1.text(-2, s_mean, "average:" + str(round(s_mean,1)) , size=10)
ax1.grid()
ax1.legend()
fig1.savefig(p_fname +'-s.png', facecolor=fig.get_facecolor())
plt.show()
fig2 = plt.figure(figsize=(16, 8), dpi=100, facecolor='lightgray', tight_layout=True)
ax2 = fig2.add_subplot(111, fc='w', xlabel='分(min)', ylabel='明度')
ax2.set_title(p_title +" 明度推移")
ax2.plot(Lt, Lv, label='Value')
ax2.plot(Lt, Lv2 , 'r', label='Value:Moving average')
ax2.axhline(y=v_mean , color='g',linestyle='dashed', linewidth=1)
ax2.text(-2, v_mean, "average:" + str(round(v_mean,1)) , size=10)
ax2.grid()
ax2.legend()
fig2.savefig(p_fname +'-v.png', facecolor=fig.get_facecolor())
plt.show()
if __name__ == '__main__':
IMG_DIR = sys.argv[1]
hsv_plt(IMG_DIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment