OpenCV 图形API(67)图像与通道拼接函数-----水平拼接(横向连接)两个输入矩阵(GMat 类型)函数concatHor()

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

算法描述

该函数用于水平拼接两个 GMat 矩阵,要求输入矩阵的行数必须一致:

cpp 复制代码
GMat A = { 1, 4,
           2, 5,
           3, 6 };
GMat B = { 7, 10,
           8, 11,
           9, 12 };
GMat C = gapi::concatHor(A, B);
//C:
//[1, 4, 7, 10;
// 2, 5, 8, 11;
// 3, 6, 9, 12]

输出矩阵的行数和数据类型必须与 src1 和 src2 相同,其列数为 src1 和 src2 列数之和。支持的矩阵数据类型包括:CV_8UC1、CV_8UC3、CV_16UC1、CV_16SC1、CV_32FC1。

注意

该函数的文本标识符为 "org.opencv.imgproc.transform.concatHor"。

参数

  • 参数 src1:第一个输入矩阵(参与水平拼接)。

  • 参数 src2:第二个输入矩阵(参与水平拼接)。

返回值

返回一个新的 GMat,表示水平拼接后的结果。

代码示例

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

int main()
{
    // 读取图像(确保尺寸和类型匹配)
    cv::Mat mat1 = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/stich1.png", cv::IMREAD_COLOR );
    cv::Mat mat2 = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/stich2.png", cv::IMREAD_COLOR );

    if ( mat1.empty() || mat2.empty() )
    {
        std::cerr << "无法加载图像!" << std::endl;
        return -1;
    }

    // 强制统一尺寸和类型(G-API不会自动调整)
    if ( mat1.rows != mat2.rows )
    {
        cv::resize( mat2, mat2, cv::Size( mat2.cols, mat1.rows ) );
    }
    if ( mat1.type() != mat2.type() )
    {
        mat2.convertTo( mat2, mat1.type() );
    }

    // 构建G-API计算图
    cv::GMat in1, in2;
    cv::GMat out = cv::gapi::concatHor( in1, in2 );
    cv::GComputation comp( cv::GIn( in1, in2 ), cv::GOut( out ) );

    // 执行计算图
    cv::Mat result;
    comp.apply( cv::gin( mat1, mat2 ), cv::gout( result ) );

    cv::imshow( "G-API拼接结果", result );
    cv::waitKey( 0 );
    return 0;
}

运行结果

相关推荐
Shawn_Shawn5 小时前
人工智能入门概念介绍
人工智能
极限实验室5 小时前
程序员爆哭!我们让 COCO AI 接管 GitLab 审查后,团队直接起飞:连 CTO 都说“这玩意儿比人靠谱多了
人工智能·gitlab
Maynor9967 小时前
Z-Image: 100% Free AI Image Generator
人工智能
爬点儿啥7 小时前
[Ai Agent] 10 MCP基础:快速编写你自己的MCP服务器(Server)
人工智能·ai·langchain·agent·transport·mcp
张人玉7 小时前
百度 AI 图像识别 WinForms 应用代码分析笔记
人工智能·笔记·百度
测试人社区-小明8 小时前
智能弹性伸缩算法在测试环境中的实践与验证
人工智能·测试工具·算法·机器学习·金融·机器人·量子计算
Spring AI学习8 小时前
Spring AI深度解析(9/50):可观测性与监控体系实战
java·人工智能·spring
罗西的思考8 小时前
【Agent】MemOS 源码笔记---(5)---记忆分类
人工智能·深度学习·算法
dajun1811234569 小时前
反 AI 生成技术兴起:如何识别与过滤海量的 AI 伪造内容?
人工智能
人邮异步社区9 小时前
PRML为何是机器学习的经典书籍中的经典?
人工智能·机器学习