0

I have images of faces in different positions. I want to rotate them, to make the line connecting the eyes always be horizontal. But I don't know how to do this in MATLAB.

And how can I calculate the angle of rotation?

Descriptive drawing of problem:

enter image description here

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
Shudy
  • 7,806
  • 19
  • 63
  • 98
  • Automated? Or do you have only a few cases and you can just click on the eyes? – Rody Oldenhuis Mar 11 '14 at 10:08
  • Do you have the image processing toolbox? – Rody Oldenhuis Mar 11 '14 at 10:08
  • I have around.. 200 images. I do the work of manually click on the eyes of all the images, and save this info in a structure. So now, with this info of the position of the eyes, I have to rotate the images, to be all in horizontal. Yes I have processing toolbox ^^ – Shudy Mar 11 '14 at 10:12

2 Answers2

3

If you already have the locations of the eyes, then it's easy :) Here's an outline:

%//    left eye - right eye
pos = [  30          90    %// X
         80          40];  %// Y 

%// The angle equals the arctangent of dy/dx
angle = atan2(diff(pos(2,:)), diff(pos(1,:)));

%// Rotate in the opposite direction 
img = imrotate(img, -angle);
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
1

Since you seem to have the Image Processing Toolbox, you can also look into the built-in landmark-based registration functions (especially if your transform is not limited to pure rotation), in particular cpselect with a syntax like:

cpselect(moving,fixed)

And then use fitgeotrans to construct the geometrical transform and imwarp to warp the moving image.

Cape Code
  • 3,584
  • 3
  • 24
  • 45