OpenCV 图形API(68)图像与通道拼接函数------垂直拼接两个图像/矩阵的函数concatVert()
- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
对给定的矩阵执行垂直拼接。该函数将两个 GMat 矩阵(列数相同)垂直连接:
GMat A = { 1, 7,2, 8,3, 9 };
GMat B = { 4, 10,5, 11,6, 12 };
GMat C = gapi::concatVert(A, B);
//C:
//[1, 7;
// 2, 8;
// 3, 9;
// 4, 10;
// 5, 11;
// 6, 12]
输出矩阵要求:
- 列数和数据类型必须与 src1 和 src2 相同
- 行数为 src1 和 src2 行数之和
支持的矩阵数据类型:CV_8UC1、CV_8UC3、CV_16UC1、CV_16SC1、CV_32FC1
备注:
该函数的文本标识符为 “org.opencv.imgproc.transform.concatVert”
函数原型
GMat cv::gapi::concatVert
(const GMat & src1,const GMat & src2
)
参数
- 参数 src1:第一个输入矩阵(参与垂直拼接)
- 参数 src2:第二个输入矩阵(参与垂直拼接)
代码示例
#include <opencv2/gapi.hpp>
#include <opencv2/gapi/core.hpp>
#include <opencv2/opencv.hpp>int main()
{// 1. 读取输入图像(确保宽度和类型相同)cv::Mat mat1 = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/top.png", cv::IMREAD_COLOR ); // 假设是 200x300 CV_8UC3cv::Mat mat2 = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/bottom.png", cv::IMREAD_COLOR ); // 假设是 100x300 CV_8UC3if ( mat1.empty() || mat2.empty() ){std::cerr << "Error: Could not load images!" << std::endl;return -1;}// 2. 检查矩阵属性(调试用)std::cout << "mat1: " << mat1.rows << "x" << mat1.cols << ", type=" << mat1.type() << std::endl;std::cout << "mat2: " << mat2.rows << "x" << mat2.cols << ", type=" << mat2.type() << std::endl;// 3. 强制统一宽度(如果不同)if ( mat1.cols != mat2.cols ){cv::resize( mat2, mat2, cv::Size( mat1.cols, mat2.rows ) );std::cout << "Adjusted mat2 width to match mat1" << std::endl;}// 4. 构建G-API计算图cv::GMat in1, in2;cv::GMat out = cv::gapi::concatVert( in1, in2 ); // 垂直拼接cv::GComputation comp( cv::GIn( in1, in2 ), cv::GOut( out ) );// 5. 执行计算图cv::Mat result;comp.apply( cv::gin( mat1, mat2 ), cv::gout( result ) );// 6. 显示结果cv::imshow( "Vertical Concatenation", result );cv::waitKey( 0 );return 0;
}