Matlab實現多種圖像配準
本文講述如何利用Matlab Image Processing Toolbox中的圖像配準工具實現線性正投影、仿射、投影、多項式、分段線性、局部加權平均配準的過程。
實驗平臺
X86 PC,Windows XP sp2, Matlab 7.1
資源的獲取
圖片資源來自http://vision.ece.ucsb.edu/registration/satellite/testimag.html,其中每個壓縮包裏存有兩副圖片,每副圖片以矩陣形式保存。
matlab工具的使用方法:查看幫助mage Processing Toolbox User's Guide——Image registration。
涉及配準方法簡介
該工具箱提供的配準方法均需手工選擇圖像間的匹配點對(control points pair),均屬於交互配準方法。其基本過程爲:讀入圖像數據->在兩副圖像上選擇足夠匹配點->選擇配准算法,計算變換參數->變換圖像。
假設input image(輸入圖像)爲欲進行配準的圖像,base image爲配準是的參考圖像。以下是我參考matlab幫助給出了簡介。
1.線性正投影(linear conformal):最簡單。平面映射成平面。
當輸入輸入圖像與參考圖像對比,只是存在全局的平移、旋轉、縮放或其三者組合的差別時(正方形仍對應正方形),選擇此配準方法。此方法至少需要2對匹配點。
2.仿射(affine):將平行線轉換成平行線。
當輸入圖像形狀存在切變現象(正方形對應平行四邊形),選此法。至少需3對匹配點。
3.投影(projective):將直線映射成直線。
如果輸入圖像呈現傾斜,翹起現象,選此法。至少需4對匹配點。
4.多項式(polynomial):將直線映射成曲線。
如果輸入圖像出現不規則曲變,採用此法。Matlab中提供有2、3、4次冪的實現,分別至少需要6,10,10對匹配點。
5.分段線性(piecewise linear)
如果輸入圖像的各個局部之間的退化模式明顯不一樣,選此法。至少需要4對匹配點。
6.局部加權平均(local weighted mean)
與分段線性一致,但效果較之好。至少需要6對(推薦12對)匹配點。
實驗步驟
1.讀取圖像數據。
因爲源圖像以矩陣形式存在一個二進制的文件裏,用fread可將其讀取到變量矩陣中。將讀取文件編製成一個子函數(RTIread.m),源代碼如下:
function imMatrix=RTIread(FILENAME,SIZE)
%RTIread Read the image matrix from binary "Registration Test Image" file.
% imMatrix=RTIread(FILENAME,SIZE) opens the file FILENAME, and reads the
% number of elements specified by SIZE.
%
% FILENAME is a string containing the name of the file to be opened.
% Valid entries for SIZE are:
% N read N elements into a column vector.
% inf read to the end of the file.
% [M,N] read elements to fill an M-by-N matrix, in column order.
% N can be inf, but M can't.
%
% It returns the image matrix.
fid=fopen(FILENAME,'r');
imMatrix=fread(fid,SIZE,'uint8=>uint8');
fclose(fid);
%image(imMatrix);
這裏我們選取了兩張600×600的圖片,文件名爲“casitas84”和“casitas86”。運行以下代碼讀取圖像矩陣:
% 1. Read the images into the MATLAB workspace.
base=RTIread('casitas84',[600,600]);
input=RTIread('casitas86',[600,600]);
2.選取匹配點(control points)。
根據預定的配準方法,選定足夠的匹配點對。運行下列代碼:
% 2.Specify control point pairs n the images and save.
cpselect(input,base); %please select 15 points for test.
出現GUI界面。
操作很簡單,只需注意選點要均勻布開,以增加其代表性。選定完畢,File-> Save Points to Workspace將數據保存到工作區中。Workspace立刻多出兩個N×2的數組(其中N爲選定的匹配點對數),分別爲input_points和base_points,如:
input_points =
119.5185 193.5926
168.9012 242.9753
105.9383 140.5062
459.0247 131.8642
313.3457 257.7901
292.3580 165.1975
276.3086 33.0988
283.7160 380.0123
76.3086 297.2963
135.5679 83.7160
360.2593 313.3457
94.8272 446.6790
70.1358 354.0864
181.2469 361.4938
381.2469 460.2593
252.8519 433.0988
3.利用十字相關法調整選定了的匹配點。
這步可選。運行代碼:
% 3.Fine-tune the control points using cross-correlation.
input_points_corr = cpcorr(input_points,base_points,input,base); %optimism the points
input_points_corr爲優化後在輸入圖片的對應匹配點。
4.計算變換公式的參數。
利用cp2tform,選定變換類型(即配準方法),計算變換參數。以下只需選定一種即可。
% 4.Specify the type of transformation to be used and infer its parameters
% (1) not Fine-tune points
Tlinear = cp2tform(input_points,base_points,'linear conformal');
Taffine = cp2tform(input_points,base_points,'affine');
Tprojective = cp2tform(input_points,base_points,'projective');
Tpolynomial2 = cp2tform(input_points,base_points,'polynomial',2);
Tpolynomial3 = cp2tform(input_points,base_points,'polynomial',3);
Tpolynomial4 = cp2tform(input_points,base_points,'polynomial',4);
Tpiecewise = cp2tform(input_points,base_points,'piecewise linear');
Tlwm = cp2tform(input_points,base_points,'lwm');
% (2)Fine-tune points
fTlinear = cp2tform(input_points_corr,base_points,'linear conformal');
fTaffine = cp2tform(input_points_corr,base_points,'affine');
fTprojective = cp2tform(input_points_corr,base_points,'projective');
fTpolynomial2 = cp2tform(input_points_corr,base_points,'polynomial',2);
fTpolynomial3 = cp2tform(input_points_corr,base_points,'polynomial',3);
fTpolynomial4 = cp2tform(input_points_corr,base_points,'polynomial',4);
fTpiecewise = cp2tform(input_points_corr,base_points,'piecewise linear');
fTlwm = cp2tform(input_points_corr,base_points,'lwm');
諸如Tlinear的變量爲一個稱爲TFORM的數據結構,尚沒做仔細研究:
Tlinear =
ndims_in: 2
ndims_out: 2
forward_fcn: @fwd_affine
inverse_fcn: @inv_affine
tdata: [1x1 struct]
5.變換圖像。
% 5.Transform the unregistered image to bring it into alignment.
title('image registration polynomial method');
subplot(2,2,1);
imshow(base);
title('Base image');
subplot(2,2,2);
imshow(input);
title('Input image');
subplot(2,2,3);
imshow(imtransform(input,Tpolynomial2));
title('registered image');
subplot(2,2,4);
imshow(imtransform(input,fTpolynomial2));
title('registered image(fine-tune points)');
結果如下:
總結
1.image和imshow區別。前者視base,input此類二維圖片矩陣爲索引圖像,在系統的index庫中選取顏色。
2.選擇適當的方法來建立轉換參數,並非算法越複雜越好,應參考成像因素(退化因素)。
3.尚沒有看出十字相關法的好處。
4. 利用cpselect選擇匹配點,cpselect可以返回一個GUI句柄。欲實現如下功能:當打開cpselect GUI 時,m文件程序暫停運行,關閉之後繼續執行。因爲對GUI編程不懂, 使用了waitfor,pause函數都沒法實現。嘗試中……