我想翘曲基于具有不同取向的给定的直线的图像的图像:翘曲基于具有不同取向的给定线
- 对于输入图像上的每个线,我可以得到像素的坐标在线
- 然后我会映射这些像素到扭曲的图像,使每一行现在是一列。
我会用interp2
因为我已经有X
,Y
和Z
,我会做Xi
和Yi
为每个行所获得的坐标。但是,您将如何:
- 避免制作
for
循环哪个索引遍布行数? - 确保输出扭曲图像中的每一列的大小相同?
- 用径向线对输入图像进行采样?
- 我认为最难的问题是,即使我使用不同的方向,你将如何保持邻域结构:例如,输入图像左侧的horiontal线(行)和右侧部分的径向线?
这里的输入图像是一个轮胎,有这种很好的圆圈图案。我可以从轮胎中心做出径向线,我想要得到一个新的图像,其列是来自径向线的像素。
这是我到目前为止的代码(没有interp2
,因为我还没有解决上述问题)。
close all
%% Image
Z = imread('tire.tif');
% The corresponding mesh
sz = size(Z);
[X,Y] = meshgrid(1:sz(2), 1:sz(1));
%% Line
lineEquation = @(c, v) (v(1)*(X-c(2))+v(2)*(Y-c(1)))/norm(v);
getLine = @(c, v) abs(lineEquation(c, v))<1/2;
% Example
c = [93, 109];
v = [2, 1];
line = getLine(c, v);
%% Circle
circleEquation = @(c, r) ((X-c(2)).^2+(Y-c(1)).^2-r^2)/r^2;
getCircle = @(c, r) abs(circleEquation(c, r))<1/r;
% Example
r = 24;
circle = getCircle(c, r);
%% Plot a sequence of line
figure;
for delta = -1:0.1:1
v = [0.1, delta];
line_delta = getLine(c, v);
Z_line = Z;
Z_line(line_delta) = 255;
imagesc(Z_line);
colormap('gray');
pause(0.05);
end
%% Plot examples
figure;
subplot(221);
imagesc(Z);
title('Image');
subplot(222);
Z_line = Z;
Z_line(line) = 255;
imagesc(Z_line);
title('Line');
subplot(223);
Z_circle = Z;
Z_circle(circle) = 255;
imagesc(Z_circle);
title('Circle');
subplot(224);
% TODO
title('Warped image');
colormap('gray');
这里有不同的输出:
这里是扭曲图像:
这里是答案代码:
[ANG, RAD] = meshgrid(0:0.01:2*pi, 0:0.5:166);
XI = c(2) + cos(ANG).*RAD;
YI = c(1) + sin(ANG).*RAD;
WARPED = interp2(X, Y, double(Z), XI, YI);
WARPED(isnan(WARPED))= max(WARPED(:));
imagesc(WARPED);
title('Warped image');
colormap('gray');
+1 for silly gifs :) – 2011-05-30 21:41:03