python实现投影变换

def warp_perspective(src, M, width, height, origin_x, origin_y, flags=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT,
                     borderValue=0, dst=None):

    B_SIZE = 32

    if dst == None:
        dst = np.zeros((height, width, 3), dtype=src.dtype)

    # Set interpolation mode.
    interpolation = flags & cv2.INTER_MAX
    if interpolation == cv2.INTER_AREA:
        raise Exception('Area interpolation is not supported!')

    # Prepare matrix.    
    M = M.astype(np.float64)
    if not(flags & cv2.WARP_INVERSE_MAP):
        M = cv2.invert(M)[1]
    M = M.flatten()

    x_dst = y_dst = 0
    for y in range(-origin_y, height, B_SIZE):
        for x in range(-origin_x, width, B_SIZE):
            # Block dimensions.
            bw = min(B_SIZE, width - x_dst)
            bh = min(B_SIZE, height - y_dst)

            # To avoid dimension errors.
            if bw <= 0 or bh <= 0:
                break

            # View of the destination array.
            dpart = dst[y_dst:y_dst+bh, x_dst:x_dst+bw]

            # Original code used view of array here, but we're using numpy array's.
            XY = np.zeros((bh, bw, 2), dtype=np.int16)
            A = np.zeros((bh, bw), dtype=np.uint16)

            for y1 in range(bh):
                X0 = M[0]*x + M[1]*(y + y1) + M[2]
                Y0 = M[3]*x + M[4]*(y + y1) + M[5]
                W0 = M[6]*x + M[7]*(y + y1) + M[8]

                if interpolation == cv2.INTER_NEAREST:
                    for x1 in range(bw):
                        W = np.float64(W0 + M[6]*x1);
                        if W != 0:
                            W = np.float64(1.0)/W

                        X = np.int32((X0 + M[0]*x1)*W)
                        Y = np.int32((Y0 + M[3]*x1)*W)
                        XY[y1, x1][0] = np.int16(X)
                        XY[y1, x1][1] = np.int16(Y)
                else:
                    for x1 in range(bw):
                        W = np.float64(W0 + M[6]*x1);
                        if W != 0:
                            W = cv2.INTER_TAB_SIZE/W

                        X = np.int32((X0 + M[0]*x1)*W)
                        Y = np.int32((Y0 + M[3]*x1)*W)
                        XY[y1, x1][0] = np.int16((X >> cv2.INTER_BITS) + origin_x)
                        XY[y1, x1][1] = np.int16((Y >> cv2.INTER_BITS) + origin_y)
                        A[y1, x1] = np.int16(((Y & (cv2.INTER_TAB_SIZE-1))*cv2.INTER_TAB_SIZE + (X & (cv2.INTER_TAB_SIZE-1))))

            if interpolation == cv2.INTER_NEAREST:
                cv2.remap(src, XY, None, interpolation, dst=dpart,
                          borderMode=borderMode, borderValue=borderValue)
            else:
                cv2.remap(src, XY, A, interpolation, dst=dpart,
                          borderMode=borderMode, borderValue=borderValue)

            x_dst += B_SIZE
        x_dst = 0
        y_dst += B_SIZE

    return dst
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python投影变换是一种图像处理技术,用于实现透视校正和倾斜纠正等功能。在基于Anaconda(Python)的图像投影变换中,首先需要安装和配置Anacond3以及相关软件包(如numpy,matplotlib,opencv-python)。然后,可以使用Python编程在VSCode中实现基于透视的图像校正,通过自动获取图像顶点变换来实现目标的倾斜纠正。具体步骤如下: 1. 安装Anacond3并配置环境,包括安装VSCode和Jupyter Notebook等软件。 2. 使用pip命令安装必要的软件包,如numpy、matplotlib和opencv-python。 3. 在VSCode中使用Python编程,导入相关库并加载需要进行投影变换的图像。 4. 使用透视变换函数对图像进行校正,可以通过自动获取图像的顶点变换来实现。 通过以上步骤,可以实现基于Anaconda(Python)的图像投影变换,对图像进行透视校正和倾斜纠正。更多关于Python投影变换的详细信息可以参考参考文献中的链接。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [基于Python的图像投影变换(图像的偏移矫正)](https://blog.csdn.net/dadadadajiangyou/article/details/98744944)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Python NLP笔记.md](https://download.csdn.net/download/weixin_52057528/88229062)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值