- 操作系統:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 編程語言:C++11
算法描述
該函數用于水平拼接兩個 GMat 矩陣,要求輸入矩陣的行數必須一致:
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,表示水平拼接后的結果。
代碼示例
#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;
}