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');

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM