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]
相关推荐
Derrick__12 分钟前
LangChain基础实战手记:如何给大模型装上“大脑(记忆)”和“双手(工具)”?
人工智能·python·langchain·个人开发
RSTJ_16258 分钟前
PYTHON+AI LLM DAY FOURTY-THREE
开发语言·人工智能·python
Volunteer Technology9 分钟前
SpringAI(二)Models 模型介绍
开发语言·人工智能·python
霸道流氓气质13 分钟前
Spring AI 实战:Ollama 本地模型工具调用与 Prompt 模板全解析
人工智能·spring·prompt
俊哥V15 分钟前
每日 AI 研究简报 · 2026-05-12
人工智能·ai
鸿怡ICsocketgirl18 分钟前
芯片老化座有哪些应用场景?-半导体制造
人工智能·python·制造
墨染天姬20 分钟前
【AI】comfy UI详解
人工智能·ui
SelectDB20 分钟前
强行拍平?全表扫描? AI Agent 动态 JSON 的观测分析
数据库·人工智能·数据分析
点PY25 分钟前
Weaving Context Across Images论文精读
人工智能
挨踢学霸25 分钟前
MsgHelper 5.0 全新发布:微信群发+微信群发群双引擎,按标签群发/定时群发/多于500人群发,数据本地化守护企业隐私安全
人工智能·安全·微信·自动化