matlab曲线区域填充,MATLAB程序如何实现曲线连接已知点
构建区域并进⾏填充
% 初始化设定
close all
% 设置图⽚⼤⼩
pix_x = 190;  %图⽚宽度像素数
pix_y = 190; %图⽚⾼度像素数
margin_h = 0.1;      %⽔平⽅向上留10%空余
margin_v = 0.1;      %垂直⽅向上留10%空余
lamda1 = 0.2; %短边凸的程度(0~0.5)
lamda2 = 0.4; %长边凹的程度(0~0.5)
% 边距象素值
v = margin_v*pix_y;
h = margin_h*pix_x;
% 在矩阵的上半部分随机地取2点
point1_y = round(v+rand*(pix_y/2-v));  %矩阵⾏坐标
point1_x = round(h+rand*(pix_x-2*h));  %矩阵列坐标
point2_y = round(v+rand*(pix_y/2-v));
point2_x = round(h+rand*(pix_x-2*h));
% 4个点的矩阵坐标
P1 = [point1_y; point1_x];
P2 = [point2_y; point2_x];
% 中⼼对称
P3 = [pix_y; pix_x] - P1;
P4 = [pix_y; pix_x] - P2;
% 规定短边凸出,长边凹⼊
if sum((P1-P2).^2) > sum((P1-P4).^2)
temp = lamda1;
lamda1 = lamda2;
lamda2 = temp;
end
Pc1 = (1-lamda1)*(P1+P2)/2 + lamda1*(P3+P4)/2;
Pc2 = (1-lamda2)*(P2+P3)/2 + lamda2*(P4+P1)/2;
Pc3 = (1-lamda1)*(P3+P4)/2 + lamda1*(P1+P2)/2;
Pc4 = (1-lamda2)*(P4+P1)/2 + lamda2*(P2+P3)/2;
t  = linspace(0,1,3);          %曲线参数
ti = linspace(t(1),t(end),80);  %端点之间离散50等分
% 样条插值
xy1 = spline(t,[P1 Pc1 P2],ti);
xy2 = spline(t,[P2 Pc2 P3],ti);
xy3 = spline(t,[P3 Pc3 P4],ti);
xy4 = spline(t,[P4 Pc4 P1],ti);
% 短边凸出
if sum((P1-P2).^2) < sum((P1-P4).^2)      %如果P1P2为短边xy1 = fliplr(bsxfun(@minus,P1+P2,xy1));
xy3 = fliplr(bsxfun(@minus,P3+P4,xy3));
else                                      %如果P1P4为短边
xy2 = fliplr(bsxfun(@minus,P2+P3,xy2));
xy4 = fliplr(bsxfun(@minus,P4+P1,xy4));
end
% 象素坐标
x = [xy1(2,:) xy2(2,:) xy3(2,:) xy4(2,:)];
y = [xy1(1,:) xy2(1,:) xy3(1,:) xy4(1,:)];
% 绘图
如何用matlab将已知点连线figure('Name','原始结果','NumberTitle','off')
I_old = poly2mask([P1(2,:) P2(2,:) P3(2,:) P4(2,:)] ,...
[P1(1,:) P2(1,:) P3(1,:) P4(1,:)],pix_y,pix_x);
imshow(I_old)
figure('Name','曲线化后的结果','NumberTitle','off')
I_new = poly2mask(x,y,pix_y,pix_x);
imshow(I_new)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。