利用matlab 对数字图片进行放大缩小是matlab在数字图像处理上的一个简单的应用

matlab库函数imresize()的功能就是这个,那么imresize具体怎么实现的呢,我们可以自己写一个myimresize()
imresize()的用法请查看matlab的HELP,搜索“imresize”

首先我们必须知道彩色数字图像其实是一个m*n*3的数字矩阵组成的,其中的m*n表示图片在宽度和高度上的像素大小,我们通常说的320*240的普通MP3的图片格式就是由宽度上320个像素点和高度上240个像素点组成。而之所以乘3,是因为彩色采用的rgb(red,green,blue)的方式。对于图片上的每个点的颜色,都由3个数字(r,g,b)来决定,如(255,0,0)为红色,(0,255,0)为绿色。每个数字介于0~255之间(8位表示法)。
对图片的放大和缩小,也就是说根据原来的图片矩阵来产生新矩阵。对于新矩阵的每个像素点,其取值有两种方式:
一种是取对应原来位置最近的那个点的像素,这种做法失真比较高,我们称这种做法为“nearest”;
另一种做法是取原来位置周围四个点的加权平均值,这种做法得到的图片比较柔和,我们称这种做法为双线性,“bilinear”,根据数学推导,很容易得知:

f(x0,y0) = (1-b)*[a*f(x+1,y)+(1-a)*f(x,y)] + b*[a*f(x+1,y+1)+(1-a)*f(x,y+1)]
源代码:
function resized = myimresize(image,scale,method);
% A funciton to resize a image
% 'resized' is the result of the function, which means change the size of
% 'image' of 'scale' times with method 'method'
%'image' is the source image
% 'scale' if 'scale'>1 it means to amplify the image with 'scale' times
%       if 'scale' <1 it means to shrink the image with 'scale' times
% 'method' if method = 'nearest' it will find the nearest point of 'image' to
% write into resized
%    else it means the 'bilinear' way
%    All right reserved by Pengxc
if strcmp(method, 'nearest')==1;
    % the first method
    [length,height,layer] = size(image);  % get the basic size of the image
    new_lenth  = length * scale;          % New lenth 
    new_height = height * scale;         % New height 
    new_lenth = floor(new_lenth);        % make it to int
    new_height = floor(new_height);      % make it to int
    
    %The code below is to find the nearest piont 
    for i = 1:new_lenth;
        for j = 1:new_height;
            
            remain_i = (i/scale) - floor(i/scale);
            % To see which side is nearer at x-label
            if remain_i >= 0.5
                o_i = ceil(i/scale);
            else
                o_i = floor(i/scale);
                % When scale>1 and i =1 ,then o_i = 0,which is wrong,so
                % make it equals 1
                if o_i == 0;
                    o_i =1;
                end
            end
            
            remain_j = (j/scale) - floor(i/scale);
            % To see which side is nearer at y-label
            if remain_j >= 0.5
                o_j = ceil(j/scale);
            else
                o_j = floor(j/scale);
                if o_j == 0;
                % When scale>1 and i =1 ,then o_i = 0,which is wrong,so
                % make it equals 1
                    o_j =1;
                end
            end
            
            for k =1:layer
            resized(i,j,k) = image(o_i,o_j,k);
            end
        end
    end
end           
if strcmp(method, 'bilinear')==1;   %    else it means the 'bilinear' way
    [lenth,height,layer] = size(image);  % the same as  above
    new_lenth = lenth * scale;
    new_height = height * scale;
    
    new_lenth = floor(new_lenth);
    new_height = floor(new_height);
    
    for i= 1 : lenth
        for j = 1 :height
            for k = 1: layer
                temp_image(i,j,k) = image(i,j,k);
                %temp_image has a row ans a column more than image
            end
        end
    end
    
    for i=1:lenth
        for k =1:layer
            temp_image(i,height+1,k) = 0;
            % add a column to keep from getting out of matrix 
        end
    end
    
    for j =1:height
        for k=1:layer
            temp_image(lenth+1,j,k)=0;
            % add a row to keep from getting out of matrix 
        end
    end
    
    % The code below use Bilinear to Calulate the value of the resized
    for i=1:new_lenth
        for j =1:new_height
            a = 0;
            b = 0;
            o_i = floor(i/scale);
            o_j = floor(j/scale);
            
            a = (i/scale) - floor(i/scale);
            b = (j/scale) - floor(j/scale);
            %a,b is the parameter, which will be detailly written in the Document
            if o_i == 0;
                o_i = 1;a=0;
            end
            if o_j == 0;
                o_j =1;b=0;
            end
            
            for k =1:layer
                resized(i,j,k) = (1-a)*(1-b)*temp_image(o_i,o_j,k) +(1-a)*b*temp_image(o_i,o_j+1,k) + a*(1-b)*temp_image(o_i+1,o_j,k) +a*b*temp_image(o_i+1,o_j+1,k);
            end
        end
    end    
end
测试与结果:
命令行敲入
I = imread(‘football.jpg’);
MSH=myimresize(I,0.7,’nearest’);
SH = imredize(I,0.7,’nearest’);
imshow(MSH);
figure,imshow(SH);
即可得到结果
左图是本段代码对“nearest”方式缩小0.7倍的测试,右边是库函数

matlab对图片进行放大和缩小相关推荐

  1. Matlab 保持图像矩阵大小不变对图像放大和缩小

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

  2. Android自定义ImageView(二)——实现双击放大与缩小图片

    效果图: 首先设置图片依据控件的大小来显示在ImageVeiw中 也就是当图片的宽与高小于控件的宽与高的时候,默认不进行对图片进行放大的操作,但是会将图片居中显示,当然使用的时候可以使用自定义的属性i ...

  3. (四)双击放大与缩小图片

    自定义ZoomImageView实现到这里,基本上完成一大半了.在上一篇又给它添加了自由移动的功能.如果你没读过,可以点击下面的链接: http://www.cnblogs.com/fuly55087 ...

  4. canvas图像绘制(图像放大、缩小、拖动和截图)

    林大大又来更新啦~ 这次主要做的是关于canvas图像绘制的部分,要实现的功能主要是绘制.对canvas的放大,缩小,拖动以及截图 本篇重点:图像的截图所使用的矩形框是怎么画出来的呢?想使用的童鞋参考 ...

  5. 计算机画图工具怎么缩小图片,Win10如何放大或缩小图片?利用win10画图工具放大、缩小图片教程...

    在日常使用电脑过程中,我们经常会碰到需要放大或缩小照片(图片)的情况.那么,win10系统下该如何扩大或者缩小照片(图片)呢?其实,我们可以通过使用win10系统自带的画图工具来实现.下面小编就向大家 ...

  6. android 手势放缩_AIR Android:放大与缩小手势

    放大与缩小手势(1) 放大与缩小手势对应TransformGestureEvent. GESTURE_ZOOM事件类型,使用时要求两个手指触摸屏幕,同时向外或向内做放缩动作,如图3-2所示. 图3-2 ...

  7. Android实现网页的放大与缩小

    以前实现打开某一网的功能都是调用系统内的浏览器,这个实例实现的是利用Webview控件实现网页的打开,放大与缩小的功能. 实现的截图如下: 正常模式的大小: 放大网页: 缩小网页: 实现这个例子 的代 ...

  8. 图像的放大与缩小(2)——双线性插值放大与均值缩小

    2019独角兽企业重金招聘Python工程师标准>>> 概述 基于上一节"等距采样法"实现图片放大与缩小的缺点.要对其进行改进,对图像的缩小则可以用"局 ...

  9. IOS代码添加控件,控件移动,放大,缩小,旋转

    控件移动,放大,缩小,旋转 1,代码添加控件 例如: [objc] view plaincopy /* 1.创建一个控件 2.设置控件的位置,大小 3.设置控件所需要的各个属性 4.添加入父控件 5. ...

最新文章

  1. 信号在PCB传播速度SDRAM布线(sdram布线距离主控的距离)
  2. Linux-NFS——配置过程
  3. XGBoost:Python下 安装
  4. 常用的MySQL图形化管理软件
  5. 德国商业经济金融发展史
  6. linux多路径配置
  7. libgmailer更新了,俺的下载空间又可以使用了(使用G-Share)
  8. 3.数据库操作相关术语,Oracle认证,insert into,批量插入,update tablename set,delete和truncate的差别,sql文件导入...
  9. Python Django 之 jQuery
  10. 12864c语言程序,LCM12864 C语言驱动程序
  11. 频域采样与恢复matlab实验,实验二 时域采样与频域采样及MATLAB程序
  12. golang连接FTP服务器并下载
  13. TiddlyWiki笔记入门之多维度标签--发散思维与收敛思维的乐园。
  14. 2021-09-30 js手动轮播图
  15. STM32 编码器驱动/旋转编码器旋钮encoder
  16. 汽车配件销售管理系统毕业设计
  17. s.length什么意思
  18. GDB调试错误显示没有文件,退出代码127
  19. 基于ICN的数据缓存
  20. 互联网广告请求链路_生产环境的全链路压测应该怎么做?答案都在这里了

热门文章

  1. 【测绘】高速公路中线放样
  2. 每打开一个文件夹就会出现一个新窗口
  3. java设置word审阅最终状态_如何设置word2013为最终状态
  4. JavaScript——鼠标移入移出事件
  5. yum如何安装特定版本的gcc_yum安装gcc
  6. C,C++面试题之2
  7. Word怎么输入美元符号
  8. 如何利用南方CASS制作地形图
  9. sql 语句查询 mysql 版本号
  10. PostCSS plugin postcss-uniapp-plugin requires PostCSS 8