讲四个偏振分量图片组合成一张DoFP图像。
def recover_dofp(img,name):
dofp = np.zeros((img.shape[0]*2,img.shape[1]*2))
# print(dofp.shape)
img0 = img[:, :, 0]
img45 = img[:, :, 1]
img90 = img[:, :, 2]
img135 = img[:, :, 3]
dofp[0:dofp.shape[0]:2, 0:dofp.shape[1]:2] = img90
dofp[1:dofp.shape[0]:2, 0:dofp.shape[1]:2] = img135
dofp[0:dofp.shape[0]:2, 1:dofp.shape[1]:2] = img45
dofp[1:dofp.shape[0]:2, 1:dofp.shape[1]:2] = img0
return dofp
一、下列的代码可以实现将四张不同偏振方向的图片合成一张DoFP图像
import cv2
import numpy as np
import os
import glob
def cal_stokes_dolp(img,name):
dofp = np.zeros((img.shape[0]*2,img.shape[1]*2))
# print(dofp.shape)
img0 = img[:, :, 0]
img45 = img[:, :, 1]
img90 = img[:, :, 2]
img135 = img[:, :, 3]
dofp[0:dofp.shape[0]:2, 0:dofp.shape[1]:2] = img90
dofp[1:dofp.shape[0]:2, 0:dofp.shape[1]:2] = img135
dofp[0:dofp.shape[0]:2, 1:dofp.shape[1]:2] = img45
dofp[1:dofp.shape[0]:2, 1:dofp.shape[1]:2] = img0
return dofp
def imsave_s0_dolp(dofp,path,name):
cv2.imwrite(os.path.join(path,"{}_dofp.png".format(name)),dofp)
print(name)
input_dir = glob.glob(os.path.join(os.getcwd()+"/test/merge/input","*"))
label_dir = glob.glob(os.path.join(os.getcwd()+"/test/merge/label","*"))
# output_dir = glob.glob(os.path.join(os.getcwd()+"/test_result/merge","*"))
input_dir.sort(key=lambda x: int(x.split('/')[-1].split('.')[0]))
label_dir.sort(key=lambda x: int(x.split('/')[-1].split('.')[0]))
# output_dir.sort(key=lambda x: int(x.split('/')[-1].split('.')[0]))
assert len(input_dir)==len(label_dir)
#assert len(output_dir)==len(input_dir)
for i in range(len(input_dir)):
input_img = cv2.imread(input_dir[i],-1)
label_img = cv2.imread(label_dir[i],-1)
# output_img = cv2.imread(output_dir[i],-1)
name = input_dir[i].split('/')[-1].split('.')[0]
input_dofp= cal_stokes_dolp(input_img,name)
label_dofp = cal_stokes_dolp(label_img,name)
# output_s0, output_dolp = cal_stokes_dolp(output_img)
path_input = os.getcwd() + "/test_result/DoFP/input"
path_label = os.getcwd() + "/test_result/DoFP/label"
# path_output = os.getcwd() + "/test_result/S0_DoLP/output"
path = [path_input,path_label]
for i in path:
if not os.path.isdir(i):
os.makedirs(i)
imsave_s0_dolp(input_dofp,path_input,name)
imsave_s0_dolp(label_dofp,path_label,name)
将一张2048*2448的DoFP图像拆分为四个分量图
def get_depart(img):
# print(name)
img = np.array(img)
img90 = img[0:img.shape[0]:2,0:img.shape[1]:2]
img135 = img[1:img.shape[0]:2,0:img.shape[1]:2]
img45 = img[0:img.shape[0]:2,1:img.shape[1]:2]
img0 = img[1:img.shape[0]:2,1:img.shape[1]:2]
return img0,img45,img90,img135
二、下列的代码可以实现将一张DoFP拆分成四张不同的偏振图像
import cv2
import numpy as np
import os
import glob
def img_save(name,img0,img45,img90,img135,S0,DoLP,AoP):
path = os.path.join(os.getcwd()+'/result',name)
if not os.path.isdir(path):
os.mkdir(path)
cv2.imwrite(path+'/{}_0.png'.format(name),img0)
cv2.imwrite(path+'/{}_45.png'.format(name), img45)
cv2.imwrite(path+'/{}_90.png'.format(name), img90)
cv2.imwrite(path+'/{}_135.png'.format(name), img135)
cv2.imwrite(path+'/{}_s0.png'.format(name), S0)
cv2.imwrite(path+'/{}_dolp.png'.format(name),DoLP)
cv2.imwrite(path + '/{}_aop.png'.format(name),AoP)
def cal_stokes_dolp(img,name):
print(name)
img = np.array(img)
img90 = img[0:img.shape[0]:2,0:img.shape[1]:2]
img135 = img[1:img.shape[0]:2,0:img.shape[1]:2]
img45 = img[0:img.shape[0]:2,1:img.shape[1]:2]
img0 = img[1:img.shape[0]:2,1:img.shape[1]:2]
S0 = (img0.astype(np.float32) + img45.astype(np.float32) +
img90.astype(np.float32) + img135.astype(np.float32)) * 0.5
S1 = img0.astype(np.float32) - img90.astype(np.float32)
S2 = img45.astype(np.float32) - img135.astype(np.float32)
#归一化一下,因为数据都是在0.0几的范围内
DoLP = np.sqrt((S1 ** 2 + S2 ** 2) / S0**2)
DoLP = np.clip(DoLP*255,0,255)
AoP = 1 / 2 * np.arctan2(S2, S1)
AoP = np.clip(AoP * 255, 0, 255)
img_save(name,img0,img45,img90,img135,S0,DoLP,AoP)
return
img_path = os.getcwd() + "/DoFP_ori"
list_dir = glob.glob(os.path.join(img_path,'*.png'))
for i in range(len(list_dir)):
img = cv2.imread(list_dir[i],0)
name = list_dir[i].split('/')[-1].split('.')[0].split('_')[0]
cal_stokes_dolp(img,name)
将一张图片拆分为四个子图,纵横分别在中间一刀切
def depart_and_save(img,save_name,str):
high,wide = img.shape
high_mid = high//2
wide_mid = wide//2
img0 = img[high_mid:high,wide_mid:wide]
img45 = img[0:high_mid,wide_mid:wide]
img90 = img[0:high_mid,0:wide_mid]
img135 = img[high_mid:high,0:wide_mid]
img_depart_save(img0,img45,img90,img135,save_name,str)