forked from jakobkarlstrand/tnm034-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate_image.asv
68 lines (56 loc) · 2.28 KB
/
rotate_image.asv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
%%The function takes the current image and the location of the eyes and
%%mouth as arguments. It uses Hough transform to rotate the image so that
%%the eyes are horizontal.
function [currentImage_rotated, lEye_rotated, rEye_rotated, mouth_rotated] = rotate_image(currentImage, lEye, rEye, mouth)
%%Create a figure with the line connecting the eyes.
figure;
size_img = size(currentImage); %%get image size
lineImage = zeros(size_img(1), size_img(2));
imshow(lineImage);
hold on
plot([lEye(1),rEye(1)],[lEye(2),rEye(2)],'Color','w','LineWidth',2)
hold off
title('Line to rotate');
set(gca,'XColor','none'); %%Remove borders for the image.
F = getframe(gca); %%Take a snapshot of the image with the plot inside it.
[temp, Map] = frame2im(F); %%Save the snapshot in temp.
temp = im2gray(temp); %%convert to grayscale for Hough transform.
% figure;
% imshow(temp);
% title('Snapshot of image');
%%Hough transform.
[H, teta, ro] = hough(temp, "Rhoresolution", 5, "Theta", -90:0.5:89.5);
[r, t] = find(H == max(H(:)));
teta = teta(t);
%%Determine if the rotation should be clockwise or counter clockwise
sign = 0;
if teta > 0
sign = 1;
elseif teta < 0
sign = -1;
end
rot_angle = teta - 90*sign; % Angle of rotation.
figure;
subplot(1,2,1);
lineImage_rotated = imrotate(temp, rot_angle, "bicubic", "crop");
imshow(lineImage_rotated);
title('Rotated line');
%%Rotate current image.
currentImage_rotated = imrotate(currentImage, rot_angle, "bicubic", "crop");
%%TODO
%%Apply rotation to eye and mouth location.
rotationMatrix = [cosd(alpha) sind(alpha); -sind(alpha) cosd(alpha)];
centerOriginalImage = (size(currentImage(:,:,1))/2)'; %%Center of original image.
centerRotatedImage = (size(currentImage_rotated(:,:,1))/2)'; %%Center of rotated image
lEye_rotated = rotationMatrix * (lEye - centerOriginalImage)*centerRotatedImage;
rEye_rotated = rotationMatrix * (rEye - centerOriginalImage)*centerRotatedImage;
mouth_rotated= rotationMatrix * (mouth - centerOriginalImage)*centerRotatedImage;
subplot(1,2,2);
imshow(currentImage_rotated);
title('Rotated Image');
hold on
plot([lEye_rotated(1),rEye(1)],[lEye(2),rEye(2)],'Color','r','LineWidth',2)
plot([mouth(1),rEye(1)],[mouth(2),rEye(2)],'Color','r','LineWidth',2)
plot([mouth(1),lEye(1)],[mouth(2),lEye(2)],'Color','r','LineWidth',2)
hold off
end