matlab矩阵缩小和放大

matlab矩阵缩小和放大,第1张

Matlab里的imresize函数可以对图像放大和缩小,但这同时也会改变图像矩阵的大小,如果想要上图所示的结果,需要再进行一些处理,处理代码如下所示。

clc

close all

% 从当前目录下打开一张图片

[filename, filepath] = uigetfile({'*.jpg*.ppmjpeg *.*.bmp*.png'},'Choose Input Image')

if isequal(filename,0) || isequal(filepath,0)

disp('User pressed cancel')

return

else

fullfp = fullfile(filepath, filename)

end

image = imread(fullfp) %代表要处理的图像

mysize = size(image)

%把图像转换成灰度图

if numel(mysize) >2

image = rgb2gray(image)

end

r_e = mysize(1)

c_e = mysize(2)

subplot(2,3,1)imshow(image,[])title('Input Image')

temp1 = imresize(image,2)%表示把图像放大到销雀陆原来的两倍,但同时图像矩阵也是变成了原来的两倍

[r_t1,c_t1] = size(temp1)

s = temp1(round(r_t1/2)-floor(r_e/2) : round(r_t1/2)+ceil(r_e/2)-1, round(c_t1/岁芹2)-floor(c_e/2) : round(c_t1/2)+ceil(c_e/2)-1)

subplot(2,3,2)imshow(s,[])title('Magnification')

temp2 = imresize(image,0.5)%表示把图像缩小到原来的一半,但同时图像矩阵也变成了原来的一半

[r_t2,c_t2] = size(temp2)

temp3 = zeros(r_e,c_e)

temp3(round(r_e/2)-floor(r_t2/2) : round(r_e/2)+ceil(r_t2/2)-1, round(c_e/2)-floor(c_t2/2) : round(c_e/2)+ceil(c_t2/2)-1) = temp2

ss = temp3

subplot(2,3,3)imshow(ss,[])title('Minification')

登录后复制

ps:直接调用imresize函数而不进行处理的效果如下图所示:

看起来图像没怎么变化,但存储图像的矩阵已经变大或变小了,如下图所示:

image为原亏顷始图像矩阵,temp1为放大后的图像矩阵,temp2为缩小后的图像矩阵

1、首先我们打开matlab软件,双击打开电脑上的matlab图标,进入matlab主界面,如图所示:

2、接着是启动simulink工具,这里主要是通过命令simulink演示,如图所示:

3、在打开的simulink工具中,我们进入simulink库浏览,如图所示:

4、我们在姿亏sinks找到scope示波器模块,并拖动到模型中,如图所示:

5、选中模块,当模块四角出迹基神现4个点时,单击一个点鼠标左键拖动就可以实现大小的调整,如图所示:

6、然后就完成了。锋扒

zoom 指令可以将图形放大或缩小,若要将图形放大时用 zoom on,zoom out,当不再须要放大或缩小图形时用 zoom off。

>>M=peaks(25)% peaks 是MATLAB内建的一个像山峰的特别函数,25是颤槐这个

>>plot(M) % 函数矩阵的大小,如果数毕洞铅值愈大则画出的山峰图愈平滑

>>zoom on % 开始放大图形,每按一次Enter键图形就放大一次

>>zoom out % 开始缩小图形,每按一手好次Enter键图形就缩小一次

>>zoom off % 停止图形放大或缩小功能

可以使用函数来实现此功能

图形移动,放大缩小等功能的函数 :

function axdrag(action)

%AXDRAG Pan and zoom with simple keystrokes

% Use this tool to move quickly around the data displayed in a 2-D plot.

% Make sure the figure has focus, and then press any of the following

% keys to zoom in or out. Clicking and dragging will pan the data.

%

% Keys you can use are:

% z, Z: zoom in, zoom out, in both dimensions

% x, X: zoom in, zoom out, x dimension only

% y, Y: zoom in, zoom out, y dimension only

% arrow keys: pan the data

% a: axis auto

% n: axis normal

% e: axis equal

% g: toggle grid state

% spacebar: toggle axis tick display state

% h: help

%

% Example

% c = pi*(1+sqrt(5))/2

% x = 0:1000

% r = 2.72378

% z = cumsum(exp(i*(c*x.*x + r)))

% plot(real(z),imag(z))

% axdrag

% % Now click, drag, and use special keys ...

% Ned Gulley, March 2003

persistent x0 dx

if nargin <1,

action = 'initialize'

end

% Use these variables to change the zoom and pan amounts

zoomFactor = 0.9

panFactor = 0.02

% Get rid of the help window if it's being displayed

helpTextAxis = findobj(gcbf,'Type','axes','Tag','axdraghelpaxis')

if isempty(helpTextAxis)

helpWasOff = 1

else

helpWasOff = 0

delete(helpTextAxis)

end

switch action

case 'initialize'

set(gca,'ButtonDownFcn','axdrag start')

set(gcf,'KeyPressFcn','axdrag keypress')

set(gcf,'DoubleBuffer','on')

case 'start'

set(gcbf,'Units','pixel')

set(gca,'Units','pixel')

set(gcbf,'WindowButtonMotionFcn','axdrag move')

set(gcbf,'WindowButtonUpFcn','axdrag stop')

currentPoint = get(gcbf,'CurrentPoint')

x0 = currentPoint

axdrag move

case 'move'

currentPoint = get(gcbf,'CurrentPoint')

dx = currentPoint - x0

x0 = currentPoint

ap = get(gca,'Position')

xLim = get(gca,'XLim')

yLim = get(gca,'YLim')

set(gca,'XLim',xLim-(diff(xLim)*dx(1)/ap(3)), ...

'YLim',yLim-(diff(yLim)*dx(2)/ap(4)))

case 'stop'

set(gcbf,'WindowButtonMotionFcn','')

set(gcbf,'WindowButtonUpFcn','')

set(gcbf,'Units','normalized')

set(gca,'Units','normalized')

case 'keypress'

currChar = get(gcbf,'CurrentCharacter')

if isempty(currChar)

return

end

if currChar=='a',

axis auto

elseif currChar=='e',

axis equal

elseif currChar=='n',

axis normal

elseif currChar=='g',

grid

elseif currChar==28,

xLim=get(gca,'XLim')

xLimNew = xLim + panFactor*diff(xLim)

set(gca,'XLim',xLimNew)

elseif currChar==29,

xLim=get(gca,'XLim')

xLimNew = xLim - panFactor*diff(xLim)

set(gca,'XLim',xLimNew)

elseif currChar==30,

yLim=get(gca,'YLim')

yLimNew = yLim - panFactor*diff(yLim)

set(gca,'YLim',yLimNew)

elseif currChar==31,

yLim=get(gca,'YLim')

yLimNew = yLim + panFactor*diff(yLim)

set(gca,'YLim',yLimNew)

elseif abs(currChar)==32,

if isempty(get(gca,'XTick')),

set(gca,'XTickMode','auto','YTickMode','auto')

else

set(gca,'XTick',[],'YTick',[],'Box','on')

end

elseif (currChar=='x') | (currChar=='X'),

if currChar == 'X',

zoomFactor=1/zoomFactor

end

xLim=get(gca,'XLim')

xLimNew = [0 zoomFactor*diff(xLim)] + xLim(1) + (1-zoomFactor)*diff(xLim)/2

set(gca,'XLim',xLimNew)

elseif (currChar=='y') | (currChar=='Y'),

if currChar == 'Y',

zoomFactor=1/zoomFactor

end

yLim=get(gca,'YLim')

yLimNew = [0 zoomFactor*diff(yLim)] + yLim(1) + (1-zoomFactor)*diff(yLim)/2

set(gca,'YLim',yLimNew)

elseif (currChar=='z') | (currChar=='Z'),

if currChar == 'Z',

zoomFactor=1/zoomFactor

end

xLim=get(gca,'XLim')

yLim=get(gca,'YLim')

xLimNew = [0 zoomFactor*diff(xLim)] + xLim(1) + (1-zoomFactor)*diff(xLim)/2

yLimNew = [0 zoomFactor*diff(yLim)] + yLim(1) + (1-zoomFactor)*diff(yLim)/2

set(gca,'XLim',xLimNew,'YLim',yLimNew)

elseif currChar=='h',

if helpWasOff

str = { ...

' '

' AXDRAG. Keys you can use are:'

' '

' z, Z: zoom in, zoom out, both dimensions '

' x, X: zoom in, zoom out, x dimension only '

' y, Y: zoom in, zoom out, y dimension only '

' arrow keys: pan the data'

' a: axis auto'

' n: axis normal'

' e: axis equal'

' g: toggle grid state'

' spacebar: toggle axis tick display state'

' h: help'

' '

' Press ''h'' again to dismiss this message'

' ' ...

}

helpTextAxis = axes( ...

'Tag','axdraghelpaxis', ...

'Units','characters', ...

'Position',[2 1 76 16], ...

'Visible','off')

text(0,1,str, ...

'Parent',helpTextAxis, ...

'VerticalAlignment','top', ...

'BackgroundColor',[1 1 0.8], ...

'FontName','courier', ...

'FontSize',6)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12454121.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存