博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现图像的局部放大
阅读量:4029 次
发布时间:2019-05-24

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

转自:http://blog.sina.com.cn/s/blog_6163bdeb0100i2ha.html

网上看到的matlab局部放大代码,认真看了看,学了不少东西。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [Hza,Hzr]=mmzoom(arg)

% 用法:

% [Hza,Hzr] = MMZOOM 返回创建的放大坐标系和方法框的句柄

% MMZOOM DRAG 使能放大框的拖放功能

% MMZOOM RESET 取消放大框的拖放功能

% MMZOOM OFF 删除放大框

% 常用用法:画好图片后输入mmzoom,圈图片放大,可以拖动放大框到理想位置,点击figure的非主坐标

% 系区域确定;还想拖动,输入mmzoom drag;删除放大框,输入mmzoom off删除

 

if nargin==0%无输入参数,则为无参数

    arg = [];

end

if isempty(arg) %无参数表示放大

    Hzoom = findobj(0,'Tag','MMZOOM'); % 找之前的放大窗口,有的话删掉

    if ~isempty(Hzoom)  

        delete(Hzoom)

    end

   

    [xlim,ylim,prect] = getbox;% 获得一个矩形框

   

    if ~isempty(prect) % 区域不能为空

        Haxes = gca;

        Hzr = rectangle('Position',prect,...%画矩形框

            'Linestyle',':',...              

            'Tag','MMZOOM');

       

        Hfig = gcf;

        Hzoom = copyobj(Haxes,Hfig); %复制一个坐标系到当前窗口

       

        OldUnits = get(Haxes,'Units');  %得到坐标系的单位

        set(Haxes,'Units','normalized') %设置坐标系的单位

        Pvect = get(Haxes,'Position');

        set(Haxes,'Units',OldUnits)

       

        alpha = 1/3;   %放大窗口缩放比例

        beta = 98/100; %放大窗口偏移

       

        Zwidth = alpha*Pvect(3);                 

        Zheight = alpha*Pvect(4);                

        Zleft = Pvect(1)+beta*Pvect(3)-Zwidth;   

        Zbottom = Pvect(2)+beta*Pvect(4)-Zheight;

       

        set(Hzoom,'units','Normalized',...            % make units normalized

            'Position',[Zleft Zbottom Zwidth Zheight],...% axes position

            'Xlim',xlim,'Ylim',ylim,...                % axis data limits

            'Box','on',...                             % axis box on

            'Xgrid','off','Ygrid','off',...            % grid lines off

            'FontUnits','points',...

            'FontSize',8,...                           % shrink font size

            'ButtonDownFcn','selectmoveresize',...      % 设置放大坐标系可拖动和缩放

            'Tag','MMZOOM',...                         % tag zoomed axes

            'UserData',Haxes)                          % store original axes

       

        [Htx,Hty,Htt] = getn(Hzoom,'Xlabel','Ylabel','Title');%得到多个属性的句柄

        set([Htx,Hty,Htt],'String','')%设置相应的句柄string属性为空

       

        set(Haxes,'DeleteFcn',...           % 设置原坐标系的删除函数

            'delete(findobj(0,''Type'',''axes'',''Tag'',''MMZOOM''))')

       

        Hchild = findobj(Hfig,'type','axes');

        Hchild(Hchild==Hzoom) = [];          

       

        set(Hfig,'Children',[Hzoom;Hchild],...%将放大的坐标系放在栈顶

            'CurrentAxes',Haxes,...    %使原坐标系为当前坐标系,如果再画图将在当前坐标系绘图

            'ButtonDownFcn','mmzoom reset')    %点击figure空白区域响应此函数

       

        if nargout>=1 

            Hza = Hzoom;

        end

    end

   

elseif strncmpi(arg,'d',1) %使能拖动放大框

   

    Hzoom = findobj(0,'Type','axes','Tag','MMZOOM');

    if ~isempty(Hzoom)

        set(Hzoom,'ButtonDownFcn','selectmoveresize')%设置放大框可拖动

    end

   

elseif strncmpi(arg,'r',1) %使放大框不能再被拖动

   

    Hzoom = findobj(0,'Type','axes','Tag','MMZOOM');

    if ~isempty(Hzoom)

        [Hfig,Haxes] = getn(Hzoom,'Parent','UserData');

        set(Hzoom,'ButtonDownFcn','','Selected','off')

        set(Hfig,'CurrentAxes',Haxes)                

    end

   

elseif strncmpi(arg,'o',1)  %删除放大框

   

    Hzoom = findobj(0,'Tag','MMZOOM');

    if ~isempty(Hzoom)

        delete(Hzoom)

    end

else

    error('Unknown Input Argument.')

end

%%%%%%%%%%%%%%%%

function [xbox,ybox,prect]=getbox

 

if waitforbuttonpress %等待鼠标按下,按下鼠标返回False,按下按键返回True

    return

end

 

Hf = gcf;      % 得到鼠标点击的窗口

Ha = gca(Hf);  % 得到鼠标点击的坐标系

 

AxesPt = get(Ha,'CurrentPoint'); % 得到第一个鼠标点击数据点在坐标系中的位置

FigPt = get(Hf,'CurrentPoint');  % 坐标轴的CurrentPoint值为一个2*3的矩阵,第一行为离观察者最

%近的点的三维坐标,第2行为离观察者最远的点的三维坐标。默认的视角View = 90度的情况下,这两行的

%x和y坐标是相同的。一般情况下,只需要取pos第1行的前两个元素,第3个元素为z轴坐标,一般不用。

 

rbbox([FigPt 0 0],FigPt) % 用鼠标圈出矩形框,鼠标松开时返回

 

AxesPt = [AxesPt;get(Ha,'CurrentPoint')];%组合起始点和终止点

 

[Xlim,Ylim] = getn(Ha,'Xlim','Ylim');%得到一个对象的多个属性的句柄

 

xbox = [min(AxesPt(:,1)) max(AxesPt(:,1))]; %得到所画矩形框两角点的横坐标

xbox = [max(xbox(1),Xlim(1)) min(xbox(2),Xlim(2))]; %点不能在边界外

 

ybox = [min(AxesPt(:,2)) max(AxesPt(:,2))];

ybox = [max(ybox(1),Ylim(1)) min(ybox(2),Ylim(2))];

 

prect = [xbox(1) ybox(1) diff(xbox) diff(ybox)];

 

%%%%%%%%%%%%%%%%

function varargout=getn(H,varargin)%一般varargin和varargout是隐藏的,但可像这里作为传入参数用,

%注意为元胞类型

% 得到一个对象的多个属性

 

if max(size(H))~=1 || ~ishandle(H)

   error('Scalar Object Handle Required.')

end

varargout=get(H,varargin);

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

 

学到的知识:

findobj

rectangle

copyobj

'ButtonDownFcn','selectmoveresize'

'CurrentAxes'

'Children'

waitforbuttonpress

%%%%%%%%%%%%%%%%matlab示例:

w = waitforbuttonpress;
if w == 0
    disp('Button click')
else
    disp('Key press')
end

%%%%%%%%%%%%%%%%

'CurrentPoint'

rbbox

%%%%%%%%%%%%%%%%matlab示例:

k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint');    % button down detected
finalRect = rbbox;                   % return figure units
point2 = get(gca,'CurrentPoint');    % button up detected
point1 = point1(1,1:2);              % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2);             % calculate locations
offset = abs(point1-point2);         % and dimensions
x = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
y = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
hold on
axis manual
plot(x,y)                            % redraw in dataspace units

%%%%%%%%%%%%%%%%

 

varargin、varargout

 

另一摘自:http://blog.sina.com.cn/s/blog_7420dcc701019h9r.html

照旧感谢原作者,分享者们,阿门!

以下三种方法,szlqq345喜欢用第一种的。

第一种:magnify是个动态放大镜,固化后可以用tools>edit plot移动小图,能选取多个局部图,这个方法不错

用法:打开figure图,输入magnify,左键动态选取查看,ctrl+左键固化,也可右键固化,‘<’和‘>’缩放方法范围,‘+’和‘-’缩放放大比例

原帖:

 

第二种:用起来也很方便,缺点是只能框选一处,不能选取多个。

原帖: 

美国学者
and
编写的MasteringMATLAB7上的例子,实现图中图缩放功能,使用了3个函数,这三个函数在附件中,当然也可以到网站去下载。

函数简单介绍:

getn()将get()函数的
输出
参数
简化为单个
变量

getbox()实现矩形区域的选择,并捕捉该区域的横纵标的范围

mmzoom创建一个缩放
坐标
轴。


试图做了例子,传上来一起分享。

例子: 
 
 
  
 
  
x = -pi:pi/12:pi;
 
  
 
  
y = tan(sin(x)) - sin(tan(x));
 
  
 
  
plot(x,y,'-- ro','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g',
 
  
 
  
'MarkerSize',7.5)
 
  
 
  
mmzoom 
 
 
 
 
 

 

第三种:也可编个小程序,分别在两个图形句柄里画图,可以借鉴下

原帖:

代码;

figure(1);

h2=axes('position',[0 0 1 1]);
axis(h2);
x2=0:pi/50:2*pi;
y2=sin(x2);
h3=plot(x2,y2,'b-');
h1=axes('position',[0.3 0.2 0.4 0.4]);
axis(h1);
x1=0:pi/50:2*pi;
y1=cos(x1);
h4=plot(x1,y1,'r-');
hold on
h=[h3; h4];
str=['大图中的曲线';'小图中的曲线'];
legend(h,str);

转载地址:http://hgmbi.baihongyu.com/

你可能感兴趣的文章
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
关于activity保存页面状态的两个方法
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
关于let{a}=B出现的解构赋值
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
android给文字加边框(修改不能居中的问题)
查看>>
coursesa课程 Python 3 programming course_2_assessment_1
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
coursesa课程 Python 3 programming Dictionary methods 字典的方法
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming Tuple Assignment with Unpacking
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>