- You can try rotation using bilinear interpolation, where each pixel in the rotated image originates from in the original image using inverse rotation, to rotate an image by "deg" degrees without using the "imrotate" function. To smooth the transition between pixels and minimize artifacts, bilinear interpolation is used to estimate pixel values. Although this method requires a lot of processing, it offers a decent trade-off between speed and image quality for tiny rotations.
- To execute the translation, each pixel's position is adjusted based on the given offsets for the vertical (y_value) and horizontal (x_value). The image content is successfully moved in the desired direction using this method, leaving any parts that go outside of boundaries as zeros (or black in a grayscale image). For color images, you would extend this logic to handle each color channel separately.
How to rotate the image without using imrotate function.
30 ビュー (過去 30 日間)
古いコメントを表示
Hello all,
I want to rotate the image in steps of 2deg, I don't have image processing toolbox to use imrotate function, Please let me know how do I rotate the image and also I need to translate the image in the axis.
1. image_rotate(handles.image,deg);
2. image_translate(handles.image,x_value,y_value);
Best Regards, Raghavendra.
0 件のコメント
回答 (1 件)
Vidhi Agarwal
2024 年 9 月 17 日
Hi @Raghavendra
I understand you are trying to rotate the image without using “imrotate” function and also need help to translate the image in the axis.
You can perform these steps using basic matrix operations.
Below is the code for rotation function, translation function and their usage.
Given below is function for rotation will look like:
function rotated_image = image_rotate(image, deg)
% Convert degrees to radians
theta = deg2rad(deg);
% Get the size of the image
[rows, cols, num_channels] = size(image);
% Calculate the center of the image
cx = (cols + 1) / 2;
cy = (rows + 1) / 2;
% Create an empty matrix for the rotated image
rotated_image = zeros(size(image), 'like', image);
% Loop through each pixel in the output image
for x = 1:cols
for y = 1:rows
% Translate coordinates to origin
x0 = x - cx;
y0 = y - cy;
% Apply the inverse rotation matrix
x_rot = cos(theta) * x0 + sin(theta) * y0 + cx;
y_rot = -sin(theta) * x0 + cos(theta) * y0 + cy;
% Check if the rotated coordinates are within bounds
if x_rot >= 1 && x_rot <= cols && y_rot >= 1 && y_rot <= rows
% Get the four surrounding pixels
x1 = floor(x_rot);
x2 = ceil(x_rot);
y1 = floor(y_rot);
y2 = ceil(y_rot);
% Calculate the weights for interpolation
a = x_rot - x1;
b = y_rot - y1;
% Ensure indices are within bounds
x1 = max(1, min(cols, x1));
x2 = max(1, min(cols, x2));
y1 = max(1, min(rows, y1));
y2 = max(1, min(rows, y2));
for c = 1:num_channels
% Interpolate the pixel value
rotated_image(y, x, c) = (1 - a) * (1 - b) * double(image(y1, x1, c)) + ...
a * (1 - b) * double(image(y1, x2, c)) + ...
(1 - a) * b * double(image(y2, x1, c)) + ...
a * b * double(image(y2, x2, c));
end
end
end
end
end
Given below is function for translation will look like:
function translated_image = image_translate(image, x_value, y_value)
% Get the size of the image
[rows, cols, num_channels] = size(image);
% Create an empty matrix for the translated image
translated_image = zeros(size(image), 'like', image);
% Calculate valid range for translation
x_start = max(1, 1 + x_value);
x_end = min(cols, cols + x_value);
y_start = max(1, 1 + y_value);
y_end = min(rows, rows + y_value);
% Calculate corresponding source indices
x_source_start = max(1, 1 - x_value);
x_source_end = min(cols, cols - x_value);
y_source_start = max(1, 1 - y_value);
y_source_end = min(rows, rows - y_value);
% Perform translation
for c = 1:num_channels
translated_image(y_start:y_end, x_start:x_end, c) = ...
image(y_source_start:y_source_end, x_source_start:x_source_end, c);
end
end
The output for sample image is given below:
Hope that Helps!
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!