公式推导:
代码
clc;
close all;
clear;
%% [1] 参数设置
width = 1024; % 投影仪的图像宽度
heigth = 768; % 投影仪的图像高度
freq = [70,64,59]; % 宽度方向有多少个周期
T = width./freq; % 条纹周期(一个正弦周期下的像素个数)
M = length(freq); % 频率个数
A = 128; % 背景强度
B = 127; % 调制强度
N = 4; % 相移步数
save_stripes = true; % 是否保存条纹图
stripes_folder = 'stripes';
%% [2] 条纹生成
stripes = cell(M,N);
if(save_stripes)
mkdir(stripes_folder);
end
for i=1:M
for j=1:N
% 生成图像
temp = zeros(heigth,width);
for col = 1:width
phi = 2*pi*col/T(i);
delta_phi = 2*pi*(j-1)/N;
temp(:,col) = A + B*cos(phi + delta_phi);
end
stripes{i,j} = temp;
% 保存图像
if(save_stripes)
save_path = [stripes_folder,'/',int2str(i),'_',int2str(j),'.png'];
imwrite(mat2gray(stripes{i,j}),save_path);
end
end
end
%% [3] 包裹相位(相位主值)
wrapped_phase = cell(M,1);
for i = 1:M
sin_sum = 0;
cos_sum = 0;
for j = 1: N
Ik = stripes{i, j};
sin_sum = sin_sum + Ik * sin(2 * (j - 1) * pi / N);
cos_sum = cos_sum + Ik * cos(2 * (j - 1) * pi / N);
end
temp = -atan2(sin_sum, cos_sum);
wrapped_phase{i,1} = temp;
% 保存图像
if(save_stripes)
save_path = [stripes_folder,'/',int2str(i),'_相位主值.png'];
imwrite(mat2gray(wrapped_phase{i,1}),save_path);
end
end
%% [4] 绝对相位(多频外差法进行相位展开)
unwrapped_phase = wrapped_phase;
unwrapped_phase_name = cell(M,1);
for i=1:M
unwrapped_phase_name{i,1} = int2str(i);
end
% 倒二叉树外差计算
for i=M-1:-1:1
for j=1:i
phi1 = unwrapped_phase{j,1};
phi2 = unwrapped_phase{j+1,1};
mask = phi1 > phi2;
pha12 = (phi1 - phi2) .* mask + (2 * pi - (phi2 - phi1)) .* (1 - mask);
unwrapped_phase{j,1} = pha12;
% 保存图像
if(save_stripes)
phase_name = [unwrapped_phase_name{j,1},'_',unwrapped_phase_name{j+1,1}];
unwrapped_phase_name{j,1} = phase_name;
save_path = [stripes_folder,'/',phase_name,'_外差.png'];
imwrite(mat2gray(unwrapped_phase{j,1}),save_path);
end
end
end
输出结果
