- 操作系統:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 編程語言:C++11
算法描述
addC 函數將給定的標量值加到給定矩陣的每個元素上。該功能可以用矩陣表達式替換:
dst = src1 + c \texttt{dst} = \texttt{src1} + \texttt{c} dst=src1+c
輸出矩陣的深度由 ddepth 參數決定。如果 ddepth 設置為默認值 -1,則輸出矩陣將具有與輸入矩陣相同的深度。 輸入矩陣可以是單通道或多個通道的矩陣。輸出矩陣必須與輸入矩陣具有相同的尺寸和通道數。
支持的矩陣數據類型包括:CV_8UC1 CV_8UC3 CV_16UC1 CV_16SC1 CV_32FC1
注意:
函數的文本ID是 “org.opencv.core.math.addC”
函數原型
GMat cv::gapi::addC
(const GMat & src1,const GScalar & c,int ddepth = -1
)
參數
- 參數src1:第一個輸入矩陣。
- 參數c:要加到輸入矩陣上的標量值。
- 參數ddepth:輸出矩陣的可選深度。
代碼示例
#include <opencv2/gapi.hpp>
#include <opencv2/gapi/core.hpp> // 包含G-API核心功能
#include <opencv2/opencv.hpp>int main()
{// 讀取輸入圖像cv::Mat img1 = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/stich1.png", cv::IMREAD_COLOR );if ( img1.empty() ){std::cerr << "無法加載圖像,請檢查路徑。" << std::endl;return -1;}// 定義標量值cv::Scalar scalar_value( 90, 90, 90 ); // BGR顏色空間中的標量值// 定義G-API圖中的輸入和輸出cv::GMat in1;auto out = cv::gapi::addC( in1, cv::GScalar( scalar_value ) ); // 使用默認深度// 創建一個計算圖cv::GComputation add_graph( cv::GIn( in1 ), cv::GOut( out ) );// 輸出矩陣cv::Mat result;// 編譯并執行計算圖add_graph.apply( img1, result, cv::GCompileArgs() );// 顯示結果cv::imshow( "原圖", img1 );cv::imshow( "Result", result );// 如果需要指定不同的輸出深度,可以這樣做:int ddepth = CV_32F; // 指定為32位浮點數auto out_with_ddepth = cv::gapi::addC( in1, cv::GScalar( scalar_value ), ddepth );// 創建另一個計算圖cv::GComputation add_graph_with_ddepth( cv::GIn( in1 ), cv::GOut( out_with_ddepth ) );// 輸出矩陣(這次是浮點型)cv::Mat result_float;// 編譯并執行計算圖add_graph_with_ddepth.apply( img1, result_float, cv::GCompileArgs() );// 轉換回8位圖像以便顯示cv::Mat result_converted;result_float.convertTo( result_converted, CV_8U );cv::imshow( "Result with specified depth", result_converted );cv::waitKey( 0 );return 0;
}