python+opencv图像下采样与扭曲(一)

图像下采样与扭曲方法(一)

`
在模型训练中往往需要合成一些假数据来添加到数据中,来增强训练效果,但是合成的样本和真实样本有的时候相差太远,因此使用下采样和扭曲的方法来模拟真实场景的图片。

一、基础下采样 + 仿射变换
二、透视变换扭曲

前言

使用opencv + python的方法来进行实现

一、基础下采样 + 仿射变换

使用opencv中的resize方法来进行图片下采样,再使用放射变换来扭曲图像

代码如下(示例):

    def basic_downsample_and_warp(image_path, scale_factor=0.5):
        img = cv2.imread(image_path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        height, width = img.shape[:2]
        new_width = int(width * scale_factor)
        new_height = int(height * scale_factor)
        downsampled = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR)

        rows, cols = downsampled.shape[:2]

        # 源点
        src_points = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1]])
        dst_points = np.float32([
            [3, 3],
            [cols - 6, -3],
            [4, rows - 4]
        ])

        # 计算仿射变换矩阵
        M = cv2.getAffineTransform(src_points, dst_points)

        # 应用仿射变换
        warped = cv2.warpAffine(downsampled, M, (cols, rows))

        return downsampled, warped

二、透视变换扭曲

使用opencv中的resize方法来进行图片下采样,再使用放射变换来扭曲图像

代码如下(示例):

    def basic_downsample_and_warp(image_path, scale_factor=0.5):
        img = cv2.imread(image_path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        height, width = img.shape[:2]
        new_width = int(width * scale_factor)
        new_height = int(height * scale_factor)
        downsampled = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR)

        rows, cols = downsampled.shape[:2]

        # 源点
        src_points = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1]])
        dst_points = np.float32([
            [5, 5],
            [cols - 10, -5],
            [8, rows - 8]
        ])

        # 计算仿射变换矩阵
        M = cv2.getAffineTransform(src_points, dst_points)

        # 应用仿射变换
        warped = cv2.warpAffine(downsampled, M, (cols, rows))

        return downsampled, warped

### 实现图像的球面化扭曲效果 为了实现图像的球面化扭曲效果,可以采用多种编程技术和库来完成这目标。以下是基于 OpenCV 和 NumPy 的 Python 方法[^1],以及 MATLAB 中的相关功能[^2]。 #### 使用 OpenCV 和 NumPy 进行球面化扭曲Python 中,可以通过 `cv2.remap` 函数实现自定义的像素映射操作。具体来说,球面化扭曲涉及将原始图像中的每个像素重新定位到个新的位置上,该位置由个数学模型决定。以下是个具体的代码示例: ```python import cv2 import numpy as np def spherical_transform(image, strength=0.5): height, width = image.shape[:2] map_x = np.zeros((height, width), dtype=np.float32) map_y = np.zeros((height, width), dtype=np.float32) for y in range(height): for x in range(width): nx = (x - width / 2) / (width / 2) ny = (y - height / 2) / (height / 2) r = np.sqrt(nx ** 2 + ny ** 2) # 应用球面变换公式 theta = r * np.pi * strength if r != 0: rx = np.sin(theta) * nx / r ry = np.sin(theta) * ny / r else: rx = nx ry = ny new_x = int(rx * width / 2 + width / 2) new_y = int(ry * height / 2 + height / 2) if 0 <= new_x < width and 0 <= new_y < height: map_x[y, x] = float(new_x) map_y[y, x] = float(new_y) else: map_x[y, x] = 0 map_y[y, x] = 0 result = cv2.remap(image, map_x, map_y, interpolation=cv2.INTER_LINEAR) return result # 加载图像并应用球面化扭曲 image = cv2.imread('input_image.jpg') spherical_warped = spherical_transform(image, strength=0.7) cv2.imwrite('output_spherical.jpg', spherical_warped) ``` 上述代码实现了基本的球面化扭曲逻辑,其中参数 `strength` 控制变形的程度。通过调整此参数,可以获得不同程度的球面效果。 #### 使用 MATLAB 实现球面化扭曲 MATLAB 提供了更高级别的支持,允许利用内置函数快速构建复杂的几何变换。下面展示了个简单的方法,使用网格重采样技术实现类似的球面化效果: ```matlab function outputImage = sphericalWarp(inputImage, strength) [rows, cols, ~] = size(inputImage); [X, Y] = meshgrid(1:cols, 1:rows); % 归化坐标系至 [-1, 1] Xn = (X - cols/2) / (cols/2); Yn = (Y - rows/2) / (rows/2); R = sqrt(Xn.^2 + Yn.^2); % 计算新的角度和半径关系 Theta = R .* pi * strength; Rx = sin(Theta).*Xn ./ max(R, eps); Ry = sin(Theta).*Yn ./ max(R, eps); newX = round(Rx*(cols/2)+cols/2); newY = round(Ry*(rows/2)+rows/2); % 边界裁剪 validIdx = newX >=1 & newX<=cols & newY>=1 & newY<=rows; outputImage = uint8(zeros(size(inputImage))); for i = 1:size(validIdx, 1) for j = 1:size(validIdx, 2) if validIdx(i,j) outputImage(i,j,:) = inputImage(newY(i,j),newX(i,j), :); end end end end ``` 这段 MATLAB 脚本同样依赖于极坐标的转换方式,并且能够灵活调节强度因子以适应不同的需求。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值