Skip to content

Instantly share code, notes, and snippets.

@jliebana
Created November 19, 2017 19:03
Show Gist options
  • Save jliebana/18ef744014fef4df902b6db4647b2cd9 to your computer and use it in GitHub Desktop.
Save jliebana/18ef744014fef4df902b6db4647b2cd9 to your computer and use it in GitHub Desktop.
Extraer los colores principales de cada uno de los fotogramas de un conjunto de videos
import numpy as np
import cv2
import os
def main():
path_dir = "/media/sf_videos" # path to the videos
for fname in os.listdir(path_dir):
if fname.lower().endswith(".mkv"): # used extension. I look for ".mkv" files
average_colors_list = list() # list that will contain all the average colors.
print("Reading file [{}]".format(fname))
video_capture = cv2.VideoCapture(os.path.join(path_dir,fname))
readed = True
num_frames = 0
while readed:
readed,image = video_capture.read() # we read each frame
if readed:
num_frames += 1
if num_frames % 24 == 1: # we skip some frames, only processing 1 per every 24
print("Read frame [{}]".format(num_frames))
#cv2.imshow("Captured image",image)
#cv2.waitKey(0)
avg_color = np.uint8(np.average(np.average(image,axis=0), axis=0)) # we average the color of the image
average_color_list.append(np.array([[avg_color]*1]*100, np.uint8)) # we create an image of 1px * 100 px for each color
average_color_img = np.concatenate(average_colors_list,axis=1) # we concatenate all small images in a big one
cv2.imwrite("Avg_"+fname.replace(".mkv",".png"),average_color_img) # save as "file_name.PNG"
print("Total frames: [{}]".format(num_frames))
print("-------------------")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment