• 因实验室项目需要,学习matlab图像处理,经常需要用到重复的函数,以至于在不能清除记得的情况下那一查找,所以特此记录,以备后用,不定期更新。 img1 = imread(‘’) %图片读入函数,括号内是图片地址 img2 = ...

    因实验室项目需要,学习matlab图像处理,经常需要用到重复的函数,以至于在不能清除记得的情况下那一查找,所以特此记录,以备后用,不定期更新。

    img1 = imread(‘’)       %图片读入函数,括号内是图片地址
    img2 = rgb2gray(img1) %图片转为灰度图像
    [imgheight,imgwidth] = size(img2) %获取图像的长宽(若是rgb图像,三个参数,最后一个是通道数量)
    img2 = imresize(img2,[200,400]) %改变图像大小


    A = imread('1.jpg','jpg');
    imshow(A) %显示图片
    imwrite(A,'1.bmp','bmp') %以bmp格式保存图片




     %计算图像两点的通道值得绝对值(因为img(i,j)不是负值,所以加double)
    d = abs( double(img(i,j)) - double(img(i,j+a)) ) 

    img_R = img1(:,:,1); %分割RGB通道
    img_G = img1(:,:,2);
    img_B = img1(:,:,3);

    基本运算:

    简单矩阵输入:A = [1,2,3;4,5,6;7,8,9]

    零矩阵:zeros()    全1矩阵:ones()    单位矩阵:eye()      随机矩阵:rand()




    基本曲线绘制:










    展开全文
  • 这些知识感觉学完还是记不住,这里自己总结了哈,希望能够加强记忆 图象显示 标准图像显示技术特殊图象显示技术 图像运算 代数运算空间变换图像配准 图像数据变换 二维傅立叶 离散余弦 其他变换  1)...

    matlab一些指令,便于后期查询

    这些知识感觉学完还是记不住,这里自己总结了哈,希望能够加强记忆

    • 图象显示

    1. 标准图像显示技术
    2. 特殊图象显示技术

    • 图像运算  

                  代数运算

                 a.图像相减取绝对值差:K=imabsdiff(I,J)  

                 b.图像求补:h=imcomplement(I)

                  空间变换

                 a.缩放图像

                                imresize

                 b.旋转图像

                                imrotate

                 c.修建图像

                                mcrop

                 d.空间变换

                              imtransform
                  图像配准


    • 图像数据变换

          二维傅立叶

    iff2

    离散余弦

    a.   dct2函数

    b.    dctmtx函数

    其他变换 

    1)radon变换:

    2)fanbeam变换:

                     

    • 图像分析与增强

            A像素值及统计

                 1)impixel :计算像素数据值
                 2)improfile:计算和绘制图像中一条或多条线段上的所有象素的灰度值(沿图像路径数据值)
                 3)   contour(I,N):(中文意思为轮廓):这个函数显示灰度图的等值线轮廓,能够自动设置坐标对象与显示图像相匹配,这里N指定了等高线的条数
         4)  imhist:    (图像柱状图【直方图】),显示索引图或灰度图的灰度分布,索引的图像只支持一个图层并且只有一个索引彩色通道。
         5)  其他统计方式

    图像分析

    MATLAB图像分析技术可以提取图像的结构信息,利用edge探测边界

    首先回顾一下边缘检测的一般步骤:

    边缘检测算法一般包含如下四个步骤:

      1.滤波(去噪)

      2.增强(一般是通过计算梯度幅值)

      3.检测(在图像中有许多点的梯度幅值会比较大,而这些点并不都是边缘,所以应该用某种方法来确定边缘点,比如最简单的边缘检测判据:梯度幅值阈值)

      4.定位(有的应用场合要求确定边缘位置,可以在子像素水平上来估计,指出边缘的位置和方向)

     边缘检测方法比较常用的有基于各种算子的方法,有基于一阶导数的各种算子(Roberts、Sobel、Prewitt等),还有基于二阶导数的拉普拉斯算子等。其中一阶导数一般找梯度极大值。二阶导数找过零点(需要忽略无意义的过零点(即均匀零区))。

    1) 灰度图像的边缘检测:edge函数
    BW=edge(I,method,thresh,direction/sigma)
    其中method可以取‘sobel’【注:在边缘检测中,常用的一种模板是Sobel 算子。Sobel 算子有两个,一个是检测水平边缘的 ;另一个是检测垂直边缘的 
     'prewitt'【注:Prewitt算子是在图像空间利用两个方向模板与图像进行邻域卷积完成的,这两个方向模板一个检测水平边缘,一个检测垂直边缘。
    'roberts'【注:Roberts边缘算子是一个2x2的模板,采用的是对角方向相邻的两个像素之差。从图像处理的实际效果来看,边缘定位较准,对噪声敏感。
    'log'【注:用高斯滤波器的拉普拉斯运算对I进行滤波,然后通过寻找0相交检测边缘】
    ‘zerocross’【注:用指定的滤波器对I滤波厚,寻找0相交边缘检测】
    'canny'【 用高斯滤波器计算得到的局部最大梯度来检测边缘】
    'thresh'【注:用于指定灵敏阈值】
    ‘sigma’【指定标准差】
    小结:在上面的方法中,canny边缘检测比较清晰,log与zerocross除了有canny检测的轮廓,还有其他的杂边,而剩下边缘检测方法边检测不太全阿。

    2)四叉树分解:qtdecomp函数(这部分转自uleen博客       

     
         一个简单的示例(来自MATLAB help for qtdecomp):
    原始矩阵:
    I =
        1    1    1    1    2    3    6    6
        1    1    2    1    4    5    6    8
        1    1    1    1    7    7    7    7
        1    1    1    1    6    6    5    5
       20   22   20   22    1    2    3    4
       20   22   22   20    5    4    7    8
       20   22   20   20    9   12   40   12
       20   22   20   20   13   14   15   16
    >> S = qtdecomp(I,.05) %执行四叉分解,阈值为0.05,实际上的阈值是ceil(0.05*255)=13,因为I是uint8类的
    S =
       (1,1)        4
       (5,1)        4
       (1,5)        4
       (5,5)        2
       (7,5)        2
       (5,7)        2
       (7,7)        1
       (8,7)        1
       (7,8)        1
       (8,8)        1

    S是一个稀疏阵,全显示为:
    >> disp(full(S));
         4     0     0     0     4     0     0     0
         0     0     0     0     0     0     0     0
         0     0     0     0     0     0     0     0
         0     0     0     0     0     0     0     0
         4     0     0     0     2     0     2     0
         0     0     0     0     0     0     0     0
         0     0     0     0     2     0     1     1
         0     0     0     0     0     0     1     1

    过程:

    图像的四叉分解

    图像的四叉分解


    图像的四叉分解

    含义:
    图像的四叉分解

        3)获得四叉树分解块值:qtdgetblk函数

           4)设置四叉树分解块值:qtsetblk函数

              C 图像调整

                图像调整技术用于图像的改善,包括提高图像信噪比、通过修正图像的颜色和灰度使某些特征更容易识别。

            1)图像灰度调整(imadjust)

           通过调节灰度范围数据,来实现增大或缩小图像对比度效果


                2)使用直方图调整灰度(histeq)

    (首先进行直方图均衡,再通过直方图均衡转换灰度图像的亮度值或索引值图像的颜色图值来增强图像对比度,输出图像的直方图近似于给定直方图)
                 3)图像色彩加强(decorrstretch)

                  

         D 图像平滑

              目的:减少噪声
            3种方法:

    1)线性滤波

    线性滤波可以从图像中去除特定成分噪音,用imfilter函数实现线性滤波

          B=imfilter(A,H)         其中H为滤波器类型
    fspecial:用于建立预定义的滤波算子,其语法格式为:H = fspecial(type)
                                                                                                     H = fspecial(type,para)其中type指定算子的类型,para指定相应的参数;type可以为以下几种:(这部分粘贴来自百度)
                           

    'average'

    averaging filter

    为均值滤波,参数为hsize代表模板尺寸,默认值为[3,3]。

    H = FSPECIAL('average',HSIZE) returns an averaging filter H of size

    HSIZE. HSIZE can be a vector specifying the number of rows and columns in

    H or a scalar, in which case H is a square matrix.

    The default HSIZE is [3 3].

    折叠'disk'

    circular averaging filter

    为圆形区域均值滤波,参数为radius代表区域半径,默认值为5.

    H = FSPECIAL('disk',RADIUS) returns a circular averaging filter

    (pillbox) within the square matrix of side 2*RADIUS+1.

    The default RADIUS is 5.

    折叠'gaussian'

    Gaussian lowpass filter

    为高斯低通滤波,有两个参数,hsize表示模板尺寸,默认值为[3 3],sigma为滤波器的标准值,单位为像素,默认值为0.5.

    H = FSPECIAL('gaussian',HSIZE,SIGMA) returns a rotationally

    symmetric Gaussian lowpass filter

    of size HSIZE with standard

    deviation SIGMA (positive). HSIZE can be a vector specifying the

    number of rows and columns in H or a scalar, in which case H is a

    square matrix.

    The default HSIZE is [3 3], the default SIGMA is 0.5.

    折叠'laplacian'

    filter approximating the 2-D Laplacian operator

    为拉普拉斯算子,参数alpha用于控制算子形状,取值范围为[0,1],默认值为0.2.

    H = FSPECIAL('laplacian',ALPHA) returns a 3-by-3 filter

    approximating the shape of the two-dimensional Laplacian

    operator. The parameter ALPHA controls the shape of the

    Laplacian and must be in the range 0.0 to 1.0.

    The default ALPHA is 0.2.

    折叠'log'

    Laplacian of Gaussian filter

    为拉普拉斯高斯算子,有两个参数,hsize表示模板尺寸,默认值为[3 3],sigma为滤波器的标准差,单位为像素,默认值为0.5.

    H = FSPECIAL('log',HSIZE,SIGMA) returns a rotationally symmetric

    Laplacian of Gaussian filter of size HSIZE with standard deviation

    SIGMA (positive). HSIZE can be a vector specifying the number of rows

    and columns in H or a scalar, in which case H is a square matrix.

    The default HSIZE is [5 5], the default SIGMA is 0.5.

    折叠'motion'

    motion filter

    为运动模糊算子,有两个参数,表示摄像物体逆时针方向以theta角度运动了len个像素,len的默认值为9,theta的默认值为0;

    H = FSPECIAL('motion',LEN,THETA) returns a filter to approximate, once

    convolved with an image, the linear motion of a camera by LEN pixels,

    with an angle of THETA degrees in a counter-clockwise direction. The

    filter becomes a vector for horizontal and vertical motions.

    The

    default LEN is 9, the default THETA is 0, which corresponds to a

    horizontal motion of 9 pixels.

    折叠'prewitt'

    Prewitt horizontal edge-emphasizing filter

    用于边缘增强,大小为[3 3],无参数

    H = FSPECIAL('prewitt') returns 3-by-3 filter that emphasizes

    horizontal edges by approximating a vertical gradient. If you need to

    emphasize vertical edges, transpose the filter H: H'.

    [1 1 1;0 0 0;-1 -1 -1].

    折叠'sobel'

    Sobel horizontal edge-emphasizing filter

    用于边缘提取,无参数

    H = FSPECIAL('sobel') returns 3-by-3 filter that emphasizes

    horizontal edges utilizing the smoothing effect by approximating a

    vertical gradient. If you need to emphasize vertical edges, transpose

    the filter H: H'.

    [1 2 1;0 0 0;-1 -2 -1].

    折叠'unsharp'

    unsharp contrast enhancement filter

    为对比度增强滤波器。参数alpha用于控制滤波器的形状,范围为[0,1],默认值为0.2.

    H = FSPECIAL('unsharp',ALPHA) returns a 3-by-3 unsharp contrast

    enhancement filter. FSPECIAL creates the unsharp filter from the

    negative of the Laplacian filter with parameter ALPHA. ALPHA controls

    the shape of the Laplacian and must be in the range 0.0 to 1.0.

    The default ALPHA is 0.2.


       2)中值滤波(medfilt2)

                特点:a.非线性处理方法;b.一定条件下克服了线性滤波模糊问题;c.适用滤除脉冲干扰以及图像扫描噪声;d.对于一些细节多,点、线、尖顶部多不宜用;

       3)自适应滤波(wiener2)

                          针对高斯白噪声的去除效果最好

    • 图像区域处理

                 1)区域设置
           通过二值掩模来实现。
                    a.形状旋转法:(与图像内容和属性无关)可使用MATLAB中的impoint,imline,imrect,imeclipse,imploy,imfreehand分别用于可拉伸点,可拉伸线,矩形区域,椭圆区域,多边形区域和自由选择区域。
    b.属性选择法(与图像属性有关)基于颜色使用roicolor函数
         2)区域滤波
                   roifilt2
                   a.读入灰度图;b.设置处理区域;c.滤波操作

                 3)区域填充
                         roifill

    • 形态学操作
          1)图像膨胀
               imdilate()


                 2)图像腐蚀
                 imerode()
                 3)形态学重建
    • 颜色处理
                    显示颜色:
                            get(0,‘ScreenDepth’)

                   减少颜色
                   转换颜色

    展开全文
  • 两个星期的图像处理研究学习差不多告一段落; 真的应了一句话,只有你接触某个领域,才真正可以体会到某个领域的魅力所在: 到目前为止

        两个星期的图像处理研究学习差不多告一段落;

        真的应了一句话,只有你接触某个领域,才真正可以体会到某个领域的魅力所在:

        到目前为止,学习的一些图像处理知识,都是最基础的,其实和机器学习、计算机视觉、人工智能等方面比起来,只是小菜一碟;感觉图像处理方面的知识博大精深,确实如此,这是一项研究生花两年的时间都不一定能掌握好的东西。只能说和嵌入式方向一样,都是水比较深。搞过这个之后,才深知自己的数学知识没学到位,后悔大学数学没有好好学,现在又有重新学数学的念头了,甚至有考个研究生的冲动了。。。

        是不是很蛋疼的事呢?我也不知道,就是觉得数学在挡我事了,我一定要把它搞定,其实,算法代码谁都会用,但关键你会写么?也许有人说我只要会用就OK了,的确,但如果你会写呢?。。。

        所以,说到底还是数学知识,以前大学里认为学的数学以后根本在实际工作中用不到,现在才发现错了;不是用不到,只是你没碰到!

        现在决定,明年,不,今年的一大重要目标,除了嵌入式、就是数学知识的提高。


        暂定的数学学习计划:

        复习《高等数学》、《线性代数》、《概率论》、《信号与系统》

        前面几个学的感觉自己水平提高到一定程度之后,最少要把那该死的FFT搞透彻;再重拾《数字信号处理》、《数字图像处理》等好书,研究一下,落实到具体的应用、代码上,不至于白学。

        具体什么时候执行,等过完年吧,与其他计划一起执行。


        对于数字图像处理,要学的还很多,可能这段时间会成为工作的一个段落而画上句号,但自己要不断的补充完善:

    可以补充的方面有以下这些:

        图像基础;灰度变换与空间滤波;频率滤波;小波变换

        图像增强;    图像复原;    图像分割; 图像压缩;  图像形态学操作等等;

        目标识别/跟踪、人工智能等;


    以下是一些比较好的网页链接,有空可以多看看:

    http://blog.csdn.net/dcraw/article/details/7617891

    http://blog.csdn.net/dcraw/article/details/7367990


        

        

    展开全文
  • Matlab图像处理小结

    2015-03-19 17:00:28
    经常做做图像处理的东西,时间长了,有些函数就忘了,看到网上有人总结,收藏了。 1. 图像和图像数据  缺省情况下,MATLAB将图像中的数据存储为双精度类型(double),64位浮点数,所需存储量很大;MATLAB还...

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

    经常做做图像处理的东西,时间长了,有些函数就忘了,看到网上有人总结,收藏了。

    1. 图像和图像数据

        缺省情况下,MATLAB将图像中的数据存储为双精度类型(double),64位浮点数,所需存储量很大;MATLAB还支持另一种类型无符号整型(uint8),即图像矩阵中每个数据占用1个字节。

        在使用MATLAB工具箱时,一定要注意函数所要求的参数类型。另外,uint8与double两种类型数据的值域不同,编程需注意值域转换。

    从uint8到double的转换

    ---------------------------------------------

    图像类型 MATLAB语句

    ---------------------------------------------

    索引色 B=double(A)+1

    索引色或真彩色 B=double(A)/255

    二值图像 B=double(A)

    ---------------------------------------------

    从double到uint8的转换

    ---------------------------------------------

    图像类型 MATLAB语句

    ---------------------------------------------

    索引色 B=uint8(round(A-1))

    索引色或真彩色 B=uint8(round(A*255))

    二值图像 B=logical(uint8(round(A)))

    ---------------------------------------------

    2. 图像处理工具箱所支持的图像类型

    2.1 真彩色图像

        R、G、B三个分量表示一个像素的颜色。如果要读取图像中(100,50)处的像素值,可查看三元数据(100,50,1:3)。

        真彩色图像可用双精度存储,亮度值范围是[0,1];比较符合习惯的存储方法是用无符号整型存储,亮度值范围[0,255]

    2.2 索引色图像

        包含两个结构,一个是调色板,另一个是图像数据矩阵。调色板是一个有3列和若干行的色彩映象矩阵,矩阵每行代表一种颜色,3列分别代表红、绿、蓝色强度的双精度数。

    注意:MATLAB中调色板色彩强度[0,1],0代表最暗,1代表最亮。

    常用颜色的RGB值

    --------------------------------------------

    颜色 R G B 颜色 R G B

    --------------------------------------------

    黑 0 0 1 洋红 1 0 1

    白 1 1 1 青蓝 0 1 1

    红 1 0 0 天蓝 0.67 0 1

    绿 0 1 0 橘黄 1 0.5 0

    蓝 0 0 1 深红 0.5 0 0

    黄 1 1 0 灰 0.5 0.5 0.5

    --------------------------------------------

     

    产生标准调色板的函数

    -------------------------------------------------

    函数名 调色板

    -------------------------------------------------

    Hsv 色彩饱和度,以红色开始,并以红色结束

    Hot 黑色-红色-黄色-白色

    Cool 青蓝和洋红的色度

    Pink 粉红的色度

    Gray 线型灰度

    Bone 带蓝色的灰度

    Jet Hsv的一种变形,以蓝色开始,以蓝色结束

    Copper 线型铜色度

    Prim 三棱镜,交替为红、橘黄、黄、绿和天蓝

    Flag 交替为红、白、蓝和黑

    --------------------------------------------------

        缺省情况下,调用上述函数灰产生一个64×3的调色板,用户也可指定调色板大小。索引色图像数据也有double和uint8两种类型。当图像数据为double类型时,值1代表调色板中的第1行,值2代表第2行……如果图像数据为uint8类型,0代表调色板的第一行,,值1代表第2行……

    2.3 灰度图像

        存储灰度图像只需要一个数据矩阵。

        数据类型可以是double,[0,1];也可以是uint8,[0,255]

    2.4 二值图像

        二值图像只需一个数据矩阵,每个像素只有两个灰度值,可以采用uint8或double类型存储。

        MATLAB工具箱中以二值图像作为返回结果的函数都使用uint8类型。

    2.5 图像序列

        MATLAB工具箱支持将多帧图像连接成图像序列。图像序列是一个4维数组,图像帧的序号在图像的长、宽、颜色深度之后构成第4维。

        分散的图像也可以合并成图像序列,前提是各图像尺寸必须相同,若是索引色图像,调色板也必须相同。

        可参考cat()函数 A=cat(4,A1,A2,A3,A4,A5)

     

    3. MATLAB图像类型转换

    图像类型转换函数

    ---------------------------------------------------------------------------

    函数名 函数功能

    ---------------------------------------------------------------------------

    dither 图像抖动,将灰度图变成二值图,或将真彩色图像抖动成索引色图像

    gray2ind 将灰度图像转换成索引图像

    grayslice 通过设定阈值将灰度图像转换成索引色图像

    im2bw 通过设定亮度阈值将真彩色、索引色、灰度图转换成二值图

    ind2gray 将索引色图像转换成灰度图像

    ind2rgb 将索引色图像转换成真彩色图像

    mat2gray 将一个数据矩阵转换成一副灰度图

    rgb2gray 将一副真彩色图像转换成灰度图像

    rgb2ind 将真彩色图像转换成索引色图像

    ----------------------------------------------------------------------------

    4. 图像文件的读写和查询

    4.1 图形图像文件的读取

        利用函数imread()可完成图形图像文件的读取,语法:

    A=imread(filename,fmt)

    [X,map]=imread(filename,fmt)

    [...]=imread(filename)

    [...]=imread(filename,idx) (只对TIF格式的文件)

    [...]=imread(filename,ref) (只对HDF格式的文件)

        通常,读取的大多数图像均为8bit,当这些图像加载到内存中时,Matlab就将其存放在类uint8中。此为Matlab还支持16bit的PNG和TIF图像,当读取这类文件时,Matlab就将其存贮在uint16中。

        注意:对于索引图像,即使图像阵列的本身为类uint8或类uint16,imread函数仍将颜色映象表读取并存贮到一个双精度的浮点类型的阵列中。

    4.2 图形图像文件的写入

        使用imwrite函数,语法如下:

    imwrite(A,filename,fmt)

    imwrite(X,map,filename,fmt)

    imwrite(...,filename)

    imwrite(...,parameter,value)

    当利用imwrite函数保存图像时,Matlab缺省的方式是将其简化道uint8的数据格式。

    4.3 图形图像文件信息的查询imfinfo()函数

    5. 图像文件的显示

    5.1 索引图像及其显示

    方法一:

    image(X)

    colormap(map)

    方法二:

    imshow(X,map)

    5.2 灰度图像及其显示

        Matlab 7.0 中,要显示一副灰度图像,可以调用函数 imshow 或 imagesc (即imagescale,图像缩放函数)

    (1) imshow 函数显示灰度图像

        使用imshow(I)或使用明确指定的灰度级书目:

    imshow(I,32)

        由于Matlab自动对灰度图像进行标度以适合调色板的范围,因而可以使用自定义大小的调色板。其调用格式如下:

    imshow(I,[low,high]) 其中,low 和 high 分别为数据数组的最小值和最大值。

    (2) imagesc 函数显示灰度图像

        下面的代码是具有两个输入参数的 imagesc 函数显示一副灰度图像

    imagesc(1,[0,1]);

    colormap(gray);

        imagesc 函数中的第二个参数确定灰度范围。灰度范围中的第一个值(通常是0),对应于颜色映象表中的第一个值(颜色),第二个值(通常是1)则对应与颜色映象表中的最后一个值(颜色)。灰度范围中间的值则线型对应与颜色映象表中剩余的值(颜色)。

        在调用 imagesc 函数时,若只使用一个参数,可以用任意灰度范围显示图像。在该调用方式下,数据矩阵中的最小值对应于颜色映象表中的第一个颜色值,数据矩阵中的最大值对应于颜色映象表中的最后一个颜色值。

    5.3 RGB 图像及其显示

    (1) image(RGB)

        不管RGB图像的类型是double浮点型,还是 uint8 或 uint16 无符号整数型,Matlab都能通过 image 函数将其正确显示出来。

    RGB8 = uint8(round(RGB64×255)); % 将 double 浮点型转换为 uint8 无符号整型

    RGB64 = double(RGB8)/255; % 将 uint8 无符号整型转换为 double 浮点型

    RGB16 = uint16(round(RGB64×65535)); % 将 double 浮点型转换为 uint16 无符号整型

    RGB64 = double(RGB16)/65535; % 将 uint16 无符号整型转换为 double 浮点型

    (2) imshow(RGB) 参数是一个 m×n×3 的数组

    5.4 二进制图像及其显示

    (1) imshow(BW)

         在 Matlab 7.0 中,二进制图像是一个逻辑类,仅包括 0 和 1 两个数值。像素 0 显示为黑色,像素 1 显示为白色。

        显示时,也可通过NOT(~)命令,对二进制图象进行取反,使数值 0 显示为白色;1 显示为黑色。

    例如: imshow(~BW)

    (2) 此外,还可以使用一个调色板显示一副二进制图像。如果图形是 uint8 数据类型,则数值 0 显示为调色板的第一个颜色,数值 1 显示为第二个颜色。

    例如: imshow(BW,[1 0 0;0 0 1])

    5.5 直接从磁盘显示图像

        可使用一下命令直接进行图像文件的显示:

    imshow filename

    其中,filename 为要显示的图像文件的文件名。

        如果图像是多帧的,那么 imshow 将仅显示第一帧。但需注意,在使用这种方式时,图像数据没有保存在Matlab 7.0 工作平台。如果希望将将图像装入工作台中,需使用 getimage 函数,从当前的句柄图形图像对象中获取图像数据,命令形式为:

    rgb = getimage;

     

     

     

    1、图像的变换

    ① fft2:fft2函数用于数字图像的二维傅立叶变换,如:

    i=imread('104_8.tif');

    j=fft2(i);

    ②ifft2::ifft2函数用于数字图像的二维傅立叶反变换,如:

    i=imread('104_8.tif');

    j=fft2(i);

    k=ifft2(j);

    2、模拟噪声生成函数和预定义滤波器

    ① imnoise:用于对图像生成模拟噪声,如:

    i=imread('104_8.tif');

    j=imnoise(i,'gaussian',0,0.02);%模拟高斯噪声

    ② fspecial:用于产生预定义滤波器,如:

    h=fspecial('sobel');%sobel水平边缘增强滤波器h=fspecial('gaussian');%高斯低通滤波器

    h=fspecial('laplacian');%拉普拉斯滤波器h=fspecial('log');%高斯拉普拉斯(LoG)滤波器

    h=fspecial('average');%均值滤波器

    3、图像的增强

    直方图:imhist函数用于数字图像的直方图显示,如:

    i=imread('104_8.tif');

    imhist(i);

    直方图均化:histeq函数用于数字图像的直方图均化,如:

    i=imread('104_8.tif');

    j=histeq(i);

    对比度调整:imadjust函数用于数字图像的对比度调整,如:

    i=imread('104_8.tif');

    j=imadjust(i,[0.3,0.7],[]);

    对数变换:log函数用于数字图像的对数变换,如:

    i=imread('104_8.tif');

    j=double(i);

    k=log(j);

    基于卷积的图像滤波函数:filter2函数用于图像滤波,如:

    i=imread('104_8.tif');

    h=[1,2,1;0,0,0;-1,-2,-1];

    j=filter2(h,i);

    线性滤波:利用二维卷积conv2滤波, 如:

    i=imread('104_8.tif');

    h=[1,1,1;1,1,1;1,1,1];

    h=h/9;

    j=conv2(i,h);

    中值滤波:medfilt2函数用于图像的中值滤波,如:

    i=imread('104_8.tif');

    j=medfilt2(i);

    锐化

    (1)利用Sobel算子锐化图像, 如:

    i=imread('104_8.tif');

    h=[1,2,1;0,0,0;-1,-2,-1];%Sobel算子

    j=filter2(h,i);

    (2)利用拉氏算子锐化图像, 如:

    i=imread('104_8.tif');

    j=double(i);

    h=[0,1,0;1,-4,0;0,1,0];%拉氏算子

    k=conv2(j,h,'same');

    m=j-k;

    3、图像边缘检测

    ①sobel算子 如:i=imread('104_8.tif');j = edge(i,'sobel',thresh)

    ②prewitt算子 如:i=imread('104_8.tif');j = edge(i,'prewitt',thresh)

    ③roberts算子 如:i=imread('104_8.tif');j = edge(i,'roberts',thresh)

    ④log算子 如:i=imread('104_8.tif');j = edge(i,'log',thresh)

    ⑤canny算子 如:i=imread('104_8.tif');j = edge(i,'canny',thresh)

    ⑥Zero-Cross算子 如:i=imread('104_8.tif');j = edge(i,'zerocross',thresh)

    4、形态学图像处理

    ①膨胀:是在二值化图像中“加长”或“变粗”的操作,函数imdilate执行膨胀运算,如:

    a=imread('104_7.tif'); %输入二值图像

    b=[0 1 0;1 1 1;0 1 0];

    c=imdilate(a,b);

    ②腐蚀:函数imerode执行腐蚀,如:

    a=imread('104_7.tif'); %输入二值图像

    b=strel('disk',1);

    c=imerode(a,b);

    ③开运算:先腐蚀后膨胀称为开运算,用imopen来实现,如:

    a=imread('104_8.tif');

    b=strel('square',2);

    c=imopen(a,b);

    ④闭运算:先膨胀后腐蚀称为闭运算,用imclose来实现,如:

    a=imread('104_8.tif');

    b=strel('square',2);

    c=imclose(a,b);

    转自:http://www.matlabfan.com/thread-700-1-1.html

     

     

    下列表格中除了个别函数外,其余函数都是图像处理工具箱提供的关于图像处理的函数,现摘录到此以备查找。

    表1 图像显示
    函数名 功能说明 函数名 功能说明
    colorbar 颜色条显示 montage 按矩形剪辑方式显示多帧图像
    getimage 从坐标系中获取图像数据 immovie 从多帧索引图像中制作电影
    image 建立显示图像 movie 播放电影
    subimage 在同一图像窗口显示多个图像 trueszie 调整图像显示大小
    imagesc 调整数据并显示图像 warp 显示图像为纹理映射表面
    imshow 图像显示 zoom 二维图形放大或缩小
    表2 图像文件输入/输出
    函数名 功能说明 函数名 功能说明
    imread 图像文件读入 load 将以mat为扩展名的图像文件调入到内存
    imwrite 图像写出 save 将内存变量中图像保存到mat文件中
    dicomread 读取DICOM图像 dicomwrite 输出DICOM图像
    iminfo 查看图形信息    
    表3 图像像素值及其统计
    函数名 功能说明 函数名 功能说明
    impixel 返回选定图像像素颜色值 imcontour 画图像数据轮廓(等高线,等值线)
    improfile 图像中沿一个路径的数据值计算 imhist 求图像数据直方图
    mean2 求均值 corr2 求相关系数
    std2 求标准差    
    表4 图像分析
    函数名 功能说明 函数名 功能说明
    edge 灰度图像边缘检测 qtgetblk 获得四叉树分解块值
    qtecomp 执行四叉树分解 qtsetblk 设置四叉树分解块值
    表5 图像增强及平滑
    函数名 功能说明 函数名 功能说明
    imadjust 对比度调整 medfilt2 二维中值滤波器
    histeq 直方图均衡 ordfilt2 顺序统计滤波器
    imnoise 给图像增加噪声 wiener2 二维自适应除噪滤波器
    表6 图像线性滤波及二维线性滤波器设计
    函数名 功能说明 函数名 功能说明
    conv2 二维卷积 freqz2 计算二维频率响应
    convmtx2 计算二维卷积矩阵 fsamp2 用频率抽样设计二维FIR滤波器
    convn 多维卷积 ftrans 用频率抽样转换二维FIRlbq
    filter2 二维线性数字滤波器 fwind1 用一维窗口方法设计二维FIR滤波器
    fspecial 产生预定义滤波器 fwind2 用二维窗口方法设计二维FIR滤波器
    frespace 确定二维频率响应间隔    
    表7 图像变换
    函数名 功能说明 函数名 功能说明
    fft 计算一维快速Fourier变换 dct 计算离散余弦变换
    ifft 计算一维FFT的逆变换 idct 计算离散反余弦变换
    fft2 计算二维FFT dct2 计算2D离散余弦变换
    ifft2 计算二维逆FFT idct2 计算2D反离散余弦变换
    fftn 计算多维FFT dctmtx 计算TCT矩阵
    ifftn 计算多维逆FFT radon 计算Radon变换
    fftshift 直流分量移到频谱中心    
    表8 图像领域及操作
    函数名 功能说明 函数名 功能说明
    bestblk 选择块处理的块大小 colfilt 使用列方向函数进行领域运算
    blkproc 对图像实行不同的块处理 im2col 重排图像块为矩阵列
    col2im 重排矩阵列成图像块 nlfilter 进行一般领域计算
    表9 二值图像操作
    函数名 功能说明 函数名 功能说明
    applylut 使用查找表进行领域操作 bwperim 确定二值图像中的目标边界
    bwarea 计算二值图像中的目标区域 bwselect 选择二值图像中的目标
    bweuler 计算二值图像中的欧拉数 dilate 对二值图像进行膨胀计算
    bwfill 二值图像背景区域填充 erode 对二值图像进行寝蚀运算
    bwlabel 标识二值图像中的连接成分 makelut 构造查找表(applylut)使用
    bwmorph 二值图像形态运算    
    表10 基于区域的图像处理
    函数名 功能说明 函数名 功能说明
    roicolor 根据颜色选择要处理的区域 roifilt2 对要处理区域滤波
    roifill 在任意区域内平滑差值 roipoly 选择要处理的多边形区域
    表11 图像几何运算
    函数名 功能说明 函数名 功能说明
    imcrop 图像剪裁 imrotate 图像旋转
    imresize 图像大小调整 interp2 二维数据差值
    表12 图像颜色图操作
    函数名 功能说明 函数名 功能说明
    brighten 颜色图像变亮或变暗 colormap 设置获取图
    cmpermute 重新排列颜色图中的颜色 imapprox 由颜色较少的图像近似索引图像
    cmunique 寻找唯一的颜色图及相应的图像 rgbplot 绘制RGB颜色图
    表13 颜色控件转换
    函数名 功能说明 函数名 功能说明
    hsv2rgb 将HSV颜色转化为RGB颜色值 mtsc2rgb 将NTSC值转换为RGB颜色空间值
    rgb2hsv 将RGB颜色值转换为HSV颜色值 rgb2ntsc 将RGB值转换为NTSC颜色空间值
    展开全文
  • Matlab 图像处理总结

    2017-06-22 10:49:59
    学习了一段时间的数字图像处理,现在把我所做的分享出来和大家交流下,因为是用MATLAB GUI 做的,有些界面并未给出,如果你想直接复制运行可能出不来结果, 所以仅仅当作参考。 基本界面 % *******灰度化****...

    学习了一段时间的数字图像处理,现在把我所做的分享出来和大家交流下,因为是用MATLAB GUI 做的,有些界面并未给出,如果你想直接复制运行可能出不来结果,

    所以仅仅当作参考。

    基本界面


    % *******灰度化********************************************************
    
    % --------------------------------------------------------------------
    function gray_Callback(hObject, eventdata, handles)
    % hObject    handle to gray (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global im;
    global map;
    global gray;
    global im_change;
    im_change = 1;
    axes(handles.axes1);
    if isempty(im)
        warndlg('Please select a picture first!','Warning');
    else
        if isempty(map)
            gray24(:,:,1) = im(:,:,1) * 0.299;
            gray24(:,:,2) = im(:,:,2) * 0.587;
            gray24(:,:,3) = im(:,:,3) * 0.114;
            gray = gray24(:,:,1)+gray24(:,:,2)+gray24(:,:,3);
            imshow(gray);
        else
            gray = ind2gray(im,map);
            imshow(gray);  
        end;
        set(handles.pushbutton_reset,'visible','on');
    end;

    % ********直方图*********************************************************
    function grayHist_Callback(hObject, eventdata, handles)
    % hObject    handle to grayHist (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global gray;
    figure('Name','histogram');
    if isempty(gray)
        warndlg('You must have a gray-scale first!','Warning');
    else
            i = gray;
            subplot(2,2,1);
            imhist(i);
            title('灰度直方图');
            subplot(2,2,2);
            H = histeq(i);
            imhist(H);
            title('直方图均衡化');
            subplot(2,2,3);
            num1 = imhist(i);
            [r1,c1] = size(i);
            sum1 = r1*c1;
            y = num1/sum1;
            x = 0:255;
            bar(x,y);
            title('灰度直方图(%)');
            subplot(2,2,4);
            H = histeq(i);
            num2 = imhist(H);
            [r2,c2] = size(i);
            sum2 = r2*c2;
            Hy = num2/sum2;
            bar(x,Hy);
            title('直方图均衡化(%)');
            figure('Name','histogram equalization');
            imshow(H);
    end;
    
    
    % ********二值化********************************************************
    function Binary_Callback(hObject, eventdata, handles)
    % hObject    handle to Binary (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global gray;
    global map;
    global im_change;
    im_change = 1;
    i = gray;
    axes(handles.axes1);
    val = getappdata(handles.slider_binary,'Value');
    if isempty(val)
        val = 0.5;
    end;
    im2bw(i,val);
    set(handles.slider_binary,'Enable','on');
    set(handles.text_binary,'Enable','on');
    
    
    
    % **********二值化阈值*****************************************************
    % --- Executes on slider movement.
    function slider_binary_Callback(hObject, eventdata, handles)
    % hObject    handle to slider_binary (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'Value') returns position of slider
    %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
    
    val = get(hObject,'Value');
    setappdata(handles.slider_binary,'Value',val);
    set(handles.text_binary,'String',num2str(val));
    Binary_Callback(hObject, eventdata, handles);
    %handles=guidata(hObject);
    
    
    % --- Executes during object creation, after setting all properties.
    function slider_binary_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to slider_binary (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: slider controls usually have a light gray background.
    if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor',[.9 .9 .9]);
    end
    
    % --- Executes on button press in pushbutton_binary.
    function pushbutton_binary_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton_binary (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    val = 0.5;
    set(handles.slider_binary,'Value',val);
    setappdata(handles.slider_binary,'Value',val);
    set(handles.text_binary,'String',num2str(val));
    Binary_Callback(hObject, eventdata, handles);
    
    
    % *********图像缩放********************************************************
    % --- Executes on slider movement.
    function slider_zoom_Callback(hObject, eventdata, handles)
    % hObject    handle to slider_zoom (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'Value') returns position of slider
    %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
    val = get(hObject,'Value');
    setappdata(handles.slider_zoom,'Value',val);
    set(handles.text_zoom,'String',num2str(val));
    
    
    % --- Executes during object creation, after setting all properties.
    function slider_zoom_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to slider_zoom (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: slider controls usually have a light gray background.
    if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor',[.9 .9 .9]);
    end
    
    % --- Executes on button press in pushbutton_ok_zoom.
    function pushbutton_ok_zoom_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton_ok_zoom (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    val = getappdata(handles.slider_zoom,'Value');
    img = getimage(handles.axes1);
    im_re = imresize(img,val);
    figure;
    imshow(im_re);
    
    % --- Executes on button press in pushbutton_reset_zoom.
    function pushbutton_reset_zoom_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton_reset_zoom (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    set(handles.slider_zoom,'Value',1.0);
    setappdata(handles.slider_zoom,'Value',1.0);
    set(handles.text_zoom,'String',num2str(1));
    
    
    % *******图像翻转********************************************************
    % --- Executes on slider movement.
    function slider_flip_Callback(hObject, eventdata, handles)
    % hObject    handle to slider_flip (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'Value') returns position of slider
    %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
    val = get(hObject,'Value');
    setappdata(handles.slider_flip,'Value',val);
    set(handles.text_flip,'String',num2str(val));
    
    
    % --- Executes during object creation, after setting all properties.
    function slider_flip_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to slider_flip (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: slider controls usually have a light gray background.
    if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor',[.9 .9 .9]);
    end
    
    % --- Executes on button press in pushbutton_ok_flip.
    function pushbutton_ok_flip_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton_ok_flip (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global save_im;
    global im_change;
    axes(handles.axes1);
    val = getappdata(handles.slider_flip,'Value');
    img = getimage(handles.axes1);
    if isempty(save_im) || im_change==1
        save_im = img;
        im_change = 0;
    end;
    im_ro = imrotate(img,val);
    imshow(im_ro);
    
    % --- Executes on button press in pushbutton_reset_flip.
    function pushbutton_reset_flip_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton_reset_flip (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global save_im;
    set(handles.slider_flip,'Value',0);
    setappdata(handles.slider_flip,'Value',0);
    set(handles.text_flip,'String',num2str(0));
    axes(handles.axes1);
    imshow(save_im);
    
    
    % ******对比度调整********************************************************
    function adjust_Callback(hObject, eventdata, handles)
    % hObject    handle to adjust (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    %将图像I中的亮度值映射到II中的新值
    global gray;
    j = imadjust(gray,[0 1],[1 0]);
    figure;
    set(gcf,'Name','adjust');
    imshow(j,[]);
    
    
    % ******对数变换********************************************************
    function Logarithmic_Callback(hObject, eventdata, handles)
    % hObject    handle to Logarithmic (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    %img = getimage(handles.axes1);
    global gray;
    %l = logv+1(1+v*i);换底公式 l = log2(1 + v*i)/log2(v+1);
    l = log(1+double(gray));
    figure;
    set(gcf,'Name','logarithmic');
    imshow(l,[]);
    
    
    % *******反转********************************************************
    function reverse_Callback(hObject, eventdata, handles)
    % hObject    handle to reverse (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    s = 255-img;
    figure('Name','reverse');
    set(gcf,'Name','reverse');
    imshow(s);
    
    
    % ******幂次变换********************************************************
    function exponent_trans_Callback(hObject, eventdata, handles)
    % hObject    handle to exponent_trans (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global gray;
    handles_power = guihandles(ImageProcessing);
    dgray = double(gray);
    s = getappdata(handles.edit_power,'String');
    if isempty(s)
        warndlg('Please enter power side!','Warning');
    else
        e = str2double(s);
        j = dgray.^e;
        figure('Name','exponent');
        imshow(j,[]);
    end;
    
    
    % *******分段线性********************************************************
    function Piecewise_linear_Callback(hObject, eventdata, handles)
    % hObject    handle to Piecewise_linear (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global gray;
    
    
    x0=0;y0=0;  %对图像灰度进行分段的点
    x1 = str2double(get(handles.edit_x1,'String'));
    x2 = str2double(get(handles.edit_x2,'String'));
    y1 = str2double(get(handles.edit_y1,'String'));
    y2 = str2double(get(handles.edit_y2,'String'));
    x3=255;y3=255;
    if (x1<0||x1>255||x2<0||x2>255||y1<0||y1>255||y2<0||y2>255)
        warndlg('Make sure the value entered is between 0 and 255!','Warning');
    else
        figure
        plot([x0,x1,x2,x3],[y0,y1,y2,y3])
        axis tight,xlabel('x'),ylabel('y')
        title('intensitytransformation')%绘制变换曲线
        k1=round(y1/x1);
        b1=0;
        k2=round((y2-y1)/(x2-x1));
        b2=y1-k2*x1;
        k3=round((y3-y2)/(x3-x2));
        b3=y2-k3*x2;
        [m,n]=size(gray);
        X=double(gray);
        for i=1:m
            for j=1:n
                x=X(i,j);
                if ((x>=0)&&(x<x1))
                    y(i,j)=k1*x;
                elseif ((x>=x1)&&(x<=x2))
                    y(i,j)=k2*x+b2;
                elseif ((x>=x2)&&(x<=x3))
                    y(i,j)=k3*x+b3;
                end
            end
        end
        figure;
        set(gcf,'Name','piecewise_linear');
        y=mat2gray(y);
        imshow(y);
    end;
    close(uipanel_piecewise_linear);
    
    % *******滤波处理********************************************************
    function Filter_Callback(hObject, eventdata, handles)
    % hObject    handle to Filter (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    
    % *******锐化********************************************************
    function sharpening_Callback(hObject, eventdata, handles)
    % hObject    handle to sharpening (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    
    % --------roberts梯度算子(微分)-------------------------------------------
    function roberts_Callback(hObject, eventdata, handles)
    % hObject    handle to roberts (1see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    global gray;
    x=double(gray);
    [m,n] = size(gray);
    b=zeros(m,n,1);   
    c=zeros(m,n,1);   
    for i=1:m-2   
        for j=1:n-2   
            b(i+1,j+1)=x(i,j)-x(i+1,j+1);   
            c(i+1,j+1)=x(i,j+1)-x(i+1,j);   
            b(i+1,j+1)=sqrt(b(i+1,j+1)^2+c(i+1,j+1)^2);   
        end  
    end  
    figure('Name','roberts');
    imshow(uint8(b));
    
    
    
    % -------sobel算子---------------------------------------------------------
    function sobel_Callback(hObject, eventdata, handles)
    % hObject    handle to sobel (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global gray;
    [m,n] = size(gray);
    x=double(gray);   
    b=zeros(m,n);   
    c=zeros(m,n);   
    for i=1:m-2   
        for j=1:n-2   
            b(i+1,j+1)=x(i,j)+2*x(i+1,j)+x(i+2,j)-x(i,j+2)-2*x(i+1,j+2)-x(i+2,j+2);   
            c(i+1,j+1)=-x(i,j)-2*x(i,j+1)-x(i,j+2)+x(i+2,j)+2*x(i+2,j+1)+x(i+2,j+2);   
            b(i+1,j+1)=sqrt(b(i+1,j+1)^2+c(i+1,j+1)^2);   
        end   
    end   
    figure('Name','sobel');
    imshow(uint8(b));
    
    
    % --------prewitt算子-------------------------------------------------------
    function prewitt_Callback(hObject, eventdata, handles)
    % hObject    handle to prewitt (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global gray;
    [m,n] = size(gray);
    x=double(gray);   
    b=zeros(m,n);   
    c=zeros(m,n);   
    for i=1:m-2   
        for j=1:n-2   
            b(i+1,j+1)=-x(i,j)-x(i+1,j)-x(i+2,j)+x(i,j+2)+x(i+1,j+2)+x(i+2,j+2);   
            c(i+1,j+1)=x(i,j)+x(i,j+1)+x(i,j+2)-x(i+2,j)-x(i+2,j+1)-x(i+2,j+2);   
            b(i+1,j+1)=sqrt(b(i+1,j+1)^2+c(i+1,j+1)^2);   
        end   
    end   
    figure('Name','prewitt');
    imshow(uint8(b));
    
    % --------拉普拉斯算子-----------------------------------------------------
    function laplace_Callback(hObject, eventdata, handles)
    % hObject    handle to laplace (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    w = fspecial('laplacian',0);
    J = imfilter(img,w,'replicate');
    figure('Name','laplace');
    imshow(J);
    
    
    % *******平滑********************************************************
    function smoothing_Callback(hObject, eventdata, handles)
    % hObject    handle to smoothing (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    
    % -------中值滤波-------------------------------------------------------
    function median_filtering_Callback(hObject, eventdata, handles)
    % hObject    handle to median_filtering (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global im;
    global gray;
    global map;
    if isempty(map)
        J = imnoise(im,'salt & pepper',0.05);
    else 
        J = imnoise(gray,'salt & pepper',0.05);
    end;
    val = get(handles.edit_val,'String');
    n = str2num(val);
    % M = medfilt2(J,[n n]);
    [height, width]=size(J);   
    x1=double(J);  
    x2=x1;  
    for i=1:height-n+1  
        for j=1:width-n+1  
            c=x1(i:i+(n-1),j:j+(n-1));
            e=c(:);%转换为行向量
    %         e=c(1,:);      
    %         for u=2:n  
    %             e=[e,c(u,:)];          
    %         end  
            mm=median(e);       
            x2(i+(n-1)/2,j+(n-1)/2)=mm;   %将模板各元素的中值赋给模板中心位置的元素  
        end  
    end   
    M = uint8(x2);
    figure('Name','med_filter');
    subplot(1,2,1);
    imshow(J);
    title('before filter');
    subplot(1,2,2);
    imshow(M);
    title('after filter');
    
    
    % -------均值滤波-------------------------------------------------------
    function average_filter_Callback(hObject, eventdata, handles)
    % hObject    handle to average_filter (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global gray;
    J = imnoise(gray,'gaussian',0,0.01);
    a = fspecial('average'); 
    A = filter2(a,gray)/255;
    figure('Name','average filter');
    subplot(1,2,1);
    imshow(J);
    title('before filter');
    subplot(1,2,2);
    imshow(A);
    title('after filter');

    % **********理想滤波*****************************************************
    function ideal_Callback(hObject, eventdata, handles)
    % hObject    handle to ideal (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % --------------------------------------------------------------------
    function l_ideal_Callback(hObject, eventdata, handles)
    % hObject    handle to l_ideal (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            if(D <= 120)  
                H_1(x+(P/2)+1,y+(Q/2)+1) = 1; 
            end    
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);     
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function h_ideal_Callback(hObject, eventdata, handles)
    % hObject    handle to h_ideal (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            if(D >= 120)  
                H_1(x+(P/2)+1,y+(Q/2)+1) = 1; 
            end   
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
           
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function s_ideal_Callback(hObject, eventdata, handles)
    % hObject    handle to s_ideal (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            D_0 = 250;
            W = 50;
            if(D < D_0-W/2 || D > D_0+W/2)  
                H_1(x+(P/2)+1,y+(Q/2)+1) = 1; 
            end;
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
           
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function p_ideal_Callback(hObject, eventdata, handles)
    % hObject    handle to p_ideal (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            D_0 = 250;
            W = 50;
            if(D >= D_0-W/2 && D <= D_0+W/2)  
                H_1(x+(P/2)+1,y+(Q/2)+1) = 1; 
            end;
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
           
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function t_ideal_Callback(hObject, eventdata, handles)
    % hObject    handle to t_ideal (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    F = fft2(fc,P,Q);
    H = ones(P,Q);
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            u_k=50;
            v_k=70;
            D0 = 20;
            D1 = ((x+u_k)^2 + (y+v_k)^2)^(0.5);
            D2 = ((x-u_k)^2 + (y-v_k)^2)^(0.5);
            if(D1 <= D0 || D2 <= D0)  
                H(x+(P/2)+1,y+(Q/2)+1) = 0; 
            end; 
         end
    end
    
    G = H .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    
    
    % **********Butterworth滤波***********************************************
    function butterworth_Callback(hObject, eventdata, handles)
    % hObject    handle to butterworth (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % --------------------------------------------------------------------
    function l_butterworth_Callback(hObject, eventdata, handles)
    % hObject    handle to l_butterworth (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    for x = 1:1:M
        for y = 1:1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    n = 1;%一阶 
    for x = (-P/2):(P/2)-1
         for y = (-Q/2):(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            D_0 = 100;
            H_1(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D/D_0)^(2*n));  
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function h_butterworth_Callback(hObject, eventdata, handles)
    % hObject    handle to h_butterworth (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    n = 1;
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            D_0 = 100;
            H_1(x+(P/2)+1,y+(Q/2)+1) = 1/(1+(D_0/D)^(2*n));   
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
           
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function s_butterworth_Callback(hObject, eventdata, handles)
    % hObject    handle to s_butterworth (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    n = 1;
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            D_0 = 250;
            W = 30;
            H_1(x+(P/2)+1,y+(Q/2)+1) = 1/(1+((D*W)/((D*D) - (D_0*D_0)))^(2*n));     
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
           
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function p_butterworth_Callback(hObject, eventdata, handles)
    % hObject    handle to p_butterworth (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    n = 1;
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            D_0 = 250;
            W = 30;
            H_1(x+(P/2)+1,y+(Q/2)+1) = 1 - 1/(1+((D*W)/((D*D) - (D_0*D_0)))^(2*n));     
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
           
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function t_butterworth_Callback(hObject, eventdata, handles)
    % hObject    handle to t_butterworth (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    
    F = fft2(fc,P,Q);
    H = ones(P,Q);
    n=2;
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D = 30;
            u_k = 50;v_k = 70; 
            D_k = ((x+u_k)^2 + (y+v_k)^2)^(0.5);
            H(x+(P/2)+1,y+(Q/2)+1) = H(x+(P/2)+1,y+(Q/2)+1) * 1/(1+(D/D_k)^(2*n));
            D_k = ((x-u_k)^2 + (y-v_k)^2)^(0.5);
            H(x+(P/2)+1,y+(Q/2)+1) = H(x+(P/2)+1,y+(Q/2)+1) * 1/(1+(D/D_k)^(2*n));
         end
    end
    
    G = H .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    
    
    % **********高斯滤波***********************************************
    function gaussian_Callback(hObject, eventdata, handles)
    % hObject    handle to gaussian (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % --------------------------------------------------------------------
    function l_gaussian_Callback(hObject, eventdata, handles)
    % hObject    handle to l_gaussian (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:1:M
        for y = 1:1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    H_2 = zeros(P,Q);
    
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            D_0 = 60;
            H_1(x+(P/2)+1,y+(Q/2)+1) = exp(-(D*D)/(2*D_0*D_0));   
            D_0 = 160;
            H_2(x+(P/2)+1,y+(Q/2)+1) = exp(-(D*D)/(2*D_0*D_0));
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:1:M,1:1:N);
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function h_gaussian_Callback(hObject, eventdata, handles)
    % hObject    handle to h_gaussian (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            D_0 = 60;
            H_1(x+(P/2)+1,y+(Q/2)+1) = 1 - exp(-(D*D)/(2*D_0*D_0));   
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
           
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function s_gaussian_Callback(hObject, eventdata, handles)
    % hObject    handle to s_gaussian (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    n = 1;
    for x = (-P/2):(P/2)-1
         for y = (-Q/2):(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            D_0 = 250;
            W = 50;
            H_1(x+(P/2)+1,y+(Q/2)+1) = 1 - exp(-((D*D-D_0*D_0)/(D*W))^2);     
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
           
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function p_gaussian_Callback(hObject, eventdata, handles)
    % hObject    handle to p_gaussian (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    
    F = fft2(fc,P,Q);
    H_1 = zeros(P,Q);
    n = 1;
    for x = (-P/2):(P/2)-1
         for y = (-Q/2):(Q/2)-1
            D = (x^2 + y^2)^(0.5);
            D_0 = 250;
            W = 50;
            H_1(x+(P/2)+1,y+(Q/2)+1) = exp(-((D*D-D_0*D_0)/(D*W))^2);     
         end
    end
    
    G = H_1 .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
           
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    % --------------------------------------------------------------------
    function t_gaussian_Callback(hObject, eventdata, handles)
    % hObject    handle to t_gaussian (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    P = 2*M;
    Q = 2*N;
    fc = zeros(M,N);
    
    for x = 1:M
        for y = 1:N
            fc(x,y) = img(x,y) * (-1)^(x+y);
        end
    end
    
    F = fft2(fc,P,Q);
    H = ones(P,Q);
    for x = (-P/2):1:(P/2)-1
         for y = (-Q/2):1:(Q/2)-1
            D0=20;
            u_k = 50;
            v_k = 70; 
            D = ((x+u_k)^2 + (y+v_k)^2)^(0.5);
            H(x+(P/2)+1,y+(Q/2)+1) = H(x+(P/2)+1,y+(Q/2)+1)*(1 - exp(-0.5*(D*D/D0^2)));
            D = ((x-u_k)^2 + (y-v_k)^2)^(0.5);
            H(x+(P/2)+1,y+(Q/2)+1) = H(x+(P/2)+1,y+(Q/2)+1)*(1 - exp(-0.5*(D*D/D0^2)));
         end
    end
    
    G = H .* F;
    g = real(ifft2(G));
    g = g(1:M,1:N);       
    
    for x = 1:M
        for y = 1:N
            g(x,y) = g(x,y) * (-1)^(x+y);
        end
    end
    figure;
    subplot(1,2,1);
    imshow(log(1 + abs(G)),[ ]);
    subplot(1,2,2);
    imshow(mat2gray(g));
    
    
    % --------维纳滤波-------------------------------------------------------
    function wiener_Callback(hObject, eventdata, handles)
    % hObject    handle to wiener (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img=getimage(handles.axes1);
    len=20;theta=30;
    psf=fspecial('motion',len,theta);
    I=im2double(img);
    noise_var=0.0001;
    estimated_nsr = noise_var / var(I(:));
    f=deconvwnr(img, psf,estimated_nsr);
    figure;
    imshow(f);
    
    % --------逆滤波----------------------------------------------------------
    function inverse_Callback(hObject, eventdata, handles)
    % hObject    handle to inverse (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img=getimage(handles.axes1);
    len=20;theta=30;
    psf=fspecial('motion',len,theta);
    f=deconvwnr(img, psf, 0);
    figure;
    imshow(f);

    % ********添加噪声***************************************************
    function add_noise_Callback(hObject, eventdata, handles)
    % hObject    handle to add_noise (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % ---------椒盐噪声-------------------------------------------------------
    function salt_pepper_Callback(hObject, eventdata, handles)
    % hObject    handle to salt_pepper (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    noise_gray = imnoise(img,'salt & pepper',0.05);
    axes(handles.axes1);
    imshow(noise_gray);
    
    % --------高斯噪声---------------------------------------------------------
    function gaussian_noise_Callback(hObject, eventdata, handles)
    % hObject    handle to gaussian_noise (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    noise_gray = imnoise(img,'gaussian',0,0.0001);
    axes(handles.axes1);
    imshow(noise_gray);
    
    % ---------均匀噪声--------------------------------------------------------
    function uniform_Callback(hObject, eventdata, handles)
    % hObject    handle to uniform (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N]=size(img);
    a = 0;  
    b = 0.3;  
    n_Uniform = a + (b-a)*rand(M,N);  
    noise_gray = im2uint8(im2double(img) + n_Uniform);
    axes(handles.axes1);
    imshow(noise_gray);
    
    % --------周期噪声------------------------------------------------------------
    function periodic_noise_Callback(hObject, eventdata, handles)
    % hObject    handle to periodic_noise (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N] = size(img);
    % I = img;
    % for i=1:M
    %     for j=1:N
    %       I(i,j)=I(i,j)+10*sin(20*i)+10*sin(20*j); 
    %     end
    % end
    I=im2double(img);
    r=imnoise3(M,N,[50,50]);
    I=I+r;
    axes(handles.axes1);
    imshow(I);
    
    
    % ---------泊松噪声-------------------------------------------------------
    function poisson_noise_Callback(hObject, eventdata, handles)
    % hObject    handle to poisson_noise (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    noise_gray = imnoise(img,'poisson');
    axes(handles.axes1);
    imshow(noise_gray);
    
    % ---------瑞利噪声--------------------------------------------------------
    function Rayleigh_Callback(hObject, eventdata, handles)
    % hObject    handle to Rayleigh (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N]=size(img);
    a = -0.5;  
    b = 1;  
    n_rayleigh = a + (-b .* log(1 - rand(M,N))).^0.5;
    noise_gray = im2uint8(im2double(img) + n_rayleigh);
    axes(handles.axes1);
    imshow(noise_gray);
    
    % --------伽马噪声------------------------------------------------------------
    function Erlang_Callback(hObject, eventdata, handles)
    % hObject    handle to Erlang (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    [M,N]=size(img);
    a = 25;  
    b = 3;  
    n_Erlang = zeros(M,N);     
    for i=1:b  
        n_Erlang = n_Erlang + (-1/a)*log(1 - rand(M,N));  
    end  
    noise_gray = im2uint8(im2double(img) + n_Erlang);
    axes(handles.axes1);
    imshow(noise_gray);
    
    % --------运动模糊---------------------------------------------------------
    function motion_blur_Callback(hObject, eventdata, handles)
    % hObject    handle to motion_blur (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img = getimage(handles.axes1);
    len=20;theta=30;
    psf=fspecial('motion',len,theta);
    noise_gray=imfilter(im2double(img),psf,'conv','circular');
    axes(handles.axes1);
    imshow(noise_gray);
    
    
    %  *******重置图片********************************************************
    % --- Executes on button press in pushbutton_reset.
    function pushbutton_reset_Callback(hObject, ~, handles)
    % hObject    handle to pushbutton_reset (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global gray;
    global im;
    axes(handles.axes1);
    if isempty(gray)
        imshow(im);
    else
        imshow(gray);
    end;
    
    
    
    %  *******变化面板********************************************************
    % --------------------------------------------------------------------
    function Trans_Callback(hObject, eventdata, handles)
    % hObject    handle to Trans (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    function Transform_Callback(hObject, eventdata, handles)
    % hObject    handle to Transform (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    set(handles.uipanel1,'visible','on');
    
    % --- Executes on button press in Close.
    function Close_Callback(hObject, eventdata, handles)
    % hObject    handle to Close (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    set(handles.uipanel1,'visible','off');
    
    % ---------水平镜像-------------------------------------------------------
    % --- Executes on button press in pushbutton_horizontal.
    function pushbutton_horizontal_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton_horizontal (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img=getimage(handles.axes1);
    [r,c] = size(img);
    result=zeros(r,c);
    for i=1:r
        for j=1:c
            x = i;
            y=c-j+1;
            result(x,y)=img(i,j);
        end;
    end;
    axes(handles.axes1);
    imshow(uint8(result));
    
    
    % ---------垂直镜像-------------------------------------------------------
    % --- Executes on button press in pushbutton_vertical.
    function pushbutton_vertical_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton_vertical (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img=getimage(handles.axes1);
    [r,c] = size(img);
    result=zeros(r,c);
    for i=1:c
        for j=1:r
            x=r-j+1;
            y = i;
            result(x,y)=img(j,i);
        end;
    end;
    axes(handles.axes1);
    imshow(uint8(result));
    
    
    % ---------转置-------------------------------------------------------
    % --- Executes on button press in pushbutton_transpose.
    function pushbutton_transpose_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton_transpose (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    img=getimage(handles.axes1);
    [r,c] = size(img);
    result=zeros(r,c);
    for i=1:c
        for j=1:r
            result(i,j)=img(j,i);
        end;
    end;
    axes(handles.axes1);
    imshow(uint8(result));
    
    
    % **********伪彩色******************************************************
    function pseudo_colour_Callback(hObject, eventdata, ~)
    % hObject    handle to pseudo_colour (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global gray; 
    im=double(gray);
    [m,n]=size(im);
    L=256;
    for i=1:m
        for j=1:n
    if im(i,j)<=L/5
        R(i,j)=0;
        G(i,j)=5*im(i,j);
        B(i,j)=L;
    else if im(i,j)<=L*2/5
            R(i,j)=0;
            G(i,j)=L;
            B(i,j)=-5*im(i,j)+5/2*L;
        else if im(i,j)<=3*L/5
                R(i,j)=5*im(i,j)-4*L;
                G(i,j)=L;
                B(i,j)=0;
             else if im(i,j)<=4*L/5
                R(i,j)=5*im(i,j)-4*L;
                G(i,j)=L;
                B(i,j)=0;
                else
                    R(i,j)=L;
                    G(i,j)=-5*im(i,j)+5*L;
                    B(i,j)=0;
                 end;
            end
        end
    end
        end
    end
    for i=1:m
        for j=1:n
            rgbim(i,j,1)=R(i,j);
            rgbim(i,j,2)=G(i,j);
            rgbim(i,j,3)=B(i,j);
        end
    end
    rgbim=rgbim/256;
    figure;
    subplot(1,2,1);
    imshow(gray);
    subplot(1,2,2);
    imshow(rgbim);

    % **********迭代法分割******************************************************
    % --- Executes on button press in Iteration.
    function Iteration_Callback(hObject, eventdata, handles)
    % hObject    handle to Iteration (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    I=getimage(handles.axes1);
    f=im2double(I);
    T=0.5*(min(f(:))+max(f(:)));
    sign=false;
    while ~sign
        g=f>=T;
        Tn=0.5*(mean(f(g))+mean(f(~g)));
        sign=abs(T-Tn)<0.1;
        T=Tn;
    end;
    T
    figure;
    imhist(f);
    hold on;
    plot([T,T],[0,1000],'r');
    im=im2bw(f,T);
    figure
    imshow(im);
    
    
    
    
    % **********局部分割******************************************************
    % --- Executes on button press in Partial.
    function Partial_Callback(hObject, eventdata, handles)
    % hObject    handle to Partial (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    I = getimage(handles.axes1);
    background=imopen(I,strel('disk',15));
    % Display the Background Approximation as a Surface
    figure(2);surf(double(background(1:8:end,1:8:end))),zlim([0 255]);
    title('background');set(gca,'ydir','reverse');
    %Subtract the Backround Image from the Original Image
    I2=imsubtract(I,background);
    figure(1);subplot(221);imshow(I2);title('Remove the background');
    %Increase the Image Contrast
    I3=imadjust(I2); 
    figure(1);subplot(222);imshow(I3);title('Adjust the contrast');
    level=graythresh(I3);
    bw=im2bw(I3,level);
    figure(1),subplot(223),imshow(bw);title('Binarized image');
    [labeled,numObjects]=bwlabel(bw,4); % 4联通
    RGB_label=label2rgb(labeled,'spring','c','shuffle');
    figure(1),subplot(224),imshow(RGB_label);title('Final image');
    
    % **********大津法分割******************************************************
    % --- Executes on button press in otsu.
    function otsu_Callback(hObject, eventdata, handles)
    % hObject    handle to otsu (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    I = getimage(handles.axes1);
    Im=I;
    I=double(I);
    [m,n]=size(I);
    Smax=-1;
    for T=0:255
        sum1=0;num1=0;
        sum2=0;num2=0;
        for i=1:m
            for j=1:n
                if I(i,j)>=T
                    sum2=sum2+I(i,j);
                    num2=num2+1;
                else
                    sum1=sum1+I(i,j);
                    num1=num1+1;
                end;
            end;
        end;
        ave1=sum1/num1;
        ave2=sum2/num2;
        ave=(sum1+sum2)/(m*n);
        d1=-1;d2=-1;
        for i=1:m
            for j=1:n
                if I(i,j)>=T
                    d=(I(i,j)-ave2)^2;
                    if d2==-1
                        d2=d;
                    else
                        d2=d2+d;
                    end
                else
                    d=(I(i,j)-ave1)^2;
                    if d1==-1
                        d1=d;
                    else
                        d1=d1+d;
                    end;
                end
            end
        end
        p1=num1/(m*n);
        p2=num2/(m*n);
        S1=p1*(ave1-ave)^2+p2*(ave2-ave)^2;
        S2=p1*d1+p2*d2;
        S=S1/S2;
        if S>Smax
            Smax=S;
            Th=T;
        end
    end
    Th
    figure;
    imhist(Im);
    hold on;
    plot([Th,Th],[0,1000],'r');
    for i=1:m
        for j=1:n
            if I(i,j)>=Th
                I(i,j)=255;
            else
                I(i,j)=0;
            end
        end
    end
    figure;
    imshow(I);

    如有什么不妥之处还望不吝赐教!

    展开全文
  • 下列表格中除了个别函数外,其余函数都是图像处理工具箱提供的关于图像处理的函数,现摘录到此以备查找。 表1 图像显示 函数名 功能说明 函数名 功能说明 colorbar 颜色条显示 ...
  • 函数功能: 在MATLAB中,该函数用于返回图像的一个裁剪区域。 调用格式: I2 = imcrop(I) X2 = imcrop(X,map) RGB2 = imcrop(RGB) 这三种调用格式把图像显示在一个图像窗口中, 并允许用户以交互方式...
  • matlab图像处理小结

    2013-06-28 20:09:54
    转自... matlab 图像分割算法源码 1.图像反转 MATLAB程序实现如下: I=imread('xian.bmp'); J=double(I); J=-J+(256-1); %图像反转线性变换 H=uint8(J); subplot(1,2,1),imshow(I);
  • 计算机图像处理实验课1-9,看完本课程可以让你matlab技能小小进步,对图像处理稍微了解。、 (一)matlab数字图像处理实验-guide的简单使用教程 (二)matlab数字图像处理实验-图像的几何变换 (三)matlab数字...
  • 概念定义 动态范围:灰度跨跃的值域称为动态范围。上限取决于饱和度,下限取决于噪声。...图像内插:已知数据来估计未知位置的数值的处理。在放大、缩放、旋转和几何校正中广泛使用,是基本的重取样方法。 最近邻
  • Matlab图像处理新手——图像分割和画框显示篇 **前言:**许多人说Matlab对未来的工作没什么用,就目前了解的情况而言并非如此。如:现版本已有matlab转化成C语音的功能;matlab与DSP(API协议技术)、FPGA、西门子...
  • MATLAB数字图像处理实战》深入浅出地介绍了数字图像基础和MATLAB数字图像处理的新功能及其应用案例,同时还结合作者科研、教学的经验,对数字图像处理的学习、思维、方法和技巧进行了总结与点拨。本书分为“基础”...
  • 此为本人MATLAB图像处理课的大作业,对于初学者可以参考
  • Atitit MATLAB 图像处理 经典书籍attilax总结   1.1. MATLAB数字图像处理1 1.2. 《MATLAB实用教程(第二版)》((美)穆尔 著)【简介_书评_在线阅读】 - 当当图书.html1 1.3. 数字图像处理MATLAB版)...
  • Matlab图像预处理总结

    2020-06-02 23:31:19
    (1)数字图像矩阵傅立叶变换(2)图像压缩(3)增强图像对比度(4)直方图均匀化(5)图像样本增强(6)图像滤波(7)图像均值滤波(8)图像自适应魏纳滤波(9)5种梯度增强法图像锐化(10)图像的高通滤波和掩模处理(11)图像平滑处理...
  • Atitit MATLAB 图像处理attilax总结     1.1. 下载 Matlab7.0官方下载_Matlab2012 v7.0 官方简体中文版-办公软件-系统大全.html1 1.2. Matlab cli接口 命令行执行m文件1 1.3. Matlab编程语言2 1.4. ...
  • Matlab 图像处理

    2016-12-17 21:14:41
    Matlab中有很多图像处理函数,在这篇博文中加以记录,经常更新
  • 基于MATLAB图像处理的中值滤波、均值滤波以及高斯滤波的实现与对比 1.背景知识 中值滤波法是一种非线性平滑技术,它将每一像素点的灰度值设置为该点某邻域窗口内的所有像素点灰度值的中值. 中值滤波是基于排序...
  • MATLAB图像处理中常用的低通滤波有三种。 第一种是理想的低通滤波器。即将设定范围圆半径内的所有频率都可以正常通过,圆以外的频率全部归零。 第二种是布特沃斯低通滤波器(BLPF) 相对平缓的过渡滤波 第三种是...
1 2 3 4 5 ... 20
收藏数 9,448
精华内容 3,779
关键字:

matlab图像处理总结