-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloor_round
More file actions
76 lines (61 loc) · 1.91 KB
/
Copy pathfloor_round
File metadata and controls
76 lines (61 loc) · 1.91 KB
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
69
70
71
72
73
74
75
clear;
% floor
% Set rotation angle (in radians)
theta = 0.1 * pi / 2;
% Load image
colorimage = imread('test1.jpg');
% Convert to double precision and split RGB channels
M = im2double(colorimage);
MR = M(:,:,1); % Red
MG = M(:,:,2); % Green
MB = M(:,:,3); % Blue
% Get image size
rows = size(MR,1);
cols = size(MR,2);
% Define rotation matrix and its inverse
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
Rinv = R^(-1);
% Pad image to avoid cropping after rotation
padsize = 100;
MR_pad = padarray(MR, [padsize, padsize], 0, 'both');
MG_pad = padarray(MG, [padsize, padsize], 0, 'both');
MB_pad = padarray(MB, [padsize, padsize], 0, 'both');
[rows_pad, cols_pad] = size(MR_pad);
cx = cols_pad / 2;
cy = rows_pad / 2;
% Create new (blank) matrices for rotated image
MR_new = zeros(size(MR_pad));
MG_new = zeros(size(MG_pad));
MB_new = zeros(size(MB_pad));
% Basic rotation using hard rounding (push method)
for x = 1:cols_pad
for y = 1:rows_pad
% Shift origin to center
x_shift = x - cx;
y_shift = y - cy;
% Apply rotation
coord_new = R * [x_shift; y_shift];
% Shift back
x_new = floor(coord_new(1) + cx);
y_new = floor(coord_new(2) + cy);
% Check if new location is within bounds
if x_new > 0 && x_new <= cols_pad && y_new > 0 && y_new <= rows_pad
MR_new(y_new, x_new) = MR_pad(y, x);
MG_new(y_new, x_new) = MG_pad(y, x);
MB_new(y_new, x_new) = MB_pad(y, x);
end
end
end
% Combine the rotated channels back into an image
Mrotate(:,:,1) = MR_new;
Mrotate(:,:,2) = MG_new;
Mrotate(:,:,3) = MB_new;
% Display and save the rotated image
figure;
imshow(Mrotate);
title('Rotated Image (Hard Rounding)');
imwrite(Mrotate, 'RotatedImage.jpg');
%Show original vs rotated side by side
figure;
subplot(1,2,1), imshow(colorimage), title('Original');
subplot(1,2,2), imshow(Mrotate), title('Rotated');