Rotate image over X, Y and Z axis in Matlab -
i'm trying rotate image on 3 axis in matlab. used function rotx, roty, rotz create rotation matrix don't know how use. these methods considering center point origin?
because image 2d projection 3d world, 1 of 3d planes needs set 0 if want work. i'm going assume z
-plane equal 0, , image laying flat on z = 0
plane.
basically... this, using cameraman.tif
image. looks like:
here's looks in 3d. first, load in image, generate (x,y)
pairs each intensity in image. done meshgrid
. once this, use scatter
plot each point assuming z = 0
, make colour of each point actual intensity seen @ image coordinate. however, see in 2d perspective scatter
naturally 2d. make 3d, change camera view you're looking @ image matlab's default 3d view, add things labels , grid reversing y
coordinate y
coordinates images positive going downwards:
%// load in image im = imread('cameraman.tif'); %// generate coordinates , unravel single vector [x,y] = meshgrid(1:size(im,2), 1:size(im,1)); x_coord = x(:); y_coord = y(:); %// plot image scatter plot scatter(x_coord, y_coord, 2, repmat(double(im(:)), 1, 3)/255); view(3); grid; xlabel('columns'); ylabel('rows'); axis ij
we this:
however, grayscale images. if want same in colour, need change way you're specifying colour of each point. that's done extracting out red, green , blue planes , putting them single column in colour matrix fourth argument. specifically, scatter
command now:
red = reshape(im(:,:,1), [], 1); green = reshape(im(:,:,2), [], 1); blue = reshape(im(:,:,3), [], 1); scatter(x_coord, y_coord, 2, double([red green blue])/255);
so have this, need take x
, y
coordinates generated in previous step , rotate each of them rotation matrix. bear in mind z
coordinate zeroes.
there rotation matrix defined respect each axis want rotate points with. rotation matrix each axis seen below:
source: wikipedia
as such, it's matter of taking unraveled coordinates above, applying them rotation matrix using these new coordinates , plotting points. bear in mind image coordinates, y
axis positive going downwards, rotating in clockwise direction positive angles , rotating counter-clockwise negative angles.
to rotate 3d point assuming origin @ (x,y,z) = (0,0,0)
(which our case well), matrix multiplication:
pout = r*p;
p
3 x 1 vector of (x,y,z)
points , pout
output vector (also 3 x 1) rotated. therefore, if want of our points, you'd have make p
3 x n
matrix n
total number of pixels in image, apply r*p
, use resulting points input scatter
.
we can show happens when rotate each axis independently.
x
-axis
first create rotation matrix x
axis given rotation angle theta
:
theta = pi/3; %// 60 degree rotation example rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
now you're done, rotate points:
pout = rx*[x_coord.'; y_coord.'; zeros(1,numel(x_coord))];
once you're done, call scatter
these new points. i'll instead call scatter3
designed 3d points , i'll need rotate camera see things properly:
scatter3(pout(1,:), pout(2,:), pout(3,:), 2, repmat(double(im(:)), 1, 3)/255); axis ij; xlabel('columns'); ylabel('rows'); view(-105, 35);
if have colour images, make sure change fourth argument talked @ beginning of post.
we this:
the x
axis here columns, , rotating similar imagining book laying flat on z = 0
plane , opening page book. spine of book x
axis, or columns.
y
-axis
you same thing different rotation matrix. however, give better perspective of rotating way, i'll need change camera angle slightly:
theta = pi/3; %// 60 degree rotation example ry = [cos(theta) 0 sin(theta); 0 1 0; -sin(theta) 0 cos(theta)]; pout = ry*[x_coord.'; y_coord.'; zeros(1,numel(x_coord))]; scatter3(pout(1,:), pout(2,:), pout(3,:), 2, repmat(double(im(:)), 1, 3)/255); axis ij; xlabel('columns'); ylabel('rows'); view(5,30); %// change camera angle you're looking @ x plane better
this get:
same effect, axis of rotation has changed. spine of book respect rows, not columns.
z
-axis
this 1 pretty benign. should have effect of rotating piece of paper on table no elevation:
theta = pi/3; %// 60 degree rotation example rz = [cos(theta) -sin(theta) 0; sin(theta) cos(theta) 0; 0 0 1]; pout = rz*[x_coord.'; y_coord.'; zeros(1,numel(x_coord))]; scatter3(pout(1,:), pout(2,:), pout(3,:), 2, repmat(double(im(:)), 1, 3)/255); axis ij; xlabel('columns'); ylabel('rows');
.... and:
hopefully more enough started. luck!
Comments
Post a Comment