OpenCV实验(6):图像变形

实验要求:
在这里插入图片描述
实验思路:
1.从输出图像开始运算,按照公式计算即可。或参考其他文章
https://blog.csdn.net/qq_38137411/article/details/83213297
2.本实验代码中不包含任何循环,通过尽可能使用numpy加速使时间从4.6s下降到约0.076s。建议是根据代码弄清楚各个numpy矩阵的含义和形式,方便理解。

实验效果:
在这里插入图片描述
在这里插入图片描述

实验代码:

import numpy as np
import cv2 as cv


def anamorphic(imgSrc):
    """
    根据实验要求的公式实现图像变形
    为了尽可能的减少运行时间,所有的循环和各种运算都尽可能地使用了Numpy,
    因此在该函数中不包含任何循环,时间从4.6s优化到了0.076s

    :param imgSrc: 输入图像
    :return: imgDst time 变形后的图像 时间
    """
    timeBegin = cv.getTickCount()  # 记录开始时间
    rows, cols, channels = imgSrc.shape

    coordinateDst = np.zeros([rows, cols, 2])  # 储存输出图像中心归一化坐标
    coordinateSrc = np.zeros([rows, cols, 2])  # 储存输入图像中心归一化坐标
    coordinateFinal = np.zeros([rows, cols, 2])  # 储存输入图像的原坐标

    # 输出图像中心归一化
    coordinateDst[:, :, 1] = np.arange(cols)
    coordinateDst[:, :, 0] = np.arange(rows).reshape(-1, 1)
    coordinateDst = (coordinateDst - 0.5 * np.array([rows, cols])) / (0.5 * np.array([rows, cols]))

    # 实现映射
    r = np.sqrt(np.sum(coordinateDst ** 2, axis=2))  # 计算所有的r
    theta = (1 - r) ** 2  # 计算所有的theta
    coordinateSrc[:, :, 0] = np.cos(theta) * coordinateDst[:, :, 0] - np.sin(theta) * coordinateDst[:, :, 1]
    coordinateSrc[:, :, 1] = np.sin(theta) * coordinateDst[:, :, 0] + np.cos(theta) * coordinateDst[:, :, 1]
    # 计算r的掩码,此时的r的值只是为0和1
    r = np.expand_dims(r, 2).repeat(2, axis=2)  # 先复制并扩充一个维度
    r[r < 1] = -1
    r[r >= 1] = 0
    r[r == -1] = 1
    coordinateFinal += np.multiply(r, coordinateSrc)  # 对应于 f^-1 中的“otherwise”
    # 下面三句交换0 和 1
    r[r == 1] = -1
    r[r == 0] = 1
    r[r == -1] = 0
    coordinateFinal += np.multiply(r, coordinateDst)  # 对应于 f^-1 中的“if r >= 1”
    coordinateFinal = (coordinateFinal + 1) * np.array([0.5*rows, 0.5*cols])
    coordinateFinal = coordinateFinal.astype(np.int)  # 不能为np.uint8,可以试一试

    x = np.arange(rows).repeat(cols)  # x 为 [0, 0, 0, ..., 2, 2, 2, ..., rows-1]
    y = np.tile(np.arange(cols), rows)  # y 为 [0, 1, 2, ..., cols-1, 0, 1, 2, ...cols-1, ...]
    # imgDst为输出图像
    imgDst = imgSrc[coordinateFinal[x, y, 0], coordinateFinal[x, y, 1], :].reshape(rows, cols, channels)

    timeEnd = cv.getTickCount()  # 记录结束时间
    time = (timeEnd - timeBegin) / cv.getTickFrequency()  # 计算总时间
    return imgDst, time  # 0.076 s


imgSrc = cv.imread('../images/images3_2/lab2.png')
imgDst, time = anamorphic(imgSrc)
cv.imshow('imgSrc', imgSrc)
cv.imshow('imgDst', imgDst)
print('Successful!!!')
print('time: %.4f s' % time)
cv.waitKey(0)
cv.destroyAllWindows()
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Python OpenCV库实现图片弹性形变的代码示例: ```python import cv2 import numpy as np # 读取图片 img = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE) # 定义弹性形变函数 def elastic_transform(image, alpha, sigma, random_state=None): """ 弹性形变函数 :param image: 图像 :param alpha: 控制形变程度的参数 :param sigma: 控制平滑程度的参数 :param random_state: 随机数生成器 """ if random_state is None: random_state = np.random.RandomState(None) shape = image.shape dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha x, y = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]), indexing='ij') indices = np.reshape(x+dx, (-1, 1)), np.reshape(y+dy, (-1, 1)) return map_coordinates(image, indices, order=1).reshape(shape) # 调用弹性形变函数 result = elastic_transform(img, alpha=200, sigma=20) # 显示原始图片和弹性形变后的图片 cv2.imshow('Original Image', img) cv2.imshow('Elastic Transform Image', result) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在以上代码中,使用了`gaussian_filter()`和`map_coordinates()`函数来实现弹性形变。其中,`gaussian_filter()`函数用于生成平滑随机数,`map_coordinates()`函数用于根据随机数生成的位移对原始图像进行形变操作。通过调整`alpha`和`sigma`参数,可以控制形变程度和平滑程度。最后,通过`cv2.imshow()`函数显示原始图片和弹性形变后的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值