Skip to content

Instantly share code, notes, and snippets.

@hirocarma
Created February 25, 2023 11:42
Show Gist options
  • Save hirocarma/ec00b5cc4b045f1e1dbfd7a9f0d76028 to your computer and use it in GitHub Desktop.
Save hirocarma/ec00b5cc4b045f1e1dbfd7a9f0d76028 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
import math
def Lab_plt(IMG_DIR, p_title):
files = os.listdir(IMG_DIR)
files = sorted(files)
fps = 24
Lt=[];Ll=[];La=[];Lb=[];Lc=[]
for i, file in enumerate(files):
if not i % (fps / 8) == 0:
continue
img_path = IMG_DIR + '/' + file
img = cv2.imread(img_path)
lab_img = cv2.cvtColor(img, cv2.COLOR_BGR2Lab)
mean_l = int(np.mean(lab_img[:,:,0]))
mean_a = int(np.mean(lab_img[:,:,1]))
mean_b = int(np.mean(lab_img[:,:,2]))
mean_c = math.sqrt(mean_a**2+mean_b**2)
Lt.append(i/fps/60)
Ll.append(mean_l)
La.append(mean_a)
Lb.append(mean_b)
Lc.append(mean_c)
num = fps
b = np.ones(num)/num
Ll2 = np.convolve(Ll, b, mode='same')
La2 = np.convolve(La, b, mode='same')
Lb2 = np.convolve(Lb, b, mode='same')
Lc2 = np.convolve(Lc, b, mode='same')
l_mean = np.mean(Ll)
a_mean = np.mean(La)
b_mean = np.mean(Lb)
c_mean = np.mean(Lc)
l_med = np.median(Ll)
a_med = np.median(La)
b_med = np.median(Lb)
c_med = np.median(Lc)
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=False)
ax = fig.add_subplot(111, fc='w', xlabel='分(min)', ylabel='L*(明度)')
ax.set_title(p_title +" L*a*b* L*(明度)推移")
ax.plot(Lt, Ll, label='L*')
ax.plot(Lt, Ll2 , 'r', label='L*:Moving average')
ax.axhline(y=l_mean , color='g',linestyle='dashed', linewidth=1)
ax.text(-4, l_mean, "average:" + str(round(l_mean,1)) , size=10)
ax.set_ylim(-5,255)
ax.grid()
ax.legend()
fig.savefig(p_fname +'-Lab-l.png', facecolor=fig.get_facecolor())
plt.show()
fig1 = plt.figure(figsize=(16, 8), dpi=100, facecolor='lightgray', tight_layout=False)
ax1 = fig1.add_subplot(111, fc='w', xlabel='分(min)', ylabel='a*(色相)')
ax1.set_title(p_title +" L*a*b* a*(色相)推移")
ax1.plot(Lt, La, label='a*')
ax1.plot(Lt, La2 , 'r', label='a*:Moving average')
ax1.axhline(y=a_mean , color='g',linestyle='dashed', linewidth=1)
ax1.text(-4, a_mean, "average:" + str(round(a_mean,1)) , size=10)
ax1.set_ylim(-5,255)
ax1.grid()
ax1.legend()
fig1.savefig(p_fname +'-Lab-a.png', facecolor=fig.get_facecolor())
plt.show()
fig2 = plt.figure(figsize=(16, 8), dpi=100, facecolor='lightgray', tight_layout=False)
ax2 = fig2.add_subplot(111, fc='w', xlabel='分(min)', ylabel='b*(彩度)')
ax2.set_title(p_title +" L*a*b* b*(彩度)推移")
ax2.plot(Lt, Lb, label='b*')
ax2.plot(Lt, Lb2 , 'r', label='b*:Moving average')
ax2.axhline(y=b_mean , color='g',linestyle='dashed', linewidth=1)
ax2.text(-4, b_mean, "average:" + str(round(b_mean,1)) , size=10)
ax2.set_ylim(-5,255)
ax2.grid()
ax2.legend()
fig2.savefig(p_fname +'-Lab-b.png', facecolor=fig.get_facecolor())
plt.show()
fig3 = plt.figure(figsize=(16, 8), dpi=100, facecolor='lightgray', tight_layout=False)
ax3 = fig3.add_subplot(111, fc='w', xlabel='分(min)', ylabel='c*(彩度)')
ax3.set_title(p_title +" L*a*b* c*(彩度)推移")
ax3.plot(Lt, Lc, label='c*')
ax3.plot(Lt, Lc2 , 'r', label='c*:Moving average')
ax3.axhline(y=b_mean , color='g',linestyle='dashed', linewidth=1)
ax3.text(-4, c_mean, "average:" + str(round(b_mean,1)) , size=10)
ax3.set_ylim(-5,255)
ax3.grid()
ax3.legend()
fig3.savefig(p_fname +'-Lab-c.png', facecolor=fig.get_facecolor())
plt.show()
if __name__ == '__main__':
IMG_DIR = sys.argv[1]
p_title = sys.argv[2]
Lab_plt(IMG_DIR, p_title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment