matlab实现,数据曲线毛刺光滑

实现效果
在这里插入图片描述

[Curve] = Model_optimization2(1:size(M,1),M1);

figure(1);
hold on; 
plot(M1,'k-');
plot(Curve,'g-.');
legend("处理前","处理后");
function [Curve] = Model_optimization(HU_curve,Curve)
%循环遍历相邻数据点,直到所有相邻数据点之间的的差值的绝对值都小于指定的阈值,遍历结束。从第一个点开始遍历,
% 判断当前点A和下一个点B的差值的绝对值,如果大于指定阈值,则继续寻找下下个点C,如果点A和点C差值满足条件,则将点B位置的值替
% 换为 (A+B)/2,此时开始点跳到C点,继续遍历,如果C点不满足条件,也即是存在大于等于2个点是存在较大的偏差的,继续遍历,
% 直到找到满足条件的C点。(允许寻找周围的最多7个点,否则终止寻找,继续下一个数据点的判定)

%%%%%%输入 

% % % tempCurve_b = BONE_curve;
% % % tempCurve = WATER_curve;

Th = 0.005;
index = 1;
end_label = 0;

while (index <= length(HU_curve)-2)
    index
    if(abs(Curve(index) - Curve(index+1))> Th && end_label ==0)
        %%%%%%%%% 判断第3个点是否满足条件
        if(abs(Curve(index) - Curve(index+2))< Th)
            Curve(index+1) = (Curve(index) + Curve(index+2))/2;
            index = index +2 ;
% %             disp("in");
        else
            %%%%如果第3个点不满足,开始计数,5个点内还找不到则放弃,如果5个点内找到满足条件的点,
            %%% 进一步判断,第一个点和当前点的大小关系,判断是单调增还是单调减,如果是单调增,且找了3个点才找到符合条件的点,就按照每次递增1/3的第一个点和当前
            %%% 符合条件的点的绝对值之差
            counter =2;
%             for kkk =3:7
            for kkk =3:40
                if (index+kkk) > length(HU_curve)
                    end_label = 1;
                    break;
                end
                
                if(abs(Curve(index) - Curve(index+kkk))< Th)    
                    break;
                end
                counter = counter +1;
            end
            
            if end_label == 0
                %%%%%%%% 改变对应位置的数据点
                del = abs(Curve(index) - Curve(index+kkk))/counter;
                for ii =1:counter
                    %%% 判断递增还是递减
                    if (index+kkk) <= length(HU_curve)
                        if(Curve(index) > Curve(index+kkk))  %%%递减
                            Curve(index+ii) = Curve(index) - del*ii;
                        else  %%%递减
                            Curve(index+ii) = Curve(index) + del*ii;
                        end
                    end
                end
                index = index + kkk;
            end
        end
    elseif(abs(Curve(index) - Curve(index+1))<= Th && end_label ==0)
         index = index +1;
    else
        break;          
    end
end

end


上一篇:【人工智能基础】机器学习基础