Skip to content

Instantly share code, notes, and snippets.

@akirayou
Last active Jul 1, 2021
Embed
What would you like to do?
# -*- coding: utf-8 -*-
"""
Created on Sun May 9 20:45:45 2021
@author: youak
"""
import glob
files=glob.glob("*.DNG")
is_rig_cam=True #meshroom では depth推定できないような配置のrigカメラは逆効果っぽい(dence画像作成時にコケる)
import sys
sys.path.append(".")
import os
import cv2
import piexif
import numpy as np
# ファイルオープン
import rawpy
import imageio
import re
def add_serial(output_file_name,rig_index,org_file,im):
exif_dict = piexif.load(org_file)
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 iwrite(file,img,s,org_file):
cv2.imwrite(file,img)
add_serial(file,s,org_file,img)
try:
os.mkdir("rig")
except:
pass
try:
if is_rig_cam:
for i in range(2):
os.mkdir("rig/{}".format(i))
except:
pass
mask=None
for file in files:
print(file)
img=rawpy.imread(file).postprocess()[:,:,::-1]
simg=np.array_split(img, 2, axis=1)
for i,f_img in enumerate(simg):
#ケラレを特徴量として捉えないようにぼかして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.9) , 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=re.sub('[^0-9]','',file)
if is_rig_cam:
iwrite("rig/"+str(i)+"/"+new_file+".jpg",f_img,str(i),file[:-4]+".JPG")
else:
iwrite("rig/"+new_file+"_"+str(i)+".jpg",f_img,str(i),file[:-4]+".JPG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment