待校正图像与参考图像之间的仿射变换关系可以用下式表示:
其中(x,y)表示待校正图像像素点位置坐标,(X,Y)表示参考图像像素点位置坐标。
几何校正函数代码:
function correctedImage = GeometricCorrection(originalImage_path,referenceImage_path)%几何校正originalImage = im2double(imread(originalImage_path));referenceImage = im2double(imread(referenceImage_path));figure('Name','请选择3处对应点'),subplot(1,2,1);imshow(originalImage);title('原始图像');subplot(1,2,2);imshow(referenceImage);title('参考图像');[x,y] = ginput(6);close all;x1 = x(1);X1 = x(2);x2 = x(3);X2 = x(4);x3 = x(5);X3 = x(6);y1 = y(1);Y1 = y(2);y2 = y(3);Y2 = y(4);y3 = y(5);Y3 = y(6);T = [1,1,1;X1,X2,X3;Y1,Y2,Y3];a = [x1,x2,x3]/T;b = [y1,y2,y3]/T;a0 = a(1);a1 = a(2);a2 = a(3);b0 = b(1);b1 = b(2);b2 = b(3);[rows_ref,cols_ref,channel_ref] = size(referenceImage);[rows_ori,cols_ori,~] = size(originalImage);correctedImage = zeros([rows_ref,cols_ref,channel_ref]);for channel = 1 : channel_reffor row = 1 : rows_reffor col = 1 : cols_reftempx = a0+a1*col+a2*row;tempy = b0+b1*col+b2*row;if tempx>=1 && tempx<=cols_ori && tempy>=1 && tempy<=rows_oricorrectedImage(row,col,channel) = (tempx-floor(tempx))*(tempy-floor(tempy))*originalImage(ceil(tempy),ceil(tempx),channel)+(tempx-floor(tempx))*(ceil(tempy)-tempy)*originalImage(floor(tempy),ceil(tempx),channel)+(ceil(tempx)-tempx)*(tempy-floor(tempy))*originalImage(ceil(tempy),floor(tempx),channel)+(ceil(tempx)-tempx)*(ceil(tempy)-tempy)*originalImage(floor(tempy),floor(tempx),channel);endendendendend
演示代码:
%% 几何校正originalImage_path = '...';referenceImage_path = '...';correctedImage = GeometricCorrection(originalImage_path,referenceImage_path);imshow(correctedImage);
运行步骤:
运行程序后会出现以下窗口,在左图中选取一点,然后在右图中选择对应点,选完三组对应点即可。
几何校正结果如下图所示:
如果觉得《Matlab实现图像简单的几何校正》对你有帮助,请点赞、收藏,并留下你的观点哦!