以下是對圖像應用仿射變換的簡單實現。一些矩陣可能會被顛倒,因爲我是從記憶中完成的。我不知道你是如何格式化你的pts數組,所以我認爲一個可行的例子是我能做的最好的。 interp2
函數應用雙線性插值,bilinear
函數執行將模擬濾波器描述爲數字濾波器的雙線性變換。這不是你想要的。
P.S.在應用圖像變形時(即,爲輸出圖像中的每個點定義要在輸入圖像中採樣的點)時,必須確保使用逆變換。如果您執行正向變換(即,定義輸出圖像中每個點映射到的輸出圖像中的點),那麼最終會出現嚴重的混疊效果並可能會在輸出圖像中出現空洞。
希望這會有所幫助。如果您有任何問題,請告訴我。
img = double(imread('rice.png'))/255;
theta = 30; % rotate 30 degrees
R = [cosd(theta) -sind(theta) 0; ...
sind(theta) cosd(theta) 0; ...
0 0 1];
sx = 15; % skew by 15 degrees in x
Skx = [1 tand(sx) 0; ...
0 1 0; ...
0 0 1];
% Translate by 1/2 size of image
tx = -size(img, 2)/2;
ty = -size(img, 1)/2;
T = [1 0 tx; ...
0 1 ty; ...
0 0 1];
% Scale image down by 1/2
sx = 0.5;
sy = 0.5;
S = [sx 0 0; ...
0 sy 0; ...
0 0 1];
% translate, scale, rotate, skew, then translate back
A = inv(T)*Skx*R*S*T;
% create meshgrid points
[x, y] = meshgrid(1:size(img,2), 1:size(img,1));
% reshape so we can apply matrix op
V = [reshape(x, 1, []); reshape(y, 1, []); ones(1, numel(x))];
Vq = inv(A)*V;
% probably not necessary for these transformations but project back to the z=1 plane
Vq(1,:) = Vq(1,:) ./ V(3,:);
Vq(2,:) = Vq(2,:) ./ V(3,:);
% reshape back into a meshgrid
xq = reshape(Vq(1,:), size(img));
yq = reshape(Vq(2,:), size(img));
% use interp2 to perform bilinear interpolation
imgnew = interp2(x, y, img, xq, yq);
% show the resulting image
imshow(imgnew);
雙線性變換是一種將模擬轉換爲數字表示(拉普拉斯變換爲z變換)的方法。我想你想用'interp2' – jodag
我會appriciate根據我的上下文的例子。 –
並且這裏雙線性插值被用於變換圖像中的填孔。 –