OpenCV 图形API(15)计算两个矩阵(通常代表二维向量的X和Y分量)每个对应元素之间的相位角(即角度)函数phase()

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

描述

cv::gapi::phase 是 OpenCV 的 G-API 模块中的一个函数,用于计算两个矩阵(通常代表二维向量的X和Y分量)每个对应元素之间的相位角(即角度)。这个函数特别适用于处理复数的极坐标表示或计算光流等应用中。

计算二维向量的旋转角度。

cv::phase 函数计算由 x 和 y 的对应元素形成的每个二维向量的旋转角度:
angle ( I ) = atan2 ( y ( I ) , x ( I ) ) \texttt{angle} (I) = \texttt{atan2} ( \texttt{y} (I), \texttt{x} (I)) angle(I)=atan2(y(I),x(I))

角度估计的精度约为 0.3 度。当 x(I)=y(I)=0 时,对应的 angle(I) 被设置为 0。

函数原型

cpp 复制代码
GMat cv::gapi::phase 
(
 	const GMat &  	x,
	const GMat &  	y,
	bool  	angleInDegrees = false 
) 		

参数

  • 参数 x: 输入浮点数数组,包含 2D 向量的 x 坐标。
  • 参数 y: 输入数组,包含 2D 向量的 y 坐标;它必须与 x 具有相同的大小和类型。
  • 参数 angleInDegrees: 如果为 true,则函数计算的角度以度为单位;否则,角度以弧度为单位。

返回值

  • 向量角度数组;它具有与 x 相同的大小和类型。

代码示例

cpp 复制代码
#include <opencv2/gapi.hpp>
#include <opencv2/gapi/core.hpp>
#include <opencv2/opencv.hpp>

int main()
{
    // 创建示例输入矩阵
    cv::Mat x = ( cv::Mat_< float >( 2, 2 ) << 1.f, -1.f, -1.f, 1.f );
    cv::Mat y = ( cv::Mat_< float >( 2, 2 ) << 1.f, 1.f, -1.f, -1.f );

    // 定义G-API计算图
    cv::GComputation phaseComp( []() {
        cv::GMat inX, inY;
        cv::GMat out = cv::gapi::phase( inX, inY, true );  // 使用度作为角度单位
        return cv::GComputation( cv::GIn( inX, inY ), cv::GOut( out ) );
    } );

    // 输出矩阵
    cv::Mat dst;

    // 执行计算图
    phaseComp.apply( x, y, dst, cv::compile_args() );

    // 打印结果
    std::cout << "Phase angles (in degrees): \n" << dst << std::endl;

    return 0;
}

运行结果

bash 复制代码
Phase angles (in degrees): 
[44.990456, 135.00955;
 224.99045, 315.00955]
相关推荐
寒墨默涵1 分钟前
Trigger.Dev Webhooks 驱动 AI 智能体实战效果展示
人工智能
manyingAi11 分钟前
AI漫剧生产系统架构解析:从文本拆解到音视频合成
人工智能·音视频
长不大的杨瑞杰16 分钟前
OpenCode v2 会话可靠性实战:Snapshots、Compaction、Instructions 与 Formatters
人工智能·安全·云计算·软件工程·devops
乌恩大侠21 分钟前
从 AI-RAN 到 AI Agent:基于 NVIDIA Nemotron 构建 5G/6G 智能网络故障诊断系统
服务器·人工智能·5g·o-ru·ai-ran·电信大模型
TMedical25 分钟前
MedPlanning 截骨模拟矫形功能
c++·图像处理·人工智能·qt·健康医疗
u1301301 小时前
GitHub 热榜项目:日榜(2026-10-29)
人工智能·github
QXWZ_IA1 小时前
路面病害漏检的成因与识别率提升
人工智能
深蓝AI1 小时前
EMBRACE 攻击打穿五个 AI 编程工具的浏览器:本地开发机成了最大攻击面
人工智能·程序员
锋行天下2 小时前
Langgraph HITL 中断运行,用户输入后输出打字效果
人工智能·python
ZhengEnCi2 小时前
agent 为什么需要自进化:从静态模型到经验驱动的持续成长
人工智能