## barrel_fix2.m ( File view )

• By 香榭丽舍 2014-06-11
• View(s)：23
• Download(s)：5
• Point(s)： 1
```
%%% ================================================================%%%
%%% first step:
%%% assume we have the inverse alias function,then we need to determine and
%%% establish the range of x and y of the original image from this function
%%% ================================================================%%%

%%% read alias image into current working memory space
origImg = imread('lena_barrel.tif','tif');
figure;
imshow(origImg,'truesize');
title('barrel image');

%%% get width and height of alias image
origWidth = size(origImg,1);
origHeight = size(origImg,2);

%%% calculate the middle coordinate of alias image
origMidOfX = round(origWidth/2);
origMidOfY = round(origHeight/2);

%%% shift the coordinate to the center of the alias image
[xOrigIndex,yOrigIndex] = meshgrid(1:origWidth,1:origHeight);
xNewIndex = xOrigIndex(:) - origMidOfX;
yNewIndex = yOrigIndex(:) - origMidOfY;

%%% transform rectangular to polar coordinate,note that theta is measured
%%% in radians,and please note that we can think Cartesian ordinate is just
%%% the rectangular coordinate,we can of course think it like this,anyway
[theta,radius] = cart2pol(xNewIndex,yNewIndex);

%%% a1 and a2 are image alias coeffients,so here we produce the aliasing image's
%%% radius in polar coordinates.
%%% note,inverse formula:newRadius = (a1 - sqrt(a1 ^ 2 - 4 * a2 * radius))/(2 * a2)
%%% is from formular:
%%% aliasRadius = a1 * radius - a2 * radius .^ 2
%%% which is the alias function I guessed,and it seems true.
a1 = 2.0;
a2 = 0.005;
newRadius = (a1 - sqrt(a1 ^ 2 - 4 * a2 * radius))/(2 * a2);
newTheta = theta;

%%% now we can map the aliasing rectangular coordinates to its polar
%%% counterpart
[xAliasIndex,yAliasIndex] = pol2cart(newTheta,newRadius);

%%% here calculate the min and max position
xMinAliasIndex = double(round(min(xAliasIndex)));
xMaxAliasIndex = double(round(max(xAliasIndex)));
yMinAliasIndex = double(round(min(yAliasIndex)));
yMaxAliasIndex = double(round(max(yAliasIndex)));

%%% calculate the to-be-fixed image's width and height according to the min and max position
newWidth = xMaxAliasIndex - xMinAliasIndex + 1;
newHeight = yMaxAliasIndex - yMinAliasIndex + 1;

%%% ================================================================%%%
%%% end first step
%%% ================================================================%%%

%%% ================================================================%%%
%%% second step:
%%% for each point in the to-be-fixed image,compute the corresponding position
%%% in the alias image through the inverse of the alias function,and
%%% then apply the bi-linear interpolate arithmetic to compute the
%%% gray-level of the to-be-fixed image of the corresponding position
%%% ================================================================%%%

%%% produce to-be-fixed image filled with black background,stress:uint8 type cast
%%% is so important that if ignored by carelessness you will get the strange
%%% result that will confuse you so much.
newImg = uint8(zeros(newWidth,newHeight));

%%% get the middle coordinates of the to-be-fixed image
xMidOfNewImg = round(newWidth/2);
yMidOfNewImg = round(newHeight/2);

%%% iterate each point of the to-be-fixed image to exert bilinear interpolate,a1
%%% and a2 are the coefficients of the transform equation or in other
%%% word,alias function's coefficients or auguments
a1 = 2.0;
a2 = 0.005;
for i = 1:newWidth
for j = 1:newHeight
xShiftOfNewImg = i - xMidOfNewImg;
yShiftOfNewImg = j - yMidOfNewImg;
[thetaOfNew,radiusOfNew] = cart2pol(xShiftOfNewImg,yShiftOfNewImg);
theta = thetaOfNew;
radius = a1 * radiusOfNew - a2 * radiusOfNew .^ 2;                                                  % alias function or transform equation
[xShiftOfSrcImg,yShiftOfSrcImg] = pol2cart(theta,radius);
xOfSrcImg = round(xShiftOfSrcImg + origMidOfX);
yOfSrcImg = round(yShiftOfSrcImg + orig
...
...
（Please download the complete source code to view）
```
...
Expand＞ ＜Close

## Want complete source code? Download it here

Point(s): 1

0 lines left, continue to read
Sponsored links

## File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
c.bmp75.05 kB31-07-13|20:04
barrel2.m5.78 kB11-06-14|11:32
barrel_fix2.m5.90 kB11-06-14|15:51
...
Sponsored links
• Sent successfully!
• 桶形畸变.rar
• 1 point

### barrel_fix2.m (4.36 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

### LOGIN

Don't have an account？ Register now
Need any help?
Mail to: support@codeforge.com

### 切换到中文版？

CodeForge Chinese Version
CodeForge English Version

### ^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!

### Warm tip!

Favorite by Ctrl+D