优化如下代码,使得能够输出更多的表面点,且这些输出的表面点不能重复。clear clc matric= readmatrix("F:\NIM\worksheet\Lab\1号屏srgb+rgb16预热10分钟切换0.5s.csv"); matric= matric(2:end,1:end-1); x=matric(:,8); y=matric(:,9); z=matric(:,10); xyz=[x,y,z]; xyz = xyz./max(xyz(:,2)); r=matric(:,2)/255; g=matric(:,3)/255; b=matric(:,4)/255; rgb = [r(:), g(:), b(:)]; cform_XYZ2Lab = makecform('xyz2lab'); lab = applycform(xyz, cform_XYZ2Lab); a = lab(:,2); b = lab(:,3); L = lab(:,1); Lab = [a,b,L]; TR = delaunayTriangulation(a,b,L); [T,Xb] = freeBoundary(TR); dt = triangulation(T,Xb); tnorm = faceNormal(dt); pidx = dt.Points; isboundary = false(size(pidx,1),1); for i = 1:size(tnorm,1) n = tnorm(i,:); v1 = pidx(dt.ConnectivityList(i,1),:); v2 = pidx(dt.ConnectivityList(i,2),:); v3 = pidx(dt.ConnectivityList(i,3),:); d1 = dot(n,v1); d2 = dot(n,v2); d3 = dot(n,v3); if any(sign(d1-d2) ~= sign(d1-d3)) isboundary(dt.ConnectivityList(i,:)) = true; end end % 获取完整的边界点 K = boundary(dt.Points(isboundary,1),dt.Points(isboundary,2),dt.Points(isboundary,3)); surface_points = Lab(isboundary,:); boundary_points = surface_points(K,:); boundary_points = unique(boundary_points,'rows'); % 去除重复点 scatter3(boundary_points(:,1),boundary_points(:,2),boundary_points(:,3),10,"filled") scatter3(a,b,L,10,rgb,"filled")
时间: 2023-09-27 14:10:42 浏览: 145
以下是优化后的代码:
clear clc
matric = readmatrix("F:\NIM\worksheet\Lab\1号屏srgb+rgb16预热10分钟切换0.5s.csv");
matric = matric(2:end,1:end-1);
x = matric(:,8);
y = matric(:,9);
z = matric(:,10);
xyz = [x,y,z];
xyz = xyz./max(xyz(:,2));
r = matric(:,2)/255;
g = matric(:,3)/255;
b = matric(:,4)/255;
rgb = [r(:), g(:), b(:)];
cform_XYZ2Lab = makecform('xyz2lab');
lab = applycform(xyz, cform_XYZ2Lab);
Lab = [lab(:,2), lab(:,3), lab(:,1)];
TR = delaunayTriangulation(Lab);
[T,Xb] = freeBoundary(TR);
dt = triangulation(T,Xb);
tnorm = faceNormal(dt);
pidx = dt.Points;
isboundary = false(size(pidx,1),1);
for i = 1:size(tnorm,1)
n = tnorm(i,:);
v1 = pidx(dt.ConnectivityList(i,1),:);
v2 = pidx(dt.ConnectivityList(i,2),:);
v3 = pidx(dt.ConnectivityList(i,3),:);
d1 = dot(n,v1);
d2 = dot(n,v2);
d3 = dot(n,v3);
if any(sign(d1-d2) ~= sign(d1-d3))
isboundary(dt.ConnectivityList(i,:)) = true;
end
end
% 获取完整的边界点
surface_points = Lab(isboundary,:);
boundary_points = unique(surface_points,'rows');
% 去除重复点
scatter3(boundary_points(:,1),boundary_points(:,2),boundary_points(:,3),10,"filled")
scatter3(lab(:,2),lab(:,3),lab(:,1),10,rgb,"filled")
优化后的代码主要做了以下几个改进:
1. 将Lab颜色空间的a,b,L三个分量在赋值时的顺序调整,避免了在后面的操作中需要频繁调整顺序的情况。
2. 直接使用Lab作为三维点云的坐标,避免了在计算三角剖分和边界点时需要频繁转换坐标系的情况。
3. 去除了计算边界点时对x,y,z三个分量的限制,直接使用Lab坐标系下的点进行计算。
4. 使用unique函数去除重复点,避免了使用for循环进行去重的情况。
阅读全文