Skip to content

Instantly share code, notes, and snippets.

@akirayou
Created August 29, 2021 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akirayou/754af5565918c5cdb1a2167efc2f3eca to your computer and use it in GitHub Desktop.
Save akirayou/754af5565918c5cdb1a2167efc2f3eca to your computer and use it in GitHub Desktop.
Cut RICHO THETA's movie frame for meshroom
# -*- coding: utf-8 -*-
"""
Created on Sun May 9 20:45:45 2021
同一フォルダにdummy.JPGとして、適当なTHETAで撮影した写真を置いておく(EXIFをコピーして流用する)
同一フォルダにある*.MP4を全部パースして[ファイル名]rigフォルダ0/1にそれぞれのカメラの画像を切りだす。
vignette処理もしてくれる
注意:
撮影時には適宜、カメラをその場回転(雑で良い)を入れて、二つのカメラの位置関係をmeshroomが認識出来るようにすること。
@author: youak
"""
#設定
FRAME_SKIP=10 #FRAME_SKIP回に1枚の画像を使う
is_rig_cam=True #出力ファイルを0/1ディレクトリに分けてrigとして認識させる
import sys
sys.path.append(".")
import os
import cv2
import pyexiv2
import piexif
import numpy as np
# ファイルオープン
import rawpy
import imageio
import re
maxFrame=999999
exif_dict = piexif.load("dummy.JPG")
def add_serial(output_file_name,rig_index,im):
global exif_dict
exif_dict["0th"][piexif.ImageIFD.ImageWidth] = (im.shape[1], 1)
exif_dict["0th"][piexif.ImageIFD.ImageLength] = (im.shape[0], 1)
exif_dict["0th"][piexif.ImageIFD.XResolution] = (im.shape[1], 1)
exif_dict["0th"][piexif.ImageIFD.YResolution] = (im.shape[0], 1)
exif_dict['Exif'][piexif.ExifIFD.PixelXDimension] = im.shape[1]
exif_dict['Exif'][piexif.ExifIFD.PixelYDimension] = im.shape[1]
exif_dict["0th"][piexif.ImageIFD.CameraSerialNumber]= bytes('00{}'.format(rig_index), 'utf-8')
exif_dict['Exif'][piexif.ExifIFD.LensSerialNumber] = bytes('00{}'.format(rig_index), 'utf-8')
exif_dict['Exif'][piexif.ExifIFD.BodySerialNumber] = bytes('00{}'.format(rig_index), 'utf-8')
#exif_dict['Exif'][piexif.ExifIFD.FocalLength] = (3,1)
exif_dict['0th'][piexif.ImageIFD.Model] = bytes('CUTED', 'utf-8')
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, output_file_name)
def _add_serial(file,s):
img = pyexiv2.Image( file )
metadata = img.read_exif()
metadata["Exif.Image.CameraSerialNumber"] = s
img.modify_exif( metadata )
img.close()
def iwrite(file,img,s):
cv2.imwrite(file,img)
add_serial(file,s,img)
def main(file):
d=file+"rig"
try:
os.mkdir(d)
except:
return
try:
if is_rig_cam:
for i in range(2):
os.mkdir("{}/{}".format(d,i))
except:
pass
mask=None
cap_file = cv2.VideoCapture(file)
count=0
while True:
ret,img=cap_file.read()
if not ret: break
count += 1
if count %FRAME_SKIP != 0: continue
#img=rawpy.imread(file).postprocess()[:,:,::-1]
print(img.shape)
simg=np.array_split(img, 2, axis=1)
for i,f_img in enumerate(simg):
if i==0:
f_img=f_img.transpose(1,0,2)[:,::-1]
else:
f_img=f_img.transpose(1,0,2)[::-1]
#ケラレを特徴量として捉えないようにぼかしてmaskする
if mask is None:
mask=np.zeros(f_img.shape,np.float32)
h=f_img.shape[0]
w=f_img.shape[1]
cv2.circle(mask, center=(h // 2, w // 2), radius=int(h/2*0.8) , color=(1,1,1), thickness=-1)
mask=cv2.GaussianBlur(mask,(int(h/2*0.4)//2*2+1,)*2,0)
f_img=(f_img.astype(np.float32)* mask).astype(np.uint16)
new_file="{:06}".format(count)
if is_rig_cam:
iwrite(d+"/"+str(i)+"/"+new_file+".jpg",f_img,str(i))
else:
num=count
if i == 1:
num = maxFrame - count
new_file="{:06}".format(num)
iwrite(d+"/"+new_file+".jpg",f_img,str(i))
import glob
for f in glob.glob("*.mp4"):
main(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment