算法的原理我不赘述,很多博主有说,但是我看代码大家都比较神秘,这里放一些关于四步相移法的python代码,仅供后人交流学习。
更多传送门:
【条纹结构光投影】正反格雷码解相算法分享,仅供交流学习
【条纹结构光投影】相位图去噪算法分享,仅供交流学习
四步相移法:条纹图像计算包裹相位
from skimage import io
import numpy as np
import math
def list_dot(l1, l2):
l3 = [0 for i in range(len(l1))]
for i in range(len(l1)):
l3[i] = l1[i] * l2[i]
return l3
# 四张相移图像
ori_img_1 = io.imread('1.bmp')
ori_img_2 = io.imread('2.bmp')
ori_img_3 = io.imread('3.bmp')
ori_img_4 = io.imread('4.bmp')
height,width = ori_img_1.shape
N = 4
P_img = np.zeros((height, width))
A_img = np.zeros((height, width))
B_img = np.zeros((height, width))
for i in range(height):
for j in range(width):
l_i = [ori_img_1[i,j], ori_img_2[i,j], ori_img_3[i,j], ori_img_4[i,j]]
a = 2 * math.pi / N
l_sin = [math.sin(4 * a), math.sin(5 * a), math.sin(6 * a), math.sin(7 * a)]
l_cos = [math.cos(0), math.cos(1 * a), math.cos(2 * a), math.cos(3 * a)]
c1 = sum(list_dot(l_i, l_sin))
c2 = sum(list_dot(l_i, l_cos))
P_img[i, j] = math.atan2(c1, c2)
A_img[i, j] = sum(l_i) / N
B_img[i, j] = 2 * math.sqrt(c1*c1 + c2*c2) / N
# 保存到bmp会被放到0-255
io.imsave('normal.tiff', P_img)
io.imsave('A.tiff', A_img)
io.imsave('B.tiff', B_img)