Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hmm... I'm not 100% percent on this stuff, but it was an interesting question and relevant to my work, so I thought I'd play around and give it a shot. <br></p> <p><strong>EDIT</strong>: I tried this once using no built-ins. That was my original answer. Then I realized that you could do it your way pretty easily:</p> <p>The easy answer to your question is to use the correct rotation matrix about the <em>z-axis</em>: </p> <pre><code>R = [cos(theta) -sin(theta) 0; sin(theta) cos(theta) 0; 0 0 1]; </code></pre> <p><strong>Here's another way to do it (my original answer):</strong></p> <p>I'm going to share what I did; hopefully this is useful to you. I only did it in 2D (though that should be easy to expand to 3D). Note that if you want to rotate the image in plane, you will need to use a different rotation matrix that you have currently coded. You need to rotate about the <em>Z-axis</em>. <br> I did not use those matlab built-ins.</p> <p>I referred to <a href="http://en.wikipedia.org/wiki/Rotation_matrix" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Rotation_matrix</a> for some info.</p> <pre><code>im = double(imread('cameraman.tif')); % must be double for interpn [x y] = ndgrid(1:size(im,1), 1:size(im,2)); rotation = 10; theta = rotation*pi/180; % calculate rotation matrix R = [ cos(theta) -sin(theta); sin(theta) cos(theta)]; % just 2D case % calculate new positions of image indicies tmp = R*[x(:)' ; y(:)']; % 2 by numel(im) xi = reshape(tmp(1,:),size(x)); % new x-indicies yi = reshape(tmp(2,:),size(y)); % new y-indicies imrot = interpn(x,y,im,xi,yi); % interpolate from old-&gt;new indicies imagesc(imrot); </code></pre> <p><img src="https://i.stack.imgur.com/1WTpT.png" alt="rotation image"></p> <p>My own question now is: "How do you change the origin about which you are rotating the image? Clearly, I'm rotating about (0,0), the top left corner. </p> <p><strong>EDIT 2</strong> In response to the asker's comment, I've tried again. <br> This time I fixed a couple of things. Now I'm using the same transformation matrix (about x) as in the original question. <br> I rotated about the center of the image by redoing the way i do the <code>ndgrid</code>s (put 0,0,0) in the center of the image. I also decided to show 3 planes of the image. This was not in the original question. The middle plane is the plane of interest. To get just the middle plane, you can leave out the zero-padding and redefine the 3rd <code>ndgrid</code> option to be just <code>1</code> instead of <code>-1:1</code>.</p> <pre><code>im = double(imread('cameraman.tif')); % must be double for interpn im = padarray(im, [0 0 1],'both'); [x y z] = ndgrid(-floor(size(im,1)/2):floor(size(im,1)/2)-1, ... -floor(size(im,2)/2):floor(size(im,2)/2)-1,... -1:1); rotation = 1; theta = rotation*pi/180; % calculate rotation matrix R = [ 1 0 0 ; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)]; % calculate new positions of image indicies tmp = R*[x(:)'; y(:)'; z(:)']; % 2 by numel(im) xi = reshape(tmp(1,:),size(x)); % new x-indicies yi = reshape(tmp(2,:),size(y)); % new y-indicies zi = reshape(tmp(3,:),size(z)); imrot = interpn(x,y,z,im,xi,yi,zi); % interpolate from old-&gt;new indicies figure; subplot(3,1,1);imagesc(imrot(:,:,1)); axis image; axis off; subplot(3,1,2);imagesc(imrot(:,:,2)); axis image; axis off; subplot(3,1,3);imagesc(imrot(:,:,3)); axis image; axis off; </code></pre> <p><img src="https://i.stack.imgur.com/Mmpci.png" alt="new version"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload