当前位置: 首页 > news >正文

图像处理 -- 仿射变换之Affine Transformation

仿射变换(Affine Transformation)

仿射变换是图像处理中的一种基本操作,通过线性变换和平移实现图像的几何变换。仿射变换包括旋转、缩放、平移、翻转、错切(shear)等操作。

1. 仿射变换的作用
  • 旋转:将图像绕一个固定点(通常是图像中心)旋转一定角度。
  • 缩放:对图像进行放大或缩小,改变图像的尺寸。
  • 平移:将图像在平面上移动到指定位置。
  • 翻转:沿特定轴将图像反转,比如水平或垂直翻转。
  • 错切:将图像的某一个方向拉伸,使图像产生倾斜效果。

仿射变换在计算机视觉和图像处理中的应用非常广泛,比如在图像配准、目标检测、图像增强等领域中都可以看到仿射变换的身影。

2. 数学实现原理

仿射变换通过一个矩阵乘法和一个向量加法来实现。假设二维空间中的一个点的坐标为 ( x , y ) (x, y) (x,y),经过仿射变换后,该点的新坐标为 ( x ′ , y ′ ) (x', y') (x,y)。则有如下数学表达式:

( x ′ y ′ ) = ( a b c d ) ( x y ) + ( e f ) \begin{pmatrix} x' \\ y' \\ \end{pmatrix} = \begin{pmatrix} a & b \\ c & d \\ \end{pmatrix} \begin{pmatrix} x \\ y \\ \end{pmatrix} + \begin{pmatrix} e \\ f \\ \end{pmatrix} (xy)=(acbd)(xy)+(ef)

这里, ( a b c d ) \begin{pmatrix} a & b \\ c & d \end{pmatrix} (acbd) 是 2x2 仿射变换矩阵, ( e f ) \begin{pmatrix} e \\ f \end{pmatrix} (ef) 是平移向量。

具体仿射变换:
  1. 平移
    x ′ = x + e , y ′ = y + f x' = x + e, \quad y' = y + f x=x+e,y=y+f
    其中, e e e f f f 是平移量。

  2. 旋转
    x ′ = x ⋅ cos ⁡ ( θ ) − y ⋅ sin ⁡ ( θ ) , y ′ = x ⋅ sin ⁡ ( θ ) + y ⋅ cos ⁡ ( θ ) x' = x \cdot \cos(\theta) - y \cdot \sin(\theta), \quad y' = x \cdot \sin(\theta) + y \cdot \cos(\theta) x=xcos(θ)ysin(θ),y=xsin(θ)+ycos(θ)
    其中, θ \theta θ 是旋转角度。

  3. 缩放
    x ′ = x ⋅ s x , y ′ = y ⋅ s y x' = x \cdot s_x, \quad y' = y \cdot s_y x=xsx,y=ysy
    其中, s x s_x sx s y s_y sy 是在 x 轴和 y 轴上的缩放比例。

  4. 错切
    x ′ = x + λ y ⋅ y , y ′ = y + λ x ⋅ x x' = x + \lambda_y \cdot y, \quad y' = y + \lambda_x \cdot x x=x+λyy,y=y+λxx
    其中, λ x \lambda_x λx λ y \lambda_y λy 是在 x 轴和 y 轴上的错切系数。

3. 注意事项
  1. 矩阵可逆性:为了保证仿射变换能被逆变换,仿射变换矩阵 ( a b c d ) \begin{pmatrix} a & b \\ c & d \end{pmatrix} (acbd) 必须是非奇异的,即其行列式不为 0。这确保了变换后的图像可以通过逆变换恢复原状。

  2. 边界处理:仿射变换可能导致图像的一部分移出图像边界,或者图像空白区域增加。因此,通常需要对图像进行裁剪或填充处理。

  3. 插值方法:在仿射变换过程中,目标图像中的像素可能对应源图像中非整数坐标的点,因此需要使用插值方法(如最近邻插值、双线性插值或双三次插值)来计算这些点的像素值。

  4. 保持图像质量:频繁进行仿射变换可能导致图像质量下降,比如模糊、锯齿效应等。因此,在设计变换时需要考虑如何最小化质量损失。

  5. 变换顺序:如果需要执行多个变换,变换的顺序会影响最终结果。例如,旋转后再平移和先平移后旋转的结果是不一样的。因此,需要谨慎设计变换的顺序以达到预期效果。

C代码示例

#include <stdio.h>
#include <stdlib.h>
#include <math.h>#define IMAGE_WIDTH 1088
#define IMAGE_HEIGHT 1288
#define DEGREE_TO_RADIAN(deg) ((deg) * M_PI / 180.0)// 图像数据结构
typedef struct {int width;int height;unsigned char *data; // 指向图像像素数据的指针
} Image;// 从RAW8文件读取图像数据
Image *read_raw8_image(const char *filename, int width, int height) {FILE *file = fopen(filename, "rb");if (!file) {printf("无法打开文件 %s\n", filename);return NULL;}// 在堆上为图像数据分配内存unsigned char *data = (unsigned char *)malloc(width * height * sizeof(unsigned char));if (!data) {printf("内存分配失败\n");fclose(file);return NULL;}// 读取图像数据fread(data, sizeof(unsigned char), width * height, file);fclose(file);// 创建Image结构并返回Image *image = (Image *)malloc(sizeof(Image));image->width = width;image->height = height;image->data = data;return image;
}// 将图像数据写入RAW8文件
void write_raw8_image(const char *filename, Image *image) {FILE *file = fopen(filename, "wb");if (!file) {printf("无法打开文件 %s\n", filename);return;}// 写入图像数据fwrite(image->data, sizeof(unsigned char), image->width * image->height, file);fclose(file);
}
// 释放图像内存
void free_image(Image *image) {if (image) {if (image->data) {free(image->data);}free(image);}
}// 仿射变换函数:旋转图像
Image *affine_transform(Image *image, float angle) {int width = image->width;int height = image->height;// 角度转弧度float radians = DEGREE_TO_RADIAN(angle);float cos_theta = cos(radians);float sin_theta = sin(radians);// 中心点int center_x = width / 2;int center_y = height / 2;// 在堆上为旋转后的图像数据分配内存Image *rotated_image = (Image *)malloc(sizeof(Image));rotated_image->width = width;rotated_image->height = height;rotated_image->data = (unsigned char *)malloc(width * height * sizeof(unsigned char));// 初始化旋转后图像为0(黑色)for (int i = 0; i < width * height; i++) {rotated_image->data[i] = 0;}// 遍历原始图像的每一个像素for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {// 计算相对于中心点的偏移int x_offset = x - center_x;int y_offset = y - center_y;// 进行仿射变换(旋转)int new_x = (int)(cos_theta * x_offset + sin_theta * y_offset) + center_x;int new_y = (int)(-sin_theta * x_offset + cos_theta * y_offset) + center_y;// 检查新坐标是否在图像范围内if (new_x >= 0 && new_x < width && new_y >= 0 && new_y < height) {rotated_image->data[new_y * width + new_x] = image->data[y * width + x];}}}return rotated_image;
}
int main() {// 读取RAW8图像Image *image = read_raw8_image("input.raw", IMAGE_WIDTH, IMAGE_HEIGHT);if (!image) {return 1;}// 对图像进行15度旋转Image *rotated_image = affine_transform(image, 15.0);// 保存旋转后的图像write_raw8_image("rotated_output.raw", rotated_image);// 释放内存free_image(image);free_image(rotated_image);return 0;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 多个条件同时查询时username和name无法被解析
  • git 配置SSH
  • 计算机网络:DNS、子网掩码、网关
  • 查找技术(4/6 改)
  • 【git】 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED
  • 低代码与AI:赋能企业数字化转型
  • WPF篇(20)- Menu菜单+ContextMenu上下文菜单+StatusBar状态栏
  • 《黑神话:悟空》媒体评分解禁 M站均分82
  • Qt:槽函数的响应和禁用
  • Nuclei文件上传小Tips
  • ubuntu20 lightdm无法自动登录进入桌面
  • JVM -垃圾回收器
  • ceph如何增删改查的管理文件
  • PostgreSQL的pg_dump测试
  • 基于springboot的校园失物招领系统--论文pf
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  • Github访问慢解决办法
  • Java IO学习笔记一
  • JAVA多线程机制解析-volatilesynchronized
  • MobX
  • Promise面试题,控制异步流程
  • React16时代,该用什么姿势写 React ?
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 百度贴吧爬虫node+vue baidu_tieba_crawler
  • 产品三维模型在线预览
  • 大快搜索数据爬虫技术实例安装教学篇
  • 大主子表关联的性能优化方法
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 深度学习在携程攻略社区的应用
  • 再谈express与koa的对比
  • 智能网联汽车信息安全
  • 自定义函数
  • C# - 为值类型重定义相等性
  • elasticsearch-head插件安装
  • 函数计算新功能-----支持C#函数
  • # linux 中使用 visudo 命令,怎么保存退出?
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • $forceUpdate()函数
  • $nextTick的使用场景介绍
  • (003)SlickEdit Unity的补全
  • (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (HAL库版)freeRTOS移植STMF103
  • (NSDate) 时间 (time )比较
  • (zt)基于Facebook和Flash平台的应用架构解析
  • (补)B+树一些思想
  • (二)Eureka服务搭建,服务注册,服务发现
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (七)c52学习之旅-中断
  • (四)库存超卖案例实战——优化redis分布式锁
  • (太强大了) - Linux 性能监控、测试、优化工具
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...