Last active
February 3, 2023 00:18
-
-
Save akirayou/8b671f8c1d9396994dd3f21b794e7f10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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