1.原理简述:
根据两幅图像中的一些已知对应点(控制点对),建立函数关系式,通过坐标变换,实现失真图像的几何校正。
设两幅图像坐标系统之间畸变关系能用解析式来描述:
根据上述的函数关系,可以依次计算畸变图像每个像素的矫正坐标值,保持各像素值不变,这样生成一幅矫正图像。
2.实现过程:
(1)因此首先要得到多项式,matlab提供了拟合多项式的函数 Isqcurvefit,
格式:lsqcurvefit(f,a,x,y)
f:符号函数句柄
a:最开始预估的值(预拟合的未知参数的估计值)。如上面的问题如果我们预估A为1,B为2,则a=[1 2]
x:我们已经获知的x的值
y:我们已经获知的x对应的y的值
函数的返回值为对应多项式系数组成的一维数组。
示例(二次多项式):建立由校正图像到畸变图像的函数
function [F] = fun(a,b)x = b(:,1);y = b(:,2);F = a(1)+a(2)*x+a(3)*y+a(4)*x.^2+a(5)*x.*y+a(6)*y.^2; endx0 = fixedPoints(:,1);y0 = fixedPoints(:,2);x1 = movingPoints(:,1);y1 = movingPoints(:,2);data = [x1,y1];a = [1 1 1 1 1 1];a1 = lsqcurvefit('fun',a,data,x0);a2 = lsqcurvefit('fun',a,data,y0);
(2)根据得到的二项式,由校正图像每个像素坐标(x,y)出发,算出在已知畸变图像上的对应坐标(x',y'),使像元一一对应,赋予校正图像对应畸变图像的像元
的像素值,最终得到校正图像。
示例:
J2 = uint8(zeros(size(J)));for rgb = 1:3 for i = 1:m for j = 1:n if round(fun(a1,[i,j]))>=1 && round(fun(a1,[i,j]))<=c && round(fun(a2,[i,j]))>=1 && round(fun(a2,[i,j]))<=d J2(i,j,rgb) = J1(round(fun(a1,[i,j])),round(fun(a2,[i,j])),rgb); end end endend
这样生成的图像像素分布不规则,会出现像素挤压、疏密不均的现象,因此最后还需对不规则图像进行灰度内插生成规则的栅格图像。
源码:
I = imread('sp.tif');J = imread('tm.tif');[m,n] = size(J);[o,p] = size(I);%cpselect(J,I);%xlswrite('data1.xls',fixedPoints);%xlswrite('data2.xls',movingPoints);%%重采样J1 = J(1:m/o:end,1:n/p:end,1:3);[c,d,q]= size(J1);fixedPoints = xlsread('data1.xls');movingPoints = xlsread('data2.xls');x0 = fixedPoints(:,1);y0 = fixedPoints(:,2);x1 = movingPoints(:,1);y1 = movingPoints(:,2);data = [x1,y1];a = [1 1 1 1 1 1];a1 = lsqcurvefit('fun',a,data,x0);a2 = lsqcurvefit('fun',a,data,y0);%多项式几何校正J2 = uint8(zeros(size(J)));for rgb = 1:3 for i = 1:m for j = 1:n if round(fun(a1,[i,j]))>=1 && round(fun(a1,[i,j]))<=c && round(fun(a2,[i,j]))>=1 && round(fun(a2,[i,j]))<=d J2(i,j,rgb) = J1(round(fun(a1,[i,j])),round(fun(a2,[i,j])),rgb); end % J2(round(fun(a1,[i,j])),round(fun(a2,[i,j]))) = J(i,j); % end end endend[x,y] = size(J2);%根据数据游标取截取区域的左上方点J3 = imcrop(I,[98 180 60*o/x 60*p/y]);J4 = imcrop(J2,[41 80 60 60]);[k,y,z] = size(J3);[h,t,e] = size(J4);%%重采样%双线性内插法u = h/k;v = t/y;itp = uint8(zeros(k,y,3));for rgb = 1:3 for i = ceil(1/u):k-1 iu = floor(i*u); for j = ceil(1/v):y-1 jv = floor(j*v); itp(i,j,rgb) = (1-(i*u-iu))*(1-(j*v-jv))*J4(iu,jv,rgb)... +(1-(i*u-iu))*(j*v-jv)*J4(iu,jv+1,rgb)... +(i*u-iu)*(1-(j*v-jv))*J4(iu+1,jv,rgb)... +(i*u-iu)*(j*v-jv)*J4(iu+1,jv+1,rgb); end endend%去黑边for rgb = 1:3 for i = 1:3 for j = 1:175 itp(i,j,rgb) = J4(ceil(i*u),ceil(j*v),rgb); itp(145,j,rgb) = J4(43,ceil(j*v),rgb); end end for j = 1:2 for i = 1:145 itp(i,j,rgb) = J4(ceil(i*u),ceil(j*v),rgb); itp(i,175,rgb) = J4(ceil(i*u),61,rgb); end endendimwrite(J3,'Core1.bmp','bmp');imwrite(itp,'Core2.bmp','bmp');subplot(231),imshow(J),title('待配准图像');subplot(232),imshow(I),title('基准图像');subplot(233),imshow(J2),title('多项式几何校正后'); subplot(234),imshow(J3),title('基准影像裁剪后');subplot(235),imshow(itp),title('校正影像裁剪重采样后');% %基准图重采样% kh = zuixiaogongbeishu(k,h);% yt = zuixiaogongbeishu(y,t);% u = h/kh;v = t/yt;% J5 = J3(1:k/kh:end,1:y/yt:end,1:3);% %配准图 双线性内插法重采样% itp = uint8(zeros(kh,yt,3));% for rgb = 1:3% for i = floor(1/u):kh-1% iu = floor(i*u);% for j = floor(1/v):yt-1 % jv = floor(j*v);% itp(i,j,rgb) = (1-(i*u-iu))*(1-(j*v-jv))*J4(iu,jv,rgb)...% +(1-(i*u-iu))*(j*v-jv)*J4(iu,jv+1,rgb)...% +(i*u-iu)*(1-(j*v-jv))*J4(iu+1,jv,rgb)...% +(i*u-iu)*(j*v-jv)*J4(iu+1,jv+1,rgb);% end% end% end% %去黑边% for rgb = 1:3% for i = 1:144 % for j = 1:10675% itp(i,j,rgb) = J4(ceil(i*u),ceil(j*v),rgb);% end% end% for j = 1:175% for i = 1:6235% itp(i,j,rgb) = J4(ceil(i*u),ceil(j*v),rgb);% end% end% end% % itp1 = uint8(zeros(6193,10615,3));% itp1(1:6193,1:10615,1:3) = itp(1:6193,1:10615,1:3);% imwrite(J5,'Crop1.bmp','bmp');% J5 = imread('Crop1.bmp');% imwrite(itp1,'Crop2.bmp','bmp');% J6 = imread('Crop2.bmp');% subplot(231),imshow(J),title('待配准图像');% subplot(232),imshow(I),title('基准图像');% subplot(233),imshow(J2),title('多项式几何校正后'); % subplot(234),imshow(J5),title('基准影像裁剪重采样后');% subplot(235),imshow(J6),title('校正影像裁剪重采样后');% a = imread('基准.bmp');% b = imread('重采样后图像.bmp');% c = imcrop(a,[1,100,100,100]);% d = imcrop(b,[1,100,100,100]);% imwrite(c,'Core3.bmp','bmp');% imwrite(d,'Core4.bmp','bmp');