1 影像的表示方法
常見的5種影像表示方法:分別是二進制影像,索引影像,灰度影像,RGB影像和多幀影像,
1.1二進制影像(二值影像)
通常由一個二維陣串列示,一位表示一個像素,非0即1,0表示黑色,1表示白色,
1.2 灰度影像(單色影像)
通常也由一個二維陣串列示,8位表示一個像素,0表示黑色,255表示白色,
1.3 RGB影像(真彩色影像):
利用三個大小相同的二維陣串列示,3個陣列分別代表R G B這三個分量,每種顏色分量占8位,每一位由[0,255]中的任意值表示,那么一個像素由24位表示,
2 影像的資料結構
數字影像處理中常用的資料結構有矩陣、鏈碼、拓撲結構和關系結構;
矩陣用于描述影像;
鏈碼用于描述影像的邊界,鏈碼又分為四鏈碼和八鏈碼;
拓撲結構用于描述影像的基本結構,有4-鄰域,對角鄰域,8-鄰域;
關系結構用于描述一組目標物體之間的相互關系,
3 計算機中影像檔案的基本格式
①BMP檔案格式:采用位映射存盤格式,影像深度可選,從1bits-24bits,不采用任何壓縮,所以占用的空間很大,
②GIF檔案格式:基于LZW演算法的連續色調的無損壓縮格式,存盤效率高,最多支持256種色彩的影像,
③JPEG檔案格式:使用一種有損壓縮演算法,壓縮比率通常在10:1到40:1,可以支持24bits真菜色,
④TIFF檔案格式:適用于RGB真彩色影像,
4 影像型別的轉換
4.1 RGB 影像的轉換
4.1.1 RGB 影像轉為灰度影像
I = imread('douluodalu.jpg'); %讀取影像
X = rgb2gray(I); %轉為灰色
figure; %畫布
subplot(121),imshow(I); %小圖顯示,一行兩列第一個
subplot(122),imshow(X); %小圖顯示,一行兩列第二個

4.1.2 RGB 影像轉索引影像
rgb = imread('doluodalu.jpg');
[x1, map1] = rgb2ind(rgb, 64); %將rgb影像轉為索引影像,顏色種類至少64中,最小方差量化
[x2, map2] = rgb2ind(rgb, 0.2); %將rgb影像轉為索引影像,顏色至少216中,均勻量化,
map3 = colorcube(128); %創建一個顏色映射表
x3 = rgb2ind(rgb, map3); %顏色128種,顏色表近似法
% set(0, 'defaultFigurePosition',[100, 100, 1000, 500]);
% set(0, 'defaultFigureColor', [1 1 1]);
figure;
subplot(131),imshow(x1, map1);
subplot(132),imshow(x2, map2);
subplot(133),imshow(x3, map3);

4.2 灰度影像的轉換
4.2.1 灰度影像轉索引影像
clear all;
clc;
gray = imread('gray.jpg'); %讀取灰度影像
[x, map] = gray2ind(gray, 8); %灰度影像轉索引影像,灰度級為 8
% figure,imshow(gray);
% figure,imshow(x, map);
figure;
subplot(121),imshow(gray);
subplot(122),imshow(x, map);

4.2.2 利用閾值法將灰度圖轉為索引圖
clear all;
clc;
gray = imread('gray.jpg'); %讀取灰度圖
ind = grayslice(gray, 32); %用閾值法將灰度圖轉為32級的索引圖
figure;
imshow(gray);
figure, imshow(ind, jet(32)); %jet函式生成一個顏色映射表


4.3 二值影像的轉換
4.3.1 將灰度圖轉為二值影像
clc;
close all;
gray = imread('gray.jpg');
bw1 = im2bw(gray, 0.4); %將二值影像轉為灰度影像,閾值為0.4.
bw2 = im2bw(gray, 0.6); %將二值影像轉為灰度影像,閾值為0.6,小于閾值的取零
figure;
subplot(131), imshow(gray);
subplot(132), imshow(bw1);
subplot(133), imshow(bw2);

4.3.2 索引圖轉為二值
clear;
clc;
load trees; %加載內置trees.mat,將資料載入到workspace
bw = im2bw(X, map, 0.4); %將索引圖轉換為二值影像,閾值為0.4
figure,imshow(X, map);
figure,imshow(bw);


4.3.3 數值矩陣轉為灰度影像
clear;
clc;
X = magic(256); %利用函式magic()產生一個256*256的方陣
%魔術矩陣,每一行元素之和,每一列元素之和,與對角線元素之和三者相等,
% X = imread('gray.jpg');
bw = mat2gray(X); %將數值矩陣轉為一個灰度影像
figure,imshow(bw);
5 檔案資訊的讀取
matlab中,對檔案操作,往往要知道檔案的相關資訊,如檔案名,檔案格式等等,
我們用imfinfo()這個函式來查看檔案資訊
>> info = imfinfo('cameraman.tif')
info =
Filename: 'C:\Program Files\MATLAB\MA...'
FileModDate: '04-十二月-2000 05:57:54'
FileSize: 65240
Format: 'tif'
FormatVersion: []
Width: 256
Height: 256
BitDepth: 8
ColorType: 'grayscale'
FormatSignature: [77 77 0 42]
ByteOrder: 'big-endian'
NewSubFileType: 0
BitsPerSample: 8
Compression: 'PackBits'
PhotometricInterpretation: 'BlackIsZero'
StripOffsets: [1x8 double]
SamplesPerPixel: 1
RowsPerStrip: 32
StripByteCounts: [1x8 double]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 255
MinSampleValue: 0
Thresholding: 1
Offset: 64872
ImageDescription: 'This image is distributed ...'
5.1 特殊影像格式的讀取
%網頁上保存gif圖只需拖到指定位置即可,
[gif, map] = imread('gif.gif', 2); %因為是彩色的,所以保存為索引圖,讀取第二幀的影像
[gif1, map1] = imread('gif.gif', 12); %讀取第12幀的影像
figure,
subplot(121), imshow(gif, map); %顯示第二幀的影像
subplot(122), imshow(gif1, map1);
rgb = imread('rgb.jpg');
imwrite(rgb, 'rgb.png'); %將 jpg 格式保存為 png格式,
png = imread('rgb.png', 'BackgroundColor', [1 0 0]); %讀取影像,并設定該影像的
%透明像素與白色合成,最后是RGB影像,
figure,
imshow(png)
5.2 函式 imshow( )
clc, clear;
I = imread('gray.jpg');
subplot(121),imshow(I);%顯示原圖
subplot(122),imshow(I, [60, 120]); %灰度級上下為[60, 120],灰度值較大的地方比較亮,
%灰度值較小的地方比較暗
5.3 函式 warp( )
I = imread('douluodalu.jpg');
figure;
% warp(I); %顯示原影像,效果類似imshow()
[x, y, z] = sphere;
% 創建三個(N+1)*(N+1)的矩陣,使得surf(X,Y,Z)建立一個球面,默認N=20
subplot(121),warp(I);
subplot(122),warp(x,y,z,I);
grid; %建立網格

6 影像點運算
6.1 線性灰度變換
函式 : imadjust ( )
gamma = 0.5; %調整線性度取值
I = imread('douluodalu.jpg');
R = I;
R(:,:,2) = 0; %只保留紅色通道
R(:,:,3) = 0;
R1 = imadjust(R, [0.5, 0.8], [0, 1], gamma); %調整灰度
G = I;
G(:,:,1) = 0;
G(:,:,3) = 0;
G1 = imadjust(G, [0.5, 0.8], [0, 1], gamma);
B = I;
B(:,:,1) = 0;
B(:,:,2) = 0;
B1 = imadjust(B, [0.5, 0.8], [0, 1], gamma);
I1 = R1 + G1 + B1;
r = figure;
subplot(121),imshow(R);
subplot(122),imshow(R1);
g = figure;
subplot(121),imshow(G);
subplot(122),imshow(G1);
b = figure;
subplot(121),imshow(B);
subplot(122),imshow(B1);
rgb = figure;
subplot(121),imshow(I);
subplot(122),imshow(I1);




7 影像代數運算
7.1 影像的加法運算
7.1.1 函式 imadd ()
clc;
clear all;
I = imread('douluodalu1.jpg');
J = imread('douluodalu2.jpg');
% imshow(I);
% imshow(J);
K = imadd(I, J);
subplot(131),imshow(I);
subplot(132),imshow(J);
subplot(133),imshow(K);

7.1.2 一幅圖每個像素增加亮度
clc;
clear all;
I = imread('douluodalu.jpg');
J = imadd(I, 30); %添加30的亮度
figure;
subplot(121),imshow(I);
subplot(122),imshow(J);

7.1.3 函式 imnoise ( )
通過對同一幅影像疊加取平均值 , 消除原影像的附加噪聲
clc;
clear all;
RGB = imread('douluodalu.jpg');
A = imnoise(RGB, 'gaussian', 0, 0.05); %加入了高斯噪聲的影像
I = A;
M = 3;
I = im2double(I); %噪聲圖都轉為浮點型
RGB = im2double(RGB); %原圖轉為浮點型
for i = 1: M
I = imadd(I, RGB); %噪聲和原圖疊加
end
avg_A = I / (M+1); %疊加求均值
figure;
subplot(121),imshow(A);
subplot(122),imshow(avg_A);

7.2 影像的減法
影像減法也稱為差分方法 , 是一種常用于檢測影像變化及運動物體的影像處理方法.或是混合影像的分離,
clc;
clear all;
A = imread('douluodalu1.jpg');
B = imread('douluodalu2.jpg');
C = imsubtract(A, B); %兩幅影像相減
figure;
imshow(C);
D = imadd(A, B); %兩幅影像相加
figure;
imshow(D);
figure;
subplot(121),imshow(D);
subplot(122),imshow(C);



7.3 影像乘法運算
一是實作掩膜操作,即屏蔽影像的某些操作 ; 二是如果一幅影像乘以一個常數因子,將改善影像的亮度
改善亮度,
A = imread('douluodalu.jpg');
B = immultiply(A, 1.5);
C = immultiply(A, 0.5);
figure;
subplot(131);imshow(A);
subplot(132);imshow(B);
subplot(133);imshow(C);

7.4 影像的幾何變換
影像的平移
move.m
move.m
function J = move(I, a, b)
% 定義一個函式名為move, I表示輸入影像,a和b描述影像沿著x軸和y軸移動的距離,
% 不考慮平移后,影像溢位的情況,找不到的地方都賦值為1
[M,N,G] = size(I);
I = im2double(I);
J = ones(M,N,G);
for i=1:M
for j=1:N
if((i+a)>=1&&(i+a)<=M && (j+b)>=1&&(j+b)<=N);
J(i+a, j+b, :)=I(i, j, :);
end
end
end
I = imread('douluodalu.jpg');
a = 200; b = 200;
J1 = move(I, a, b);
a = -200; b = 200;
J2 = move(I, a, b);
a = 200; b = -200;
J3 = move(I, a, b);
a = -200; b = -200;
J4 = move(I, a, b);
subplot(221),imshow(J1);
subplot(222),imshow(J2);
subplot(223),imshow(J3);
subplot(224),imshow(J4);

8 備注
完整代碼或者代寫添加QQ 1564658423
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/280689.html
標籤:其他
