如何在MATLAB中实现鱼眼镜头效果(桶形变换)?

时间:2010-04-07 03:37:57

标签: matlab image-processing geometry

如何实现该图像中所示的鱼眼镜头效果:

fisheye example

可以尝试使用Google的徽标:

alt text

顺便说一下,它的用语是什么?

3 个答案:

答案 0 :(得分:13)

我认为这通常被称为“鱼眼镜头”效果或“桶形变换”。以下是我发现的两个演示链接:

实施例

在这个例子中,我从first link above开始使用函数radial.m,并修改了它与输入和输出空间之间的点相关的方式,以创建一个漂亮的圆形图像。新功能fisheye_inverse如下所示,应放在MATLAB path的文件夹中,以便稍后在此示例中使用它:

function U = fisheye_inverse(X, T)

  imageSize = T.tdata(1:2);
  exponent = T.tdata(3);
  origin = (imageSize+1)./2;
  scale = imageSize./2;

  x = (X(:, 1)-origin(1))/scale(1);
  y = (X(:, 2)-origin(2))/scale(2);
  R = sqrt(x.^2+y.^2);
  theta = atan2(y, x);

  cornerScale = min(abs(1./sin(theta)), abs(1./cos(theta)));
  cornerScale(R < 1) = 1;
  R = cornerScale.*R.^exponent;

  x = scale(1).*R.*cos(theta)+origin(1);
  y = scale(2).*R.*sin(theta)+origin(2);
  U = [x y];

end

当应用于方形图像时,鱼眼失真看起来最佳,因此您需要通过裁剪或用某种颜色填充图像来使图像成为方形。由于图片的转换看起来不适合indexed images,因此您还需要使用RGB images将任何索引图像转换为ind2rgbGrayscalebinary images也可以正常使用。以下是为您的示例Google logo执行此操作的方法:

[X, map] = imread('logo1w.png');  % Read the indexed image
rgbImage = ind2rgb(X, map);       % Convert to an RGB image
[r, c, d] = size(rgbImage);       % Get the image dimensions
nPad = (c-r)/2;                   % The number of padding rows
rgbImage = cat(1, ones(nPad, c, 3), rgbImage, ones(nPad, c, 3));  % Pad with white

现在我们可以使用maketform创建转换,并将其应用于imtransform(或imwarp,如新版本中所推荐的那样):

options = [c c 3];  % An array containing the columns, rows, and exponent
tf = maketform('custom', 2, 2, [], ...  % Make the transformation structure
               @fisheye_inverse, options);
newImage = imtransform(rgbImage, tf);   % Transform the image
imshow(newImage);                       % Display the image

这是你应该看到的图像:

enter image description here

您可以通过更改options数组中的第三个值来调整失真度,这是图像点径向变形中使用的指数幂。

答案 1 :(得分:1)

我认为你指的是fisheye镜头效果。 Here是在matlab中模仿鱼眼的一些代码。

答案 2 :(得分:1)

仅供记录:

这种效应是一种称为“桶形失真”的径向失真。

有关详细信息,请参阅:

http://en.wikipedia.org/wiki/Distortion_(optics)

这是使用纹理贴图(改编自MATLAB Documentation)应用类似于桶形失真的效果的另一种方法:

[I,map] = imread('logo.gif');
[h,w] = size(I);

sphere; 

hS = findobj('Type','surface');

hemisphere = [ones(h,w),I,ones(h,w)];

set(hS,'CData',flipud(hemisphere),...
    'FaceColor','texturemap',...
    'EdgeColor','none')

colormap(map)
colordef black
axis equal
grid off
set(gca,'xtick',[],'ztick',[],'ytick',[],'box','on')
view([90 0])

这将为您提供您正在寻找的圆形框架,但是锯齿工件可能太多而无法处理。