Skip to content

Instantly share code, notes, and snippets.

@hirocarma
Created January 30, 2022 09:12
Show Gist options
  • Save hirocarma/ff52006ce7159ac53ddc0f0c4a6446a5 to your computer and use it in GitHub Desktop.
Save hirocarma/ff52006ce7159ac53ddc0f0c4a6446a5 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_plt(IMG_DIR, p_title):
files = os.listdir(IMG_DIR)
files = sorted(files)
fps = 24
Lt=[];Lh=[];Ls=[];Lv=[];Lhstd=[]
for i, file in enumerate(files):
if not i % (fps / 8) == 0:
continue
img_path = IMG_DIR + '/' + file
img = cv2.imread(img_path)
img_h = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = img_h[:,:,0], img_h[:,:,1], img_h[:,:,2]
av_h = np.mean(h)
av_s = np.mean(s)
av_v = np.mean(v)
h_std = np.std(h)
Lt.append(i/fps/60)
Lh.append(av_h)
Ls.append(av_s)
Lv.append(av_v)
Lhstd.append(h_std)
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')
Lhstd2 = np.convolve(Lhstd, b, mode='same')
h_mean = np.mean(Lh)
s_mean = np.mean(Ls)
v_mean = np.mean(Lv)
h_std_mean = np.mean(Lhstd)
h_med = np.median(Lh)
s_med = np.median(Ls)
v_med = np.median(Lv)
h_std_med = np.median(Lhstd)
p_fname =p_title
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 +" hsv 色相推移")
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.set_ylim(-5,255)
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 +" hsv 彩度推移")
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.set_ylim(-5,255)
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 +" hsv 明度推移")
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.set_ylim(-5,255)
ax2.grid()
ax2.legend()
fig2.savefig(p_fname +'-v.png', facecolor=fig.get_facecolor())
plt.show()
fig3 = plt.figure(figsize=(16, 8), dpi=100, facecolor='lightgray', tight_layout=True)
ax3 = fig3.add_subplot(111, fc='w', xlabel='分(min)', ylabel='偏差')
ax3.set_title(p_title +" hsv 色相偏差推移")
ax3.plot(Lt, Lhstd, label='Hue:Standard deviation')
ax3.plot(Lt, Lhstd2 , 'r', label='Std:Moving average')
ax3.axhline(y=h_std_mean , color='g',linestyle='dashed', linewidth=1)
ax3.text(-2, h_std_mean, "average:" + str(round(h_std_mean,1)) , size=10)
ax3.grid()
ax3.legend()
fig3.savefig(p_fname +'-h_std.png', facecolor=fig.get_facecolor())
plt.show()
fig4 = plt.figure(figsize=(16, 8), dpi=100, facecolor='lightgray', tight_layout=True)
ax4 = fig4.add_subplot(111, fc='w', xlabel='彩度', ylabel='明度')
ax4.set_title(p_title +" hsv トーン:明度x彩度")
ax4.scatter(Ls,Lv)
ax4.grid()
ax4.legend()
fig4.savefig(p_fname +'-tone.png', facecolor=fig.get_facecolor())
plt.show()
if __name__ == '__main__':
IMG_DIR = sys.argv[1]
p_title = sys.argv[2]
hsv_plt(IMG_DIR, p_title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment