# others - 在MATLAB中，如何实现鱼眼镜头效果？

## 例子

``````
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

``````

``````
rgbImage = ind2rgb(X,map); %# Convert to an RGB image
[r,c,d] = size(rgbImage); %# Get the image dimensions

``````

``````
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

``````

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

``````
[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])

``````