📄 rotateimage.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><head> <title>Description of RotateImage</title> <meta name="keywords" content="RotateImage"> <meta name="description" content="RotateImage - Rotates an image by X degrees"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="generator" content="m2html © 2003 Guillaume Flandin"> <meta name="robots" content="index, follow"> <link type="text/css" rel="stylesheet" href="../m2html.css"></head><body><a name="_top"></a><div><a href="../index.html">Home</a> > <a href="index.html">mri_toolbox</a> > RotateImage.m</div><!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td><td align="right"><a href="index.html">Index for mri_toolbox <img alt=">" border="0" src="../right.png"></a></td></tr></table>--><h1>RotateImage</h1><h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>RotateImage - Rotates an image by X degrees</strong></div><h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>function[Image] = RotateImage(Image, degrees) </strong></div><h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre class="comment"> RotateImage - Rotates an image by X degrees
Image_Rotated = RotateImage(Image, degrees)
Rotates an image by the angle degrees in the
CCW direction. Degrees may be any number.
The function will put degrees in the range 0
to 360 degrees and then into a range of -45 to 45
degrees after performing elementary 90 degree rotations.
The rotation performed is the 3 Pass Separable rotation
using FFT-based methods to perform the skews. Aliased spectral
components are masked after EACH skew using the MaskImage function.
Normal Rotation
|x'| = | cos(a) -sin(a) ||x|
|y'| | sin(a) cos(a) ||y|
3 Pass Rotation
|x'| = | 1 -tan(a/2) || 1 0 || 1 -tan(a/2) ||x|
|y'| | 0 1 || sin(a) 1 || 0 1 ||y|
This function pads images during the rotation process. Images
can be non-square, but odd dimensions will cause incorrect padding
Therefore an error is returned, if a dimension is odd.
Dimensions need not be powers of 2 and will not necessarily be padded
to a power of 2. The Matlab fft functions are capable of non-power of 2
transforms.
Note: The results often display some Gibbs ringing.
Example:
load mri
img = double( squeeze(D(:,:,1,16)) );
img_rot = real( RotateImage(img,45) );
figure;
subplot(1,2,1); imagesc(img,[0 88]); axis image;
subplot(1,2,2); imagesc(img_rot,[0 88]); axis image;</pre></div><!-- crossreference --><h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>This function calls:<ul style="list-style-image:url(../matlabicon.gif)"></ul>This function is called by:<ul style="list-style-image:url(../matlabicon.gif)"></ul><!-- crossreference --><h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><ul style="list-style-image:url(../matlabicon.gif)"><li><a href="#_sub1" class="code">function[Image] = MaskImage(Image, a, b, c, d)</a></li></ul><h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function[Image] = RotateImage(Image, degrees)</a>0002 <span class="comment">% RotateImage - Rotates an image by X degrees</span>0003 <span class="comment">%</span>0004 <span class="comment">% Image_Rotated = RotateImage(Image, degrees)</span>0005 <span class="comment">%</span>0006 <span class="comment">% Rotates an image by the angle degrees in the</span>0007 <span class="comment">% CCW direction. Degrees may be any number.</span>0008 <span class="comment">% The function will put degrees in the range 0</span>0009 <span class="comment">% to 360 degrees and then into a range of -45 to 45</span>0010 <span class="comment">% degrees after performing elementary 90 degree rotations.</span>0011 <span class="comment">%</span>0012 <span class="comment">% The rotation performed is the 3 Pass Separable rotation</span>0013 <span class="comment">% using FFT-based methods to perform the skews. Aliased spectral</span>0014 <span class="comment">% components are masked after EACH skew using the MaskImage function.</span>0015 <span class="comment">%</span>0016 <span class="comment">% Normal Rotation</span>0017 <span class="comment">%</span>0018 <span class="comment">% |x'| = | cos(a) -sin(a) ||x|</span>0019 <span class="comment">% |y'| | sin(a) cos(a) ||y|</span>0020 <span class="comment">%</span>0021 <span class="comment">% 3 Pass Rotation</span>0022 <span class="comment">%</span>0023 <span class="comment">% |x'| = | 1 -tan(a/2) || 1 0 || 1 -tan(a/2) ||x|</span>0024 <span class="comment">% |y'| | 0 1 || sin(a) 1 || 0 1 ||y|</span>0025 <span class="comment">%</span>0026 <span class="comment">% This function pads images during the rotation process. Images</span>0027 <span class="comment">% can be non-square, but odd dimensions will cause incorrect padding</span>0028 <span class="comment">% Therefore an error is returned, if a dimension is odd.</span>0029 <span class="comment">%</span>0030 <span class="comment">% Dimensions need not be powers of 2 and will not necessarily be padded</span>0031 <span class="comment">% to a power of 2. The Matlab fft functions are capable of non-power of 2</span>0032 <span class="comment">% transforms.</span>0033 <span class="comment">%</span>0034 <span class="comment">% Note: The results often display some Gibbs ringing.</span>0035 <span class="comment">%</span>0036 <span class="comment">% Example:</span>0037 <span class="comment">%</span>0038 <span class="comment">% load mri</span>0039 <span class="comment">% img = double( squeeze(D(:,:,1,16)) );</span>0040 <span class="comment">% img_rot = real( RotateImage(img,45) );</span>0041 <span class="comment">% figure;</span>0042 <span class="comment">% subplot(1,2,1); imagesc(img,[0 88]); axis image;</span>0043 <span class="comment">% subplot(1,2,2); imagesc(img_rot,[0 88]); axis image;</span>0044 0045 <span class="comment">%</span>0046 <span class="comment">%************************************************************************</span>0047 <span class="comment">%* (c) Copyright 1998 *</span>0048 <span class="comment">%* Biomathematics Resource *</span>0049 <span class="comment">%* Mayo Foundation *</span>0050 <span class="comment">%************************************************************************</span>0051 <span class="comment">%</span>0052 <span class="comment">% 11/22/98 Implemented by Edward Brian Welch, edwardbrianwelch@yahoo.com</span>0053 <span class="comment">%</span>0054 0055 0056 <span class="comment">% NOTE:</span>0057 <span class="comment">% This function is partially vectorized (only single loops,</span>0058 <span class="comment">% no double loops) to gain some benefit in speed. The tradeoff</span>0059 <span class="comment">% is that more memory is required to hold the large matrices.</span>0060 <span class="comment">% It would be possible to completely vectorize certain parts</span>0061 <span class="comment">% of the mfile, but that would require large amounts of memory</span>0062 <span class="comment">% and would also make the code less readable.</span>0063 0064 [xdim ydim] = size(Image);0065 0066 <span class="keyword">if</span> mod(xdim,2)==1 | mod(ydim,2)==1,0067 error(<span class="string">'RotateImage() can only rotate images with even dimensions.'</span>);0068 <span class="keyword">end</span>0069 0070 <span class="comment">% Determine type of image to return</span>0071 <span class="keyword">if</span> isreal(Image),0072 real_flag = 1;0073 <span class="keyword">else</span>0074 real_flag = 0;0075 <span class="keyword">end</span>0076 0077 <span class="comment">% Put degrees into the range [0,360]</span>0078 <span class="keyword">while</span> degrees<0,0079 degrees = degrees + 360;0080 <span class="keyword">end</span>0081 0082 <span class="keyword">while</span> degrees>360,0083 degrees = degrees - 360;0084 <span class="keyword">end</span>0085 0086 <span class="comment">% Count number of elementary 90 degree rotations</span>0087 <span class="comment">% to put rotation into range [-45,45]</span>0088 count=0;0089 <span class="keyword">while</span> degrees>45,0090 degrees = degrees - 90;0091 count = count + 1;0092 <span class="keyword">end</span>0093 0094 Image = rot90(Image, count);0095 0096 <span class="comment">% Calculate Trigonometric Values</span>0097 <span class="comment">% Not Necessary in Matlab implementation to Negate degrees</span>0098 <span class="comment">% so that CCW rotations are positive</span>0099 theta = (degrees/360)*2*pi;0100 sin_theta = sin(theta);0101 negtan_thetadiv2 = -tan(theta/2);0102 0103 <span class="comment">%---------------------------</span>0104 <span class="comment">% FIRST SKEW</span>0105 <span class="comment">% |x1| = | 1 -tan(a/2) ||x|</span>0106 <span class="comment">% |y1| = | 0 1 ||y|</span>0107 <span class="comment">%---------------------------</span>0108 0109 <span class="comment">% Pad image rows to double the row size</span>0110 Image2 = zeros(2*xdim,ydim);0111 Image2( (xdim/2+1):(xdim/2+xdim),:)=Image;0112 0113 <span class="comment">% Forward FFT image rows</span>0114 Image2 = fft(Image2, 2*xdim, 1);0115 0116 <span class="comment">% Calculate image's center coordinates</span>0117 xno = (2*xdim-1)/2;0118 yno = (ydim-1)/2;0119 0120 <span class="comment">% Prepare to use the Fourier Shift Theorem</span>0121 <span class="comment">% f(x-deltax) <=> exp(-j*2*pi*deltax*k/N)*F(k)</span>0122 0123 <span class="comment">% Initialize constant part of the exponent</span>0124 <span class="comment">% expression. The skew is row dependent.</span>0125 cons1 = (-2.0*pi/(2*xdim))*negtan_thetadiv2;0126 0127 <span class="comment">% Calculate k values (Nyquist is at x=xno)</span>0128 k_array = zeros((2*xdim),1);0129 0130 <span class="keyword">for</span> x=1:(2*xdim), 0131 <span class="keyword">if</span> (x-1)<=xno,0132 k_array(x) = (x-1);0133 <span class="keyword">else</span>0134 k_array(x) = (x-1-2*xdim);0135 <span class="keyword">end</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -