如何在 MATLAB 中实现鱼眼镜头效果(桶形变换)? [英] How can I implement a fisheye lens effect (barrel transformation) in MATLAB?
问题描述
如何实现该图像中所示的鱼眼镜头效果:
可以尝试使用 Google 的徽标:
顺便说一句,它的术语是什么?
我相信这通常被称为鱼眼镜头"效果或桶形变换".以下是我找到的两个演示链接:
您可以通过更改
options
数组中的第三个值来调整失真程度,该值是用于图像点径向变形的指数幂.How can one implement the fisheye lens effect illustrated in that image:
One can use Google's logo for a try:
BTW, what's the term for it?
解决方案I believe this is typically referred to as either a "fisheye lens" effect or a "barrel transformation". Here are two links to demos that I found:
Sample code for how you can apply fisheye distortions to images using the
'custom'
option for the functionmaketform
from the Image Processing Toolbox.An image processing demo which performs a barrel transformation using the function
tformarray
.
Example
In this example, I started with the function
radial.m
from the first link above and modified the way it relates points between the input and output spaces to create a nice circular image. The new functionfisheye_inverse
is given below, and it should be placed in a folder on your MATLAB path so you can use it later in this example: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
The fisheye distortion looks best when applied to square images, so you will want to make your images square by either cropping them or padding them with some color. Since the transformation of the image will not look right for indexed images, you will also want to convert any indexed images to RGB images using
ind2rgb
. Grayscale or binary images will also work fine. Here's how to do this for your sample 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
Now we can create the transform with
maketform
and apply it withimtransform
(orimwarp
as recommended in newer versions):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
And here's the image you should see:
You can adjust the degree of distortion by changing the third value in the
options
array, which is the exponential power used in the radial deformation of the image points.这篇关于如何在 MATLAB 中实现鱼眼镜头效果(桶形变换)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!