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]
相关推荐
双鱼大猫13 小时前
opencode 吃掉 100G 空间:Agent 会话数据要分级清理
人工智能·agent·ai编程
用户58333398166813 小时前
RPA 流程的异常处理设计:重试、降级与告警,生产环境实战指南
人工智能
sanxing三省14 小时前
DeepSeek Harness 到底是什么?标题里最重的词其实不是「DeepSeek」
前端·人工智能·deepseek
合尘猫15 小时前
用 6GB 显卡搭短视频产线:MiniMax H3 + ffmpeg 全流程实录(55 秒片 / 40 分钟)
人工智能·音视频
ZONGYUAN-ROOT15 小时前
【自愈治理】自愈式数据管道:大规模公开数据采集的治理方法
人工智能
Starry-sky(jing)15 小时前
Hermes Agent 升级 v0.23.2:先辨更新渠道,再看 CLI 退出残留修复
人工智能·python·ai agent
oil欧哟15 小时前
AI Agent Skill 入门指南:文件夹本质、渐进式加载与挑选方法
人工智能·ai编程
百度Geek说16 小时前
从代码生成到需求交付:DarkFactory 的工程实践
人工智能
Baihai_IDP16 小时前
使用场景不同,模型“推理速度慢”的原因也不同
人工智能·面试·llm