博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
广义距离变换
阅读量:4320 次
发布时间:2019-06-06

本文共 4252 字,大约阅读时间需要 14 分钟。

广义距离变换

距离变换操作在二值图像上,广义距离变换可以操作的任何函数上。需要注意的是,常见的距离变换采用的欧式距离为:

dis=(pq)2
而广义距离的变换,采用的是欧式距离的平方。
Df(p)=minqG((pq)2+f(q))

入口:

%%clearall;maxval=1e12;%% 设置isShow=true;% 是否显示结果isUsed=true;%是否使用和显示慢方法。isShowC=true;%是否显示使用c语言的方法。%% change Status to look at the different resultsStatus=true; % Status==true表示0的代价无穷大,非0的代价为0.即黑的代价为无穷大,白的代价为0,反之亦然。%%img=imread('img\tst8.png');img=rgb2gray(img);%img=imresize(img,[100 100]);if isShow    figure(1);    subplot(2,4,1);    imshow(img);    title('原图像');end[h,w]=size(img);fprintf(' 图像的大小:\n 高*宽=%d*%d\n',h,w);f=zeros(size(img));%函数fif Status    f(:,:)=0;    f(find(img<255))=maxval;else    f(:,:)=maxval;    f(find(img<255))=0;endif isShow    subplot(2,4,2);    imshow(f,[]);    title('代价函数');end%%child=f;father=zeros(size(child));%% 使用遍历if isUsed    disp('慢方法的计算时间:');    tic;    res_s=zeros(size(father));%% res_s :res_slow    for i2=1:size(father,1)        for j2=1:size(father,2);            temp=zeros(size(child));            for i1=1:size(child,1)                for j1=1:size(child,2)                    %temp(i1,j1)=child(i1,j1)+sqrt((i1-i2)^2+(j2-j1)^2);                    temp(i1,j1)=child(i1,j1)+(i1-i2)^2+(j2-j1)^2;                end            end            % find min value            res_s(i2,j2)=min(min(temp));            %[h,w]=find(temp==max(max(temp)));        end    end    toc;    if isShow        subplot(2,4,3);        imshow(res_s,[]);        title('慢方法');    endend%% 距离变换,采用x' y的形式。即采用公式(3)和公式(4)% x' y的形式表示,首先,固定x',然后计算括号内的min,然后固定y计算最外层的min% D_f(x,y)=min_x'((x-x')^2+min_y'((y-y')^2+f(x',y')))% D_f(x,y)=min_x'((x-x')^2+D_f|x'(y))disp('广义距离变换quick1:');tic;temp_res=zeros(size(child,1),size(father,2));%x'y%% 固定x'for i=1:size(child,1)% x'    child2=child(i,:);    father2=zeros(1,size(father,2));% y    temp_res(i,:)=my_testdp(child2,father2);endres_q1=zeros(size(father));%% res_q1 : res_quick1%% 固定yfor j=1:size(temp_res,2)%y    child2=temp_res(:,j);    father2=zeros(1,size(father,1));%x    res_q1(:,j)=my_testdp(child2,father2)';endif isShow    subplot(2,4,5);    maxv=max(max(res_q1));    imshow(res_q1,[0 maxv]);    title('广义距离变换quick1');endtoc;%% 距离变换,采用y' x的形式。即采用公式(5)和公式(6)% x' y的形式表示,首先,固定y',然后计算括号内的min,然后固定x计算最外层的min% D_f(x,y)=min_y'((y-y')^2+min_x'((x-x')^2+f(x',y')))% D_f(x,y)=min_y'((y-y')^2+D_f|y'(x))disp('广义距离变换quick2:');tic;temp_res=zeros(size(father,1),size(child,2));%y'x%% 固定y'for i=1:size(child,2)% y'状态的数目    child2=child(:,i);    father2=zeros(1,size(father,1));    temp_res(:,i)=my_testdp(child2,father2)';endres_q2=zeros(size(father));%% res_quick2%% 固定xfor j=1:size(temp_res,1)%    child2=temp_res(j,:);    father2=zeros(1,size(father,2));%x    res_q2(j,:)=my_testdp(child2,father2);endif isShow    subplot(2,4,6);    maxv=max(max(res_q2));    imshow(res_q2,[0 maxv]);    title('广义距离变换quick2');endtoc;%% 换了参数传递的方式disp('广义距离变换quick3:');tic;temp_res=zeros(size(child,1),size(father,2));%x'yfor i=1:size(child,1)% x'    child2=child(i,:);    %father2=zeros(1,size(father,2));% y    temp_res(i,:)=disTrans(child2,1,length(child2),1,size(father,2))';endres_l=zeros(size(father));for j=1:size(temp_res,2)%y    child2=temp_res(:,j);    %father2=zeros(1,size(father,1));%    res_l(:,j)=disTrans(child2,1,length(child2),1,size(father,1))';endif isShow    subplot(2,4,7);    maxv=max(max(res_l));    imshow(res_l,[0 maxv]);    title('广义距离变换quick3');endtoc;%% C++ 距离变换,采用x' y的形式。即采用公式(3)和公式(4)% x' y的形式表示,首先,固定x',然后计算括号内的min,然后固定y计算最外层的min% D_f(x,y)=min_x'((x-x')^2+min_y'((y-y')^2+f(x',y')))% D_f(x,y)=min_x'((x-x')^2+D_f|x'(y))disp('广义距离变换quick1 C++:');% clear memory% clear res_s res_l res_q1 res_q2 ;tic;temp_res=zeros(size(child,1),size(father,2));%x'y%% 固定x'if isShowC    for i=1:size(child,1)% x'        child2=child(i,:);        father2=zeros(1,size(father,2));% y        temp_res(i,:)=dp(child2,father2);    end    res_qc=zeros(size(father));%% res_q1 : res_quick1    %% 固定y    for j=1:size(temp_res,2)%y        child2=temp_res(:,j);        father2=zeros(1,size(father,1));%x        res_qc(:,j)=dp(child2,father2)';    end    if isShow        subplot(2,4,8);        maxv=max(max(res_qc));        imshow(res_qc,[0 maxv]);        title('广义距离变换quick1 C++');    end    toc;end

运行结果:

五种计算方法,严格来说为4种

五种计算方法,严格来说为4种

运行时间

这里写图片描述

转载于:https://www.cnblogs.com/raby/p/5886709.html

你可能感兴趣的文章
多浏览器开发需要注意的问题之一
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
Activity的几种启动跳转方式
查看>>
ng-深度学习-课程笔记-2: 神经网络中的逻辑回归(Week2)
查看>>
正则表达式的搜索和替换
查看>>
个人项目:WC
查看>>
地鼠的困境SSL1333 最大匹配
查看>>
flume+elasticsearch+kibana遇到的坑
查看>>
【MM系列】在SAP里查看数据的方法
查看>>
C#——winform
查看>>
CSS3 transform制作的漂亮的滚动式导航
查看>>
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>
spring中的ResourceBundleMessageSource使用和测试示例
查看>>