OpenCV-坐标转换

在目标检测中,图像标注一般是4个顶点坐标或者是bbox的中心坐标、宽高和旋转角度,在特定的处理函数中会用到不同的标注方法,这时就需要对坐标进行转换,具体方法如下

# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import cv2
import numpy as np


def forward_convert(coordinate, with_label=True):
    """
    :param coordinate: format [y_c, x_c, h, w, theta]
    :return: format [y1, x1, y2, x2, y3, x3, y4, x4]
    """
    boxes = []
    if with_label:
        for rect in coordinate:
            box = cv2.boxPoints(((rect[1], rect[0]), (rect[3], rect[2]), rect[4]))
            box = np.reshape(box, [-1, ])
            boxes.append([box[1], box[0], box[3], box[2], box[5], box[4], box[7], box[6], rect[5]])
    else:
        for rect in coordinate:
            box = cv2.boxPoints(((rect[1], rect[0]), (rect[3], rect[2]), rect[4]))
            box = np.reshape(box, [-1, ])
            boxes.append([box[1], box[0], box[3], box[2], box[5], box[4], box[7], box[6]])

    return np.array(boxes, dtype=np.float32)


def back_forward_convert(coordinate, with_label=True):
    """
    :param coordinate: format [x1, y1, x2, y2, x3, y3, x4, y4, (label)] 
    :param with_label: default True
    :return: format [y_c, x_c, h, w, theta, (label)]
    """

    boxes = []
    if with_label:
        for rect in coordinate:
            box = np.int0(rect[:-1])
            box = box.reshape([4, 2])
            rect1 = cv2.minAreaRect(box)

            x, y, w, h, theta = rect1[0][0], rect1[0][1], rect1[1][0], rect1[1][1], rect1[2]
            boxes.append([y, x, h, w, theta, rect[-1]])

    else:
        for rect in coordinate:
            box = np.int0(rect)
            box = box.reshape([4, 2])
            rect1 = cv2.minAreaRect(box)

            x, y, w, h, theta = rect1[0][0], rect1[0][1], rect1[1][0], rect1[1][1], rect1[2]
            boxes.append([y, x, h, w, theta])

    return np.array(boxes, dtype=np.float32)


if __name__ == '__main__':
    coord = np.array([[150, 150, 50, 100, -90, 1],
                      [150, 150, 100, 50, -90, 1],
                      [150, 150, 50, 100, -45, 1],
                      [150, 150, 100, 50, -45, 1]])

    coord1 = np.array([[150, 150, 100, 50, 0],
                      [150, 150, 100, 50, -90],
                      [150, 150, 100, 50, 45],
                      [150, 150, 100, 50, -45]])

   # coord2 = forward_convert(coord)
    #coord3 = forward_convert(coord1, mode=-1)
    #print(coord2)
    # print(coord3-coord2)
    coord_label = np.array([[167., 203., 96., 132., 132., 96., 203., 167., 1.]])
    #
    coord4 = back_forward_convert(coord_label, with_label=1)
    coord5 = back_forward_convert(coord_label)

    print(coord4)
    # print(coord5)

    # coord3 = coordinate_present_convert(coord, -1)
    # print(coord3)
    # coord4 = coordinate_present_convert(coord3, mode=1)
    # print(coord4)

### OpenCV 中的图像坐标转换方法 在计算机视觉领域,尤其是涉及 OpenCV 的应用时,理解并掌握不同坐标系间的转换是非常重要的。以下是关于如何在 OpenCV 中实现图像坐标转换的具体方法。 #### 坐标系基础概念 为了更好地理解图像坐标系的转换过程,首先需要明确几个基本的概念: - **世界坐标系 (World Coordinate System)**:通常用于描述物体的真实位置。 - **相机坐标系 (Camera Coordinate System)**:表示相对于相机的位置和方向。 - **图像坐标系 (Image Coordinate System)**:通过投影变换得到的结果,单位通常是米或毫米。 - **像素坐标系 (Pixel Coordinate System)**:最终显示在屏幕上的离散化坐标体系[^4]。 这些不同的坐标系之间存在一定的数学关系,可以通过矩阵运算来完成相互之间的映射。 #### 使用 `cv2.getPerspectiveTransform` 进行透视变换 当涉及到平面内的几何形状变形或者矫正倾斜图片等问题时,可以利用 `cv2.getPerspectiveTransform()` 来计算源点到目标点的透视变换矩阵,并进一步调用 `cv2.warpPerspective()` 应用该变换。 ```python import cv2 import numpy as np # 定义输入四个角点以及期望输出对应的四边形顶点 src_points = np.float32([[0, 0], [100, 0], [100, 100], [0, 100]]) dst_points = np.float32([[10, 10], [90, 10], [80, 90], [20, 90]]) # 获取透视变换矩阵 M = cv2.getPerspectiveTransform(src_points, dst_points) # 对原始图像执行变换操作 image = cv2.imread('input_image.jpg') result = cv2.warpPerspective(image, M, (100, 100)) ``` 上述代码片段展示了如何定义两组对应的关键点集合 src_points 和 dst_points ,并通过它们构建一个仿射变换模型[^1]。 #### 利用 `solvePnP` 实现三维空间定位 对于更复杂的场景比如增强现实(AR),可能还需要考虑实际物理环境中的深度信息,则可借助于 PnP(Point-to-Plane)算法求解姿态参数: ```python objectPoints = np.array([ [[0],[0],[0]], [[1],[0],[0]], ... ]) imgPoints = ... ret, rvec, tvec = cv2.solvePnP(objectPoints, imgPoints, cameraMatrix, distCoeffs) rotation_matrix, _ = cv2.Rodrigues(rvec) translation_vector = tvec.reshape((3,)) print("Rotation Matrix:\n", rotation_matrix) print("Translation Vector:", translation_vector) ``` 这里我们假设已经获取到了一系列匹配好的特征点对 objectPoints 和 imgPoints 。接着就可以调用 solvePnP 得到旋转向量 rvec 和平移向量 tvec 表达形式下的外参估计值[^2]。 #### 总结 综上所述,在 OpenCV 当中存在着多种途径来进行有效的图像坐标系转化处理工作。无论是简单的二维平面调整还是深入探索立体视觉技术都离不开扎实的基础理论支撑与灵活多变的实际编程技巧相结合的过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值