zoukankan      html  css  js  c++  java
  • MATLAB好玩的代码以及基础学习网站

    https://www.yiibai.com/matlab/matlab_commands.html  基础学习网站

    https://www.jianshu.com/p/0068dd5a3939

    我的电脑里还有一些在火狐浏览器自动下载的地方  好多

    (1)图像显示及转换内容
    example 1:
    >>clear;close all;                          %清除MATLAB所有工作变量,关闭已打开的图形窗口
    I=imread('pout.tif');imshow('pout.tif');    %读取图像
    whos;                                       %查图像的存储内存
    figure,imhist(I);                           %创建图像直方图
    I2=hiateq(I);fugure,imshow(I2);             %直方图均衡化,灰度值扩展到整个灰度范围【0,255】,提高对比度
    imwrite(I2,'pout2.tif');                    %保存图像
    imfinof('pout2.tif')                        %检查新生成图像内容

    example 2
    >>clear;close all;I=imread('rice.png');imshow('rice.png');  %读取和显示图像
    background=imopen(I,strel('disk',15));                      %估计图像背景
    I2=imsubtract(I,background);figure,imshow(I2);              %从原始图像中减去背景图像
    I3=imadjust(I2,stretchlim(I2),[0 1]);figure,imshow(I3);     %调节图像对比度
    level=graythresh(I3);bw=im2bw(I3,level);figure,imshow(bw);  %使用阈值操作将图像转换为二进制图像
    【labeled,numobjects】=bwlabel(bw,4);                       %检查图像中的对象个数
    grain=imcrop(labeled);                                      %检查标记矩阵
    RGB_label=label2rgb(labeled,@spring,'c','shuffle');
    imshow(RGB_label);                                          %将标记矩阵转换为伪彩色的索引图像
    graindata=regionprops(labeled,'basic')                      %调节图像中对象或区域的属性
    allgrains【graindata.area】;max(allgrains)                  %max获得最大的米粒大小
    ans=
         695
    biggrain=find(allgrains==695)                               %返回最大尺寸米粒的标记号
    biggrain=
          68
    mean(allgrains)                                             %获取米粒尺寸平均大小
    ans=
          249
    hist(allgrains,20);                                         %绘制一个包含20柱的直方图来说明米粒大小分布情况

    example 3
    RGB=reshape(ones(64,1)*reshape(jet(64),1,192)[64,64,3]);
    R=RGB(:,:,1);G=RGB(:,:,2);B=RGB(:,:,3);
    subplot(221),imshow(R);subplot(222),imshow(G);subplot(223),imshow(B);subplot(224),imshow(RGB);

    example 4 使用索引图像chess.met的颜色图map,通过抖动map中的颜色,产生RGB图像autumn.tif的索引图像
    >>%-- 2014-3-1 22:50 --%
    load chess;
    RGB=inread('autumn.tif');
    RGB=imread('autumn.tif');
    subplot(121),imshow(RGB);
    Y=dither(RGB,map);
    subplot(122),imshow(Y,map);
    %-- 2014-3-2 17:00 --%

    example 5 将灰度图像转换成索引图像,颜色图分别为gray(128),gray(16)
    >> I=imread('pout.tif');
    >> [I1,map1]=gray2ind(I,128);
    >> [I2,map2]=gray2ind(I,16);
    >> subplot(131),imshow(I1,map1);subplot(132),imshow(I2,map2);subplot(133),imshow(I);
    example 6 将一幅索引图trees.mat转换为灰度图
    >>load trees
    I=ind2gray(X,map);
    subplot(121),imshow(X,map);subplot(122),imshow(I);

    example 7 将RGB图像onion.png转换为灰度图像
    >>RGB=imread('onion.png');
    figure(1);imshow(RGB);figure(2);Y=rgb2gray(RGB);imshow(Y);

    example 8 将RGB图像onion.png转换为索引图像
    >>RGB=imread('onion.png');
    figure(1);imshow(RGB);figure(2);Y=rgb2ind(RGB,128);imshow(Y);

    example 9 将索引图像wmandril.mat转换为RGB图像
    >>load wmandril;figure(1);imshow(X,map);I=ind2rgb(X,map);figure(2),imshow(I);

    example 10 通过阀值法将索引图像转换为二值图像,阀值0.4
    >>load trees;
    BW=im2bw(X,map,0.4);figure(1);imshow(X,map);figure(2),imshow(BW);

    example 11 将一幅灰度图像pout.tif转换为索引图像
    >>I=imread('pout.tif');figure(1),imshow(I);X=grayslice(I,16);figure(2),imshow(X,hot(16));

    example 12 将图像滤波后产生的矩阵转换为灰度图像
    >>I=imread('trees.tif');figure(1),imshow(I);
    J=filter2(fspecial('sobel'),I);K=mat2gray(J);figure(2),imshow(K);

    example 13 过滤一个类为unit8的图像,然后将其显示为灰度图,并添加呀颜色条
    >>I=imread('trees.tif');h=[121;000;-1-2-1];J=filter2(h,I);imshow(J,[]);colorbar;

    example 14 并排显示两幅图像
    [X1,map1]=imread('forest.tif');[X2,map2]=imread('trees.tif');subplot(121),imshow(X1,map1);subplot(122),imshow(X2,map2);
    [X1,map1]=imread('forest.tif');[X2,map2]=imread('trees.tif');subplot(121),subimage(X1,map1);subplot(122),subimage(X2,map2);

    (2)图像运算内容
    example 1 将灰度图像使用灰度变换函数进行线性点运算
    >>rice=imread('rice.png');I=double(rice);J=I*0.43+60;rice2=uint8(J);subplot(121),imshow(rice);subplot(122),imshow(rice2);

    eaxmple 2 两幅图像叠加
    >>I=imread('rice.png');J=imread('cameraman.tif');subplot(131),imshow(I);subplot(132),imshow(J);
    K=imadd(I.J);subplot(133),imshow(K);

    example 3 图像的减法运算
    >>clear;close all;I=imread('rice.png');                     %读取和显示图像
    background=imopen(I,strel('disk',15));                      %估计图像背景亮度
    I2=imsubtract(I,background);                                %从原始图像中减去背景图像
    subplot(121),imshow('rice.png');subplot(122),imshow(I2);

    example 4 图像的乘法运算
    >>I=imread('moon.tif');J=immultiply(I,1.2);subplot(121),imshow(I);subplot(122),imshow(J);

    example 5 图像的除法运算
    >>rice=imread('rice.png');background=imopen(rice,strel('disk',15));rice2=imsubtract(rice,background);
    rice3=imdivide(rice,rice2);subplot(131),imshow(rice);subplot(132),imshow(rice2);subplot(133),imshow(rice3);

    (3) 几何变换
    example 1 使用不同的插值方法对图像进行放大
    >>load woman2
    subplot(2,2,1),imshow(X,map);
    X1=imresize(X,2,'nearest');subplot(2,2,2),imshow(X1,[]);
    X2=imresize(X,2,'bilinear');subplot(2,2,3),imshow(X2,[]);
    X3=imresize(X,2,'bicubic');subplot(2,2,4),imshow(X3,[]);

    example 2 图像旋转
    >>I=imread('trees.tif');J=imrotate(I,35,'bilinear');subplot(121),imshow(I);subplot(122),imshow(J);

    example 3 图像剪裁
    >>I=imread('trees.tif');J=imcrop(I);imshow(J);

    example 4 图像的滑动平均操作
    >>I=imread('trees.tif');I2=colfilt(I,[5 5],'sliding','mean');figure(1),imshow(I);figure(2),imshow(I2,[]);

    example 4 对输入图像处理,输出图像为每个像素领域的最大值
    >>I=imread('trees.tif');figure(1),imshow(I);f=inline('ones(64,1)*mean(x)');
    I2=colfilt(I,[8 8],'distinct',f);figure(2),imshow(I2,[]);

    example 4 调用nlfilter函数进行滑动操作
    >>I=imread('tire.tif');f=inline('max(x(:))');J=nlfilter(I,[3 3],f);
    subplot(121),imshow(I);subplot(122),imshow(J);

    example 5 图像块操作 计算图像8x8区域的局部标准差
    >>I=imread('tire.tif');f=inline('uint8(round(std2(x)*ones(size(x))))');
    I2=blkproc(I,[8 8],f);subplot(121),imshow(I);subplot(122),imshow(I2);

    example 6 返回像素或像素集的数据值
    >>imshow canoe.tif;vals=impixel  %在显示图像上点几个点,回车后显示数据值

    example 7 强度描述图 improfile函数用于沿着图像一条直线路径或直线路径计算并绘制其强度值(灰度值)
    >>imshow debyel.tif;improfile     %确定直线段后,按回车键,得到轨迹强度图
    >>RGB=imread('peppers.png');figure(1),imshow(RGB);improfile

    example 8 图像轮廓图
    >>I=imread('rice.png');imshow(I);figure;imcontour(I);

    example 9 图像柱状图
    >>I=imread('rice.png');imhist(I,64);

    ( 4 )图像分析
    example 1 灰度边缘检测
    >>RGB=imread('peppers.png');figure(1);imshow(RGB);          %调入及显示RGB图
    I=rgb2gray(RGB);figure(2),imshow(I);colorbar('horiz');      %RGB图转换为灰度图
    ED=edge(I,'sobel',0.08);figure(3);imshow(ED);               %边缘检测,thresh值越小,显示细节越多

    example 2 Sobel边界探测器和Canny边界探测器在图像分析中的应用
    >>I=imread('rice.png');BW1=edge(I,'sobel');BW2=edge(I,'Canny');
    figure(1),imshow(I);figure(2),imshow(BW1);figure(3),imshow(BW2);

    ( 5 )特定区域处理
    example 1 根据指定的坐标选择一个六边形区域(选中区域为白色,其余的为黑色)
    >>I=imread('eight.tif');c=[222 272 300 272 222 194];r=[21 21 75 121 121 75];
    BW=roipoly(I,c,r);subplot(121),imshow(I);subplot(122),imshow(BW);

    example 2 按灰度分割图像中的目标
    >>I=imread('rice.png');BW=roicolor(I,128,255)                 %选择图像灰度范围在128和255之间的像素
    subplot(121),imshow(I);subplot(122),imshow(BW);

    example 3 特定区域滤波,对指定区域进行锐化滤波
    >>I=imread('eight.tif');c=[222 272 300 272 222 194];r=[21 21 75 121 121 75];BW=roipoly(I,c,r);
    h=fspecial('unsharp');     %指定滤波算子为'unsharp'
    J=roifilt2(h,I,BW);subplot(121),imshow(I);subplot(122),imshow(J);

    example 4 特定区域填充:填充指定的区域
    >>I=imread('rice.png');c=[52 72 300 270 221 194];r=[71 21 75 121 121 75];
    J=roifill(I,c,r);subplot(121),imshow(I);subplot(122),imshow(J);

    ( 6 )图像变换,傅里叶变换,
    example 1 一幅图像的二维傅里叶变换
    >>figure(1);load imdemos saturn2;                          %装入原始图像
    imshow(saturn2);                                           %显示图像
    figure(2);B=fftshift(fft2(saturn2));                       %进行傅里叶变换;
    %fftshift(I)函数用于调整fft,fft2,fftn的输出结果。对于向量,fftshift(I),将I的左右两半交换位置;对于矩阵I,
     fftshift(I)将I的一,三象限和二四象限进行互换;对于高维矢量,fftshift(I)将矩阵各维的两半进行互换。
    imshow(log(abs(B)),[]);colormap(jet(64)),colorbar;         %显示变换后的系数分布

    example 2 利用freqz2函数得到的高斯滤波器的频率响应
    >>h=fspecial('gaussian');freqz2(h)

    example 3 一个计算魔方阵和一个矩阵的卷积
    >>A=magic(3);B=ones(3);A(8 ,8)=0;B(8 ,8)=0;        %对A,B进行零填充,使之成为8X8矩阵
    C=ifft2(fft2(A).*fft2(B));
    C=C(1:5,1:5);                                    %抽取矩阵中的非零部分
    C=real(C)                                        %去掉错误的,由四舍五入产生的虚部

    example 4 图像特征识别:将包含字母a的图像与text.tif图像进行相关运算,也就是首先将字母a和图像text.tif进行傅里叶变换,然后利用快速卷积的方法,计算字母a和图像text.tif的卷积,提取卷积运算的峰值,图中所示的白色亮点,即得到在图像text.tif中字母a定位的结果
    >>I=imread('text.tif');a=I(59:71:81:91);         %从图像中提取字母a的图像
    subplot(221),imshow(I);subplot(222),imshow(a);
    C=real(ifft2(fft2(I).*fft2(rot90(a,2),256,256)));subplot(223),imshow(C,[]);
    thresh=max(C(:));                                %找到C中的最大值,选择一个略小于该数的数值作为阈值
    subplot(224),imshow(C>thresh);                   %显示像素值超过阈值

    example 5 离散余弦变换应用:把输入图像划分成8x8的图像块,计算它们的DCT系数,并且只保留64个DCT系数中的10个,然后对每个图像块利用这10个系数进行DCT反变换来重构图像
    >>    
    example 6
    >>RGB=imread('autumn.tif');

  • 相关阅读:
    JavaScript—— scroolleftoffsetleft 系列的含义以及浏览器兼容问题
    GCD
    Treap
    快速* 模板
    线性筛素数
    珂朵莉树
    One Night
    长整数相加运算(内含减法)。。= =
    骑士周游 非完美版。。= =
    《Windows取证分析》
  • 原文地址:https://www.cnblogs.com/pengzhi12345/p/11236745.html
Copyright © 2011-2022 走看看