matlib 多种方法实现图像旋转不使用imrotate函数
原理
方法很棒https://blog.csdn.net/qq_41140138/article/details/104737705
方法一
function g = rotate_image1(f, theta)
[M,N]=size(f);
theta=theta*pi/180;
% img_rotate = ones(M,N);
T= [cos(theta),sin(theta);-sin(theta),cos(theta)];%旋转变换 % 计算显示完整图像需要的画布大小
hh = floor(M*sin(theta)+N*cos(theta))+1; %floor取整
ww = floor(M*cos(theta)+N*sin(theta))+1;
img_rotate = uint8(ones(hh, ww)*128);
for i=1:hh for j=1:ww p = round(T'*([i;j]-[hh/2;ww/2])+[M/2;N/2]);
% p = floor([i,j,1]*T^-1);%由P_new = P_old*T 可得:P_old = P_new*(T^-1) if (p(1)<=M)&&(p(1)>0)&&(p(2)<=N)&&(p(2)>0) %限制范围 img_rotate(i,j) = f(p(1),p(2)); %坐标变换关系 elseimg_rotate(i,j) = 0; %没有的点赋值为0 endend
end
imshow(img_rotate);
方法二:
close all;clear all;clc
f = imread('Lena.bmp');
[m,n]=size(f);
%theta=theta*pi/180;
theta=30*pi/180;
%反向映射
r_new = round(abs(m*cos(theta)) + abs(n*sin(theta)));
c_new = round(abs(m*sin(theta)) + abs(n*cos(theta)));
T2 = [1,0,0;0,1,0;-r_new/2,-c_new/2,1]; %x、y轴平移到原点
T3 = [1,0,0;0,1,0;m/2,n/2,1]; %x、y轴反平移
T1 = [cos(theta),sin(theta),0;-sin(theta),cos(theta),0;0,0,1];%旋转变换
T = T2*T1*T3;
dst = zeros(r_new,c_new);
for i = 1:r_new for j = 1:c_new %从新图像映射到原图像中去如果不在原图像中,新图像为0,如果在的,将该点的最邻近取值赋值过来 P = T'*[i;j;1] ;%P(1)为x的值 P(2)为y的值 if(round(P(1))>0 && round(P(1))<m && round(P(2))>0 && round(P(2))<n) dst(i,j,1) = f(round(P(2)),round(P(1)),1);%取f的最邻近值 elsedst(i,j) = 0;endend
end
figure; imshow(uint8(dst)); title('最邻近插值');
方法三
img = imread('lena_color_512.tif');
[height, width, color] = size(img);theta = 1/3*pi;
C = round([height/2, width/2]); % 原图像中心
R = [cos(theta) sin(theta); -sin(theta) cos(theta)];% 计算新图像大小
nw = round(([0, 0]-C)*R + C);
ne = round(([0, width]-C)*R + C);
sw = round(([height, 0]-C)*R + C);
se = round(([height, width]-C)*R + C);
h = max([nw(1), ne(1), sw(1), se(1)]) - min([nw(1), ne(1), sw(1), se(1)]);
w = max([nw(2), ne(2), sw(2), se(2)]) - min([nw(2), ne(2), sw(2), se(2)]);target = zeros(h, w, 3, 'uint8') + 255;
c = round([h/2, w/2]); % 新图像中心% 旋转
for x = 1 : heightfor y = 1 : widthP = [x, y];Q = round((P-C)*R + c);if (Q(1)>0 && Q(1)<=h) && (Q(2)>0 && Q(2)<=w) % 越界判断target(Q(1), Q(2), :) = img(x, y, :);endend
end
imshow(target)
改进代码
%%
img = imread('Lena.bmp');
[height, width, color] = size(img);theta = 1/4*pi;
C = round([height/2, width/2]); % 原图像中心
R = [cos(theta) sin(theta); -sin(theta) cos(theta)];
R = inv(R); % 逆矩阵% 原图像四个角旋转后的坐标
nw = round(([0, 0]-C)*R + C);
ne = round(([0, width]-C)*R + C);
sw = round(([height, 0]-C)*R + C);
se = round(([height, width]-C)*R + C);% 新图像
h = max([nw(1), ne(1), sw(1), se(1)]) - min([nw(1), ne(1), sw(1), se(1)]);
w = max([nw(2), ne(2), sw(2), se(2)]) - min([nw(2), ne(2), sw(2), se(2)]);
target = zeros(h, w, 3, 'uint8') + 255;
c = round([h/2, w/2]); % 新图像中心for x = 1 : hfor y = 1 : wQ = [x, y];% 根据目标像素点计算原图像像素点P = round((Q-c)*R + C);if (P(1)>0 && P(1)<=height) && (P(2)>0 && P(2)<=width) % 越界判断target(x, y, :) = img(round(P(1)), round(P(2)), :);elsetarget(x, y, :) =0;endend
end
subplot(1,2,1), imshow(img),title('input image');
subplot(1,2,2),imshow(target),title('rotate image');
matlib 多种方法实现图像旋转不使用imrotate函数相关推荐
- Python 使用多种方法对图像进行锐化处理——图像处理
通过使用不同方法对图像进行锐化处理,更改参数对比图像显示,代码如下: # (6).随机读取一幅图像,对其进行锐化, #导入库 import cv2 import skimage.filters as ...
- 小白学习图像处理3——图像旋转原理
文章目录 一.图像旋转的原理 二.使用matlab实现 1.思路 2.实现代码 三.优化 1.思路 2.代码实现 3.使用双线性插值 四.matlab函数实现图像旋转 1.imrotate函数 2.i ...
- python函数图像平移_[Python图像处理]六.图像缩放,图像旋转,图像翻转与图像平移...
图像缩放 图像缩放主要是调用resize()函数实现,result = cv2.resize(src, dsize[, result[.fx, fy[,interpolation]]]) 其中src ...
- 数字图像处理(9): 图像缩放、图像旋转、图像翻转 和 图像平移
目录 1 图像缩放- resize() 2 图像旋转- getRotationMatrix2D(), warpAffine() 3 图像翻转- flip() 4 图像平移- warpAffine() ...
- 图像旋转的原理与实现
图像旋转的原理与实现 图像旋转的原理与实现 一般图像的旋转是以图像的中心为原点,旋转一定的角度,也就是将图像上的所有像素都旋转一个相同的角度.旋转后图像的的大小一般会改变,即可以把转出显示区域的图像截 ...
- openCV Python基础--镜像翻转和图像旋转
镜像翻转 flip()函数: flip函数是矩阵或者图像翻转,其实图像的本质也是矩阵. void flip(InputArray src, OutputArray dst, int flipCode) ...
- 【OpenCV学习笔记】之六 手写图像旋转函数---万丈高楼平地起
话说,平凡之处显真格,这一点也没错! 比如,对旋转图像进行双线性插值,很简单吧? 可,对我,折腾了大半天,也没有达到预期效果! 尤其是三个误区让我抓瞎好久: 1,坐标旋转公式. 这东西,要用 ...
- QT界面中实现视频帧显示的多种方法及应用
QT界面中实现视频帧显示的多种方法及应用 (一) 引言 1.1 视频帧在QT界面中的应用场景 1.2 不同方法的性能和适用性分析 1.2.1 使用QLabel和QPixmap 1.2.2 使用QPai ...
- 基于深度学习的目标检测:数据增强(一)图像翻转、图像旋转、图像放缩
1.数据增强简介 数据增强(data augmentation),又名数据增广或数据扩充,其本质是通过使用图像处理方法,基于有限的数据产生更多的数据,以此增加训练样本的数量以及多样性,进而提升模型的泛 ...
- python opencv 图像旋转
python opencv 图像旋转 原图 顺时针旋转 代码: import cv2 path = '2.jpg' img = cv2.imread(path,1) trans_img = cv2.t ...
最新文章
- 语音识别可以直接编码吗
- [armv9]-ARMV8/ARMV9安全架构介绍(ARMv9 CCA)
- [抄]外部奖励对内在动机的侵蚀
- php接口防止app重复提交,AOP防止接口重复提交
- CIO:节省IT部门开支十招
- 【转】详解vue的diff算法
- 含泪推荐5款实用又小巧的PC软件
- biopython有什么用_Biopython介绍
- [realview] warning: #550-D: variable d was set but never used
- 图森面试官| 图森未来首席科学家王乃岩:播下去的种子,早晚会开花
- 姿态识别+校准|视觉技术新突破
- 巧妙设置win7系统给WPS文档加密
- WPF绑定(Binding)绑定对象集合修改显示属性问题
- There is no getter/setter for property named ‘XXX‘ in ‘class com.XXX‘
- NTP 时区+时间同步
- 最新:拼多多将追回所有“薅羊毛”订单,包括已充值话费和Q币订单...
- #用中国速度与疫情赛跑#【火神山建设不完全手册】
- python-布尔运算
- vue模板字符串中点击事件传递参数
- 纬度app:果酸焕肤是个什么原理,自己在家可以做吗?