仿射变换和弹性变换

仿射变换   

相当于对于图像做了一个平移、旋转、放缩、剪切、对称。与刚体变换相同的是,可以保持线点之间的平行和共线关系。即 原来平行的直线变化后还是平行的。但是和刚体变换不同的是线段之间的长度会发生变化。

è¿éåå¾çæè¿°

 

在python中用opencv实现: 
由于变换矩阵的确定太过复杂,所以会采用3点去定法。即给出3个点在原图和变换后图像的位置,返回一个变换矩阵。pts1和pts2是3个点的list

M = cv2.getAffineTransform(pts1, pts2)

 

随机的仿射变换:

    shape = image.shape
    shape_size = shape[:2]
    # Random affine
    center_square = np.float32(shape_size) // 2
    square_size = min(shape_size) // 3
    pts1 = np.float32([center_square + square_size, [center_square[0]+square_size, center_square[1]-square_size], center_square - square_size])
    pts2 = pts1 + random_state.uniform(-alpha_affine, alpha_affine, size=pts1.shape).astype(np.float32)
    M = cv2.getAffineTransform(pts1, pts2)
    image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)  

 

 

弹性变化

        弹性变化算法(Elastic Distortion)最先是由Patrice等人在2003年提出的,最开始应用在mnist手写体数字识别数据集中,发现对原图像进行弹性变换的操作扩充样本以后,对于手写体数字的识别效果有明显的提升。此后成为一种很普遍的扩充字符样本图像的方式。

è¿éåå¾çæè¿°

 

       弹性变化是对像素点各个维度产生(-1,1)区间的随机标准偏差,并用高斯滤波(0,sigma)对各维度的偏差矩阵进行滤波,最后用放大系数alpha控制偏差范围。 因而由A(x,y)得到的A’(x+delta_x,y+delta_y)。A‘的值通过在原图像差值得到,A’的值充当原来A位置上的值。一般来说,alpha越小,sigma越大,产生的偏差越小,和原图越接近。

代码实现:

import numpy as np
import pandas as pd
import cv2
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter
import matplotlib.pyplot as plt
def Elastic_transform(image, alpha, sigma):
    shape = image.shape
    shape_size = shape[:2]
    dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
    dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
    dz = np.zeros_like(dx)

    x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
    indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1)), np.reshape(z, (-1, 1))

    return map_coordinates(image, indices, order=1, mode='reflect').reshape(shape)

 

随机仿射弹性变换实现:

def elastic_transform(image, alpha, sigma, alpha_affine, random_state=None):
    """Elastic deformation of images as described in [Simard2003]_ (with modifications).
    .. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
         Convolutional Neural Networks applied to Visual Document Analysis", in
         Proc. of the International Conference on Document Analysis and
         Recognition, 2003.
     Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5
    """
    if random_state is None:
        random_state = np.random.RandomState(None)

    shape = image.shape
    shape_size = shape[:2]

    # Random affine
    center_square = np.float32(shape_size) // 2
    square_size = min(shape_size) // 3
    pts1 = np.float32([center_square + square_size, [center_square[0]+square_size, center_square[1]-square_size], center_square - square_size])
    pts2 = pts1 + random_state.uniform(-alpha_affine, alpha_affine, size=pts1.shape).astype(np.float32)
    M = cv2.getAffineTransform(pts1, pts2)
    image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)

    dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
    dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
    dz = np.zeros_like(dx)

    x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
    indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1)), np.reshape(z, (-1, 1))

    return map_coordinates(image, indices, order=1, mode='reflect').reshape(shape)
 

随机仿射弹性变换效果

è¿éåå¾çæè¿°

è¿éåå¾çæè¿° 

### 实现仿射变换图像配准的代码示例 为了实现基于仿射变换的图像配准,可以采用多种编程语言库来完成这一任务。下面提供了一个Python环境下的例子,该实例利用了`scikit-image`库中的函数来进行仿射变换操作。 #### 导入必要的包 ```python from skimage import io, color, transform import numpy as np ``` #### 加载待配准的两幅图片 ```python fixed_image = io.imread('path_to_fixed_image') # 固定图像路径 moving_image = io.imread('path_to_moving_image') # 移动图像路径 ``` #### 定义控制点用于计算仿射变换矩阵 这里假设已知一些匹配的关键点对,这些点可以帮助定义从移动图到固定图的空间映射关系。 ```python src_points = np.array([[x1,y1], [x2,y2]]) # 来自于moving_image的关键点坐标 dst_points = np.array([[X1,Y1], [X2,Y2]]) # 对应于fixed_image的位置 tform = transform.estimate_transform('affine', src=src_points, dst=dst_points) ``` #### 应用仿射变换至移动图像上并显示结果 ```python warped_moving_image = transform.warp(moving_image, tform.inverse) # 显示原图与变形后的对比 io.imshow(fixed_image); plt.show() io.imshow(warped_moving_image); plt.show() # 将两张图片重叠查看配准效果 overlay = fixed_image.copy() for channel in range(3): # 假设是RGB三通道彩色图像 overlay[:,:,channel] = (fixed_image[:,:,channel]/2 + warped_moving_image[:,:,channel]/2).astype(np.uint8) io.imshow(overlay); plt.show() ``` 上述过程展示了如何使用Python及其科学计算生态系统的工具集执行简单的仿射变换图像配准[^1]。此方法适用于具有线性几何变化的情况;对于更复杂的非刚体形变,则可能需要考虑其他类型的模型或算法,比如弹性注册(elastic registration)[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值