Matlab教學2

Report
Matlab教學
Speaker:陳珮妮
Date:2013/03/21
MATLAB的影像格式
 MATLAB 最常處理的影像格式為索引影像
(Indexed Images)
 顯示此類型影像的語法如下:
image(X)
colormap(map)
其中X為影像的資料矩陣,map為色盤矩陣。
 色盤矩陣的大小為K×3,每個橫列由三個元素所組成,分別
是R(紅) 、G(綠)、B(藍),每個元素的範圍為0~1
 X的值為1~K,也就是當X(i, j)的值為p,則像素點(i, j) 的顏色
為map(p, :)這一列的向量所決定。
影像檔案的讀取與寫入
 imread 指令可用於讀取影像檔案。
 imwrite 則可用於寫入影像檔案。
 這兩個指令可以處理的影像格式有下列幾種:
影像檔案格式
副檔名
相關字串
微軟視窗的(Bitmap)
bmp
‘bmp’
階層式資料格式(Hierarchical Data Format)
hdf
‘hdf’
Joint Photographic Expert Group
jpg 或 jpeg
‘jpg’ 或 ‘jpeg’
微軟視窗的(Paintbrush)
pcx
‘pcx’
可攜式網路圖形(Portable Network Graphics)
png
‘png’
標記式影像檔案格式(Tagged Image File Format)
tiff
‘tif’ 或 ‘tiff’
X視窗傾印(X Windows Dump)
xwd
‘xwd’
圖形交換格式(Graphic Interchange Format)
gif
‘gif’
imfinfo指令
 imfinfo 指令可傳回影像檔案的各項資訊,例如:
 info1=imfinfo('simulinkteam.jpg')
 info2=imfinfo('sbtree.gif')
 對於不同的檔案格式,imfinfo 傳回的資訊項目可能有所不
info2=imfinfo('sbtree.gif')
同。 info1=imfinfo(‘simulinkteam.jpg')
info1 =
info2 =
Filename: 'simulinkteam.jpg'
FileModDate: '28-三月-2000 17:30:36'
FileSize: 24071
Format: 'jpg'
FormatVersion: ''
Width: 234
Height: 126
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: ''
NumberOfSamples: 3
CodingMethod: 'Huffman'
CodingProcess: 'Sequential'
Comment: {[1x70 char]}
Filename: 'sbtree.gif'
FileModDate: '10-九月-1997 14:53:14'
FileSize: 7121
Format: 'GIF'
FormatVersion: '87a'
Width: 99
Height: 80
BitDepth: 8
ColorType: 'indexed'
FormatSignature: 'GIF87a'
BackgroundColor: 0
AspectRatio: 0
ColorTable: [256x3 double]
Interlaced: 'no'
特效目錄
1)
Gray scale 灰階
2)
Negative 負片效果
3)
Flipping vertically 垂直翻轉
4)
Flipping horizontally 水平翻轉
5)
Switching top left and bottom right 垂直水平翻轉
6)
Darkening 色調加深
7)
Lightening 色調加亮
8)
Scrolling vertically 垂直滾動
9)
Scrolling horizontally 水平滾動
10)
Zooming in 放大
11)
Zomming out 縮小
12)
Blurring 模糊
Gray figure灰階圖片
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 imwrite(gri,'gray.jpg');
Negative 負片效果
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 nei = imcomplement(gri); %將灰階圖片做負片化
 imshow(nei);
 imwrite(nei,'negative.jpg');
Flipping vertically 垂直翻轉
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 fvim = gri(end:-1:1, :,:); %將灰階圖片垂直翻轉
 fvim = flipud(gri); %將灰階圖片垂直翻轉,沿水平軸
 imshow(fvim);
 imwrite(fvim,'flipping vertically.jpg');
Flipping horizontally 水平翻轉
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 fhim = fliplr(gri); %將灰階圖片水平翻轉,沿垂軸
 imshow(fhim);
 imwrite(fhim, 'flipping horizontally.jpg');
Switching top left and bottom right
垂直水平翻轉
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 sim = fliplr(fvim); %將垂直翻轉的圖片在做水平翻轉
 imshow(sim);
 imwrite(sim,'switchin.jpg');
Darkening 色調加深
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 %brighten(-0.8); %亮度變深,目前指令沒有改變對上一個圖像加深
 dim = imadjust(gri,[ ],[ ],2); %亮度變深
 imshow(dim);
 imwrite(dim,'darkening.jpg');
Lightening 色調加亮
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 brighten(0.8); %亮度變亮,目前指令沒有改變對上一個圖像加亮
Scrolling vertically 垂直滾動
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 svim = circshift(gri,[-300,0]); %垂直滾動,[a,b],a 決定垂直滾動負值像上
正值向下
 imshow(svim);
 imwrite(svim,'scroll vertically.jpg');
Scrolling horizontally 水平滾動
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 shim = circshift(gri,[0,-300]); %水平滾動,[a,b],b 決定水平滾動負值像左
正值向右
 imshow(shim);
 imwrite(shim,'scroll horizontally.jpg');
Zooming in 放大
 [X Y] = size(gri); %把灰階突的大小存到矩陣
 ziim = imresize(gri,2); %把灰階圖放大
 imshow(ziim);
 imwrite(ziim,'zooming in.jpg');
 zim = imcrop(ziim,[500 500 X Y]); %裁切放大後的灰階圖從(500,500)的位
置開始,裁下原灰階圖的大小
 imshow(zim);
 imwrite(zim,'zooming in end.jpg');
Zomming out 縮小
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 zoim = imresize(gri,[50 50]); %縮小到50*50的大小
 imshow(zoim);
 imwrite(zoim,'zooming out.jpg');
Blurring 模糊
 ori = imread('Shunsuke.jpg'); %讀入圖片
 gri = rgb2gray(ori); %將RGB圖片轉成Gray figure
 imshow(gri);
 b = fspecial('average',[10 10]); %制定一個20*20的平均濾波
 bim = imfilter(gri,b); %將平均濾波加入到灰階圖中,使圖變模糊
 imshow(bim);
 imwrite(bim,'blurring.jpg');

similar documents