/////////////原程序
% W = fspecial('gaussian',[3,3],0.6);
% W_P=angle(Ore_inv);
% W_P = imfilter(W_P, W, 'replicate');%复制边缘像素值,防止边缘处出现异常值。
% %瑙e寘瑁?
% % cur_unphase=unwrap_phase(W_P);
% cur_unphase=unwrap_phase1(W_P);
%////////////////////////////////////////////////////////////
%先高斯滤波解包裹再减去高斯滤波相位差
%//////// 高斯滤波辅助解包裹 //////////////////////////////////
W = fspecial('gaussian', [3, 3], 0.6); % 你原本使用的高斯核
% 原始相位图(含包裹)
phi_raw = angle(Ore_inv);
% 平滑后的相位图
phi_smooth = imfilter(phi_raw, W, 'replicate'); % 平滑相位(仅用于辅助解包裹)
% 对平滑相位解包裹
phi_unwrapped_smooth = unwrap_phase(phi_smooth); % 用平滑数据解包裹
% 计算平滑相位的包裹轮数
wrap_count = round((phi_unwrapped_smooth - phi_smooth) / (2 * pi));
% 利用包裹轮数 + 原始相位重建真实连续相位
cur_unphase = phi_raw + wrap_count * 2 * pi;
%///////////////////////////////////////////////
% %实部和虚部滤波/////////////////////////////
% % --- 原始复振幅 ---
% real_part = real(Ore_inv);
% imag_part = imag(Ore_inv);
%
% % --- 设置高斯滤波器 ---
% W = fspecial('gaussian', [3,3], 0.6);
%
% % --- 对实部和虚部分别进行高斯滤波 ---
% real_filtered = imfilter(real_part, W, 'replicate');
% imag_filtered = imfilter(imag_part, W, 'replicate');
%
% % --- 重新计算复数和相位 ---
% filtered_complex = complex(real_filtered, imag_filtered);
% W_P = angle(filtered_complex); % 滤波后的相位
%
% % --- 相位解包裹 ---
% cur_unphase = unwrap_phase(W_P);
% %///////////////////////////////////////////////////////////
% %中值滤波///////////////////////////////////////////
% W_P = angle(Ore_inv); % 提取相位
% W_P = medfilt2(W_P, [3 3], 'symmetric'); % 应用中值滤波(3×3窗口)
% cur_unphase = unwrap_phase(W_P); % 解包裹相位
%
% %//////////////////////////////////////////////////////////
% cur_unphase1=unwrap1(W_P);
load('phase_plane_new_26.mat', 'phase_plane_new_26');%标定所拍摄的平面镜相位
cur_unphase1=cur_unphase-phase_plane_new_26;
% figure
% imshow(W_P)
% figure, surf(W_P), title('未解包裹相位'), shading interp, colorbar
%%
final_phase1 =a_slope_correc(cur_unphase1);
figure, surf(cur_unphase1), title('解包裹后的相位图'), shading interp, colorbar
delta_L = (lambda * final_phase1) / (4 * pi); % 根据公式计算位移(单位:m)
% 将位移数据从米转换为微米(μm)用于显示
delta_L_um1 = delta_L * 1e6;
% 绘制面形图(单位:μm)
figure;surf(delta_L_um1);title('未滤波面形图');shading interp;colorbar;zlabel('μm'); % 添加纵坐标标签,使用\mum表示微米符号
cleaned_data=remove_outliers_local(delta_L_um1);
figure;surf(cleaned_data);title('去除坏点后的面形图');shading interp;colorbar;zlabel('μm');
cha=cleaned_data-delta_L_um1;
shape1_3_26_2=cleaned_data;
% shape1_1=cleaned_data;
figure;surf(cha);title('坏点后的面形图');shading interp;colorbar;zlabel('μm');
% shape1-4-26 = imgaussfilt(cleaned_data, 35);
% figure;surf(phase_plane_new);title('面形图');shading interp;colorbar;zlabel('μm'); % 添加纵坐标标签,使用\mum表示微米符号
% save('shape1-3-26-2.mat', 'shape1_3_26_2');
%以下为框选图像 提取范围
% 假设图像大小为 M × N
[M, N] = size(cleaned_data);
pixel_size = 0.0712; % 每个像素对应的实际距离,单位:μm
% 构建实际坐标轴
x = (0:N-1) * pixel_size;
y = (0:M-1) * pixel_size;
% 显示二维图像并框选
figure;
imagesc(x, y,cleaned_data); % 显示原始面形图(单位μm)
axis image;
title('请用鼠标框选一个区域');
xlabel('X [\mum]');
ylabel('Y [\mum]');
colorbar;
% 框选矩形区域
disp('请用鼠标在图像上框选一个矩形区域...');
roi = drawrectangle('Color','r');
uiwait(msgbox('框选完成后请双击红框或按 ESC 退出', '提示', 'modal'));
% 获取矩形位置 [x_start_um, y_start_um, width_um, height_um]
roi_pos = roi.Position;
x_start_um = roi_pos(1);
y_start_um = roi_pos(2);
x_end_um = x_start_um + roi_pos(3);
y_end_um = y_start_um + roi_pos(4);
fprintf('? 横坐标范围:%.2f μm 到 %.2f μm\n', x_start_um, x_end_um);
fprintf('? 纵坐标范围:%.2f μm 到 %.2f μm\n', y_start_um, y_end_um);
% 将 μm 坐标转换为像素索引
col_start = max(1, floor(x_start_um / pixel_size));
col_end = min(N, ceil(x_end_um / pixel_size));
row_start = max(1, floor(y_start_um / pixel_size));
row_end = min(M, ceil(y_end_um / pixel_size));
% 提取矩形区域数据
selected_region = cleaned_data(row_start:row_end, col_start:col_end);
x_selected = x(col_start:col_end);
y_selected = y(row_start:row_end);
[X_sel, Y_sel] = meshgrid(x_selected, y_selected);
figure;
imagesc(x_selected, y_selected, selected_region);
axis image;
title('视场区域');
xlabel('X [μm]');
ylabel('Y [μm]');
% 添加带单位的colorbar(推荐方法1)
h = colorbar;
h.Label.String = 'μm';
h.Label.FontSize = 12; % 可调整字体大小[2](@ref)
% 绘制三维面形图
figure;
surf(X_sel, Y_sel, selected_region);
title('框选区域的三维面形图');
xlabel('X [\mum]');
ylabel('Y [\mum]');
zlabel('位移 [\mum]');
shading interp;
colorbar;
[M, N] = size(final_phase1);
pixel_size = 0.0712; % 每像素 μm
% 构建坐标轴
x = (0:N-1) * pixel_size;
y = (0:M-1) * pixel_size;
% 显示相位图并让用户点击两点
figure;
imagesc(x, y, cleaned_data);
axis image;
xlabel('X [\mum]');
ylabel('Y [\mum]');
title('请点击两点以测量距离并查看相位截面');
colorbar;
% 让用户点击两点
disp('请用鼠标点击两点...');
[pt_x, pt_y] = ginput(2);
% 将坐标转换为像素索引
col1 = round(pt_x(1) / pixel_size);
row1 = round(pt_y(1) / pixel_size);
col2 = round(pt_x(2) / pixel_size);
row2 = round(pt_y(2) / pixel_size);
% 确保索引在范围内
col1 = min(max(col1, 1), N);
col2 = min(max(col2, 1), N);
row1 = min(max(row1, 1), M);
row2 = min(max(row2, 1), M);
% 计算两点之间的欧氏距离
dist_um = sqrt((pt_x(2) - pt_x(1))^2 + (pt_y(2) - pt_y(1))^2);
fprintf('? 两点间实际距离为:%.2f μm\n', dist_um);
% 使用插值获取两点之间的相位剖面(沿直线)
num_samples = 500;
x_line = linspace(col1, col2, num_samples);
y_line = linspace(row1, row2, num_samples);
line_profile = interp2(cleaned_data, x_line, y_line); % 相位值
% 生成距离轴(μm)
distance_axis = linspace(0, dist_um, num_samples);
% 绘制相位剖面图
figure;
plot(distance_axis, line_profile, 'b', 'LineWidth', 1.5);
xlabel('沿直线距离 [\mum]');
ylabel('面形 [\mum]');
title('两点之间的相位截面图');
grid on;
%[rows, cols] = size(delta_L_um1);
% % 提示输入行号
% prompt = sprintf('请输入要查看的行号(1 ~ %d):', rows);
% row_idx = input(prompt);
% % 校验行号有效性
% if isempty(row_idx) || row_idx<1 || row_idx>rows
% error('行号超出范围,请输入 1 到 %d 之间的整数。', rows);
% end
% 提取该行数据
%profile = delta_L_um1(row_idx, :);
%
% % 绘制该行的截面曲线
% figure;
% x = 1:cols; % 列坐标
% plot(x, profile, 'LineWidth', 1.5);
% xlabel('列索引');
% ylabel('\delta_L\_um1 (μm)');
% title(sprintf('第 %d 行截面曲线', row_idx));
% grid on;
% a=delta_L_um1-delta_L_um;
% figure;surf(a);title('减去畸变的差距');shading interp;colorbar;zlabel('μm'); % 添加纵坐标标签,使用\mum表示微米符号
% % 原始畸变位移图像
% raw_surface = delta_L_um;
%
%高斯滤波去除毛刺
% smooth_surface = imgaussfilt(delta_L_um1,40); % 滤波核越大,平滑越强,可尝试 3~9
% %
% % 可视化比较
% figure;
% subplot(1,2,1);
% surf(delta_L_um1); title('原始带毛刺的面形图');
% shading interp; colorbar; zlabel('μm');
%
% subplot(1,2,2);
% surf(smooth_surface); title('高斯滤波后的光滑面形图');
% shading interp; colorbar; zlabel('μm');
% phase_plane=(4*pi ./ lambda) .*smooth_surface/1e6;
% figure;surf(phase_plane);title('面形图');shading interp;colorbar;zlabel('μm'); % 添加纵坐标标签,使用\mum表示微米符号
% save('phase_plane.mat', 'phase_plane');
最新发布