加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

matlab实现的画中画放大代码(转载)

(2011-04-22 09:20:30)
标签:

matlab

画中画

教育

分类: Matlab学习

matlab实现的画中画放大代码

经过几天的修改,终于完成了稍微像样的Matlab程序

http://s9/middle/49d955154a177ac13bcc8&690


function myzoom
%% 综合实例
% 俗话说,一图值千字。同样,对于应用软件而言,一例值千言。
%% 画中画缩放程序
myDraw   %绘原始图
while 1
    try
        if waitforbuttonpress % 按键时返回1,%关闭图像时,waitforbuttonpress产生错误
            close gcf
            break
        end
    catch
        return;
    end
    [xlim,ylim,prect] = getbox; %获取选取框
    if prect(3)==0 || prect(4)==0
        continue
    end
    mmzoom(xlim,ylim,prect)   %放大窗口
end
%% getn
function varargout=getn(H,varargin)
% 演示了可变参数及可变返回值的使用
% This simplifies the construct
% [Xlim,Ylim,Xlabel] = deal(get(gca,{'Xlim','Ylim','Xlabel'}))
if max(size(H))~=1 || ~ishandle(H)
   error('Scalar Object Handle Required.')
end
varargout=get(H,varargin);
%% myDraw
function myDraw
t = linspace(0,1); %等分100份
x = sin(2*pi*t);
y = 1.2*cos(4*pi*t);
z = exp(-2*t).*sin(3*pi*t - pi/4);
plot(t,x,t,y,'-.',t,z,'--')
title('画中画示例.');xlabel 't',ylabel('X, Y, Z')
h=legend('x','y','z',2); %设置图例,0--空地,1--右上,2--左上
    set(gca,'ButtonDownFcn','selectmoveresize') %图形可以选中、移动、改变大小
%% getbox
function [xbox,ybox,prect]=getbox
%获取选择框
Hf = gcf;      % get current figure when button was pressed
Ha = gca(Hf); % get current axes where button was pressed
AxesPt = get(Ha,'CurrentPoint'); % 返回当前坐标系统下的位置,3D。get first axes data point clicked
FigPt = get(Hf,'CurrentPoint'); % 返回当前图形坐标下的位置,与用户坐标无关,2D。get first figure point clicked
rbbox([FigPt 0 0],FigPt) % function returns as soon as mouse button is up
AxesPt = [AxesPt;get(Ha,'CurrentPoint')];
[Xlim,Ylim] = getn(Ha,'Xlim','Ylim');
xbox = [min(AxesPt(:,1)) max(AxesPt(:,1))]; % x axis limits of selection
xbox = [max(xbox(1),Xlim(1)) min(xbox(2),Xlim(2))]; % limit to axes size
ybox = [min(AxesPt(:,2)) max(AxesPt(:,2))];
ybox = [max(ybox(1),Ylim(1)) min(ybox(2),Ylim(2))]; % limit to axes size
prect = [xbox(1) ybox(1) diff(xbox) diff(ybox)]; % position rectangle
%% MMZOOM
function mmzoom(xlim,ylim,prect)
Hzoom = findobj(0,'Tag','MMZOOM'); % find previous zoomed axes
if ~isempty(Hzoom)   % delete prior zoomed axes if it exists
    delete(Hzoom)
end
Hzr = rectangle('Position',prect,'Linestyle',':','Tag','MMZOOM');

Haxes = gca; % handle of axes where selection box was drawn
Hzr = rectangle('Position',prect,'Linestyle',':','Tag','MMZOOM');
Hfig = gcf; % handle of Figure where selection box was drawn
Hzoom = copyobj(Haxes,Hfig); % copy original axes and its children
Pvect=getNormalSize(Haxes,'Position');
alpha = 1/3;   % position scaling for zoomed axes
beta = 98/100; % position shift for zoomed axes
Zwidth = alpha*Pvect(3);                  % zoomed axes width
Zheight = alpha*Pvect(4);                 % zoomed axes height
Zleft = Pvect(1)+beta*Pvect(3)-Zwidth;    % zoomed axes left
Zbottom = Pvect(2)+beta*Pvect(4)-Zheight; % zoomed axes bottom
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
    'Tag','MMZOOM',...                         % tag zoomed axes
    'UserData',Haxes)                          % store original axes
[Htx,Hty,Htt] = getn(Hzoom,'Xlabel','Ylabel','Title');
set([Htx,Hty,Htt],'String','')      % delete labels on zoomed axes

% place zoomed axes at top of object stack
Hchild = findobj(Hfig,'type','axes'); % get all axes in figure
Hchild(Hchild==Hzoom) = [];           % remove zoomed axes from list
set(Hfig,'Children',[Hzoom;Hchild],'CurrentAxes',Haxes,'ButtonDownFcn','mmzoom reset')
%% getNormalSize
function [normalValue]=getNormalSize(Ha,prName)
OldUnits = get(Ha,'Units'); % get position vector of original
set(Ha,'Units','normalized') % axes in normalized units
normalValue = get(Ha,prName);
set(Ha,'Units',OldUnits)
来源:http://hi.baidu.com/imhuanxi/blog/item/5d1a7629c6575df499250ad7.html

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

更新

Matlab官网的File Exchange有一个不错的例子

magnify
version 1.0 (4.13 KB) by Rick Hindman
Turns the mouse into a pop-up magnifying glass to look at details of 2D plots.

Ever wish MATLAB had a magnifying glass so you could look at the details of a small region of your plots without having to zoom in and out again and again? Just run 'magnify.m' with the figure of interest as the current figure, then use the left mouse button to bring up a magnified veiw that you control. Or use the 'Ctrl' key while clicking to bring up a magnifying glass that 'locks' onto the figure when released (for use when copying and printing figures). Use the '<' and '>' keys to make the magnifying glass smaller or larger. Use '-' and '+' to decrease or increase the magnification level.

http://cn.mathworks.com/matlabcentral/fileexchange/5961-magnify

可免费注册mathworks账户,然后下载。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有