• 使用 OpenCV 进行图像投影变换


    1d7fd0687ee0d71fc09ef252e993d416.png

    投影变换(仿射变换

    在数学中,线性变换是将一个向量空间映射到另一个向量空间的函数,通常由矩阵实现。如果映射保留向量加法和标量乘法,则映射被认为是线性变换。

    要将线性变换应用于向量(即,一个点的坐标,在我们的例子中——像素的 x 和 y 值),需要将该向量乘以表示线性变换的矩阵。作为输出,你将获得一个坐标转换后的向量。

    投影变换可以用以下矩阵表示:

    5a6ac1edd18a8f2df1f48c6adb1a1aad.png

    其中:

    65db3f5ab458c31d509bce8c037e7fa0.png

    是一个旋转矩阵。该矩阵定义了将要执行的变换类型:缩放、旋转等。

    d126365b4d023058d851b27748222daa.png

    是平移向量。它只是移动点。

    21702321f0fe3ec2bed8cce47da2562c.png

    是投影向量。对于仿射变换,该向量的所有元素始终等于 0。

    如果 x 和 y 是一个点的坐标,则可以通过简单的乘法进行变换:

    afa5b79aca7e7c752782711b5ba3bdf4.png

    这里,x' 和 y' 是变换点的坐标。

    这就是仿射变换的全部理论。现在我将深入研究该程序:

    步骤 1:读取源图像并获取源图像大小:

    1. # Read source image
    2. img_src = cv2.imread('source_image.jpg')
    3. h, w, c = img_src.shape
    4. # Get source image parameter: 
    5. #[[left,top], [left,bottom], [right, top], [right, bottom]]
    6. img_src_coordinate = np.array([[0,0],[0,h],[w,0],[w,h]])

    根据源图像,我们将得到相关坐标如下:

    [[左,上],[左,下],[右,上],[右,下]]

    8b2543d514f62de73e260f861b2ebe10.png

    好的!现在我们使用 get_paste_position 来获取目标图像中的坐标,源图像将被粘贴到该坐标。

    306c38a3a67733146122f63b2b5b4a45.png
    1. def get_paste_position(event, x, y, flags, paste_coordinate_list):
    2.     
    3.     cv2.imshow('collect coordinate', img_dest_copy)
    4.     
    5.     if event == cv2.EVENT_LBUTTONUP:
    6.     
    7.     # Draw circle right in click position
    8.     
    9.     cv2.circle(img_dest_copy, (x, y), 2, (00255), -1)
    10.     
    11.     # Append new clicked coordinate to paste_coordinate_list
    12.     
    13.     paste_coordinate_list.append([x, y])

    点击4个点后,我们将4个点保存到paste_coordinate_list:

    1. while True:
    2.     cv2.waitKey(1)
    3.     
    4.     if len(paste_coordinate) == 4:
    5.     
    6.         break

    然后,这 4 个点将通过 cv2.findHomography 计算投影变换矩阵。

    matrix, _ = cv2.findHomography(img_src_coordinate, paste_coordinate, 0)

    得到投影变换矩阵后,我们将使用 cv2.warpPerspective 将源图像转换为具有目标图像大小的透视图像。

    perspective_img = cv2.warpPerspective(img_src, matrix, (img_dest.shape[1], img_dest.shape[0]))

    这是透视图的样子:

    cb4de6d8c68350ab7d9838820806e41f.jpeg

    透视图

    最后,将透视图像应用于目标图像,这就是最终效果:

    24f97783f8bce63d3400e70f40f94497.jpeg

    完整代码:

    1. import cv2 as cv2
    2. import numpy as np
    3. # This function will get click pixel coordinate that source image will be pasted to destination image
    4. def get_paste_position(event, x, y, flags, paste_coordinate_list):
    5.     cv2.imshow('collect coordinate', img_dest_copy)
    6.     if event == cv2.EVENT_LBUTTONUP:
    7.     
    8.     # Draw circle right in click position
    9.     cv2.circle(img_dest_copy, (x, y), 2, (00255), -1)
    10.     
    11.     # Append new clicked coordinate to paste_coordinate_list
    12.     paste_coordinate_list.append([x, y])
    13. if __name__ == '__main__':
    14.     
    15.     # Read source image
    16.     img_src = cv2.imread('woman-1807533_960_720.webp', cv2.IMREAD_COLOR)
    17.     
    18.     # cv2.imwrite('source_image.jpg', img_src)
    19.     h, w, c = img_src.shape
    20.     
    21.     # Get source image parameter: [[left,top], [left,bottom], [right, top], [right, bottom]]
    22.     img_src_coordinate = np.array([[0,0],[0,h],[w,0],[w,h]])
    23.     
    24.     # Read destination image
    25.     img_dest = cv2.imread('billboard-g7005ff0f9_1920.jpg', cv2.IMREAD_COLOR)
    26.     
    27.     # copy destination image for get_paste_position (Just avoid destination image will be draw)
    28.     img_dest_copy = img_dest.copy()#np.tile(img_dest, 1)
    29.     
    30.     # paste_coordinate in destination image
    31.     paste_coordinate = []
    32.     cv2.namedWindow('collect coordinate')
    33.     cv2.setMouseCallback('collect coordinate', get_paste_position, paste_coordinate)
    34.     
    35.     while True:
    36.         cv2.waitKey(1)
    37.         if len(paste_coordinate) == 4:
    38.             break
    39.     paste_coordinate = np.array(paste_coordinate)
    40.     
    41.     # Get perspective matrix
    42.     matrix, _ = cv2.findHomography(img_src_coordinate, paste_coordinate, 0)
    43.     print(f'matrix: {matrix}')
    44.     perspective_img = cv2.warpPerspective(img_src, matrix, (img_dest.shape[1], img_dest.shape[0]))
    45.     cv2.imshow('img', perspective_img)
    46.     cv2.copyTo(src=perspective_img, mask=np.tile(perspective_img, 1), dst=img_dest)
    47.     cv2.imshow('result', img_dest)
    48.     cv2.waitKey()
    49.     cv2.destroyAllWindows()
  • 相关阅读:
    Idea开发工具操作git回滚提交步骤
    ORACLE 19C pdb修改的参数保存在哪个数据字典中?
    计算机网络 ——HTTP协议(一)
    GAN论文精读 P2GAN: Posterior Promoted GAN 用鉴别器产生的后验分布来提升生成器
    环境治理行业标识解析二级节点平台建设解决方案
    小米商城侧边栏【显示向右箭头】
    贪心算法-IPO问题
    大数据库题目集——判断题
    【使用ImageFolder加载数据】
    mysql文档--默认存储引擎--innodb存储引擎--innodb引擎全解
  • 原文地址:https://blog.csdn.net/woshicver/article/details/126458074