# include <opencv2\core\core.hpp>
#include <iostream>
using namespace std;
using namespace cv;
template<class T>
void printMat(Mat matrix)
{
for (int r = 0; r < matrix.rows; r++)
{
for (int c = 0; c < matrix.cols; c++)
{
if (typeid(T) == typeid(uchar)) {
cout << (int)(matrix.at<T>(r, c)) << ",";
}
else
{
cout << (matrix.at<T>(r, c)) << ",";
}
}
cout << endl;
}
cout << endl;
}
template<class T>
void printMatT(Mat matrix)
{
for (int r = 0; r < matrix.rows; r++)
{
for (int c = 0; c < matrix.cols; c++)
{
cout << (matrix.at<T>(r, c)) << ",";
}
cout << endl;
}
cout << endl;
}
int main()
{
Mat src1 = (Mat_<uchar>(2, 3) << 23, 123, 90, 100, 250, 0);
Mat src2 = (Mat_<uchar>(2, 3) << 125, 150, 60, 100, 10, 40);
Mat dst = src1 + src2;
printMat<uchar>(dst);
float value = 100.00;
Mat dst1 = value + src1;
printMat<uchar>(dst1);
Mat src3 = (Mat_<float>(2, 3) << 125, 150, 60, 100, 10, 40);
Mat dst3;
add(src1, src3, dst3, Mat(), CV_64FC1);
printMat<double>(dst3);
Mat dst4 = src1 - src2;
printMat<uchar>(src1 - src2);
Mat dst5 = src1.mul(src2);
printMat<uchar>(dst5);
Mat dst6;
multiply(src1, src3, dst6, 1, CV_32FC1);
printMat<float>(dst6);
Mat dst7 = src2 / src1;
printMat<uchar>(dst7);
Mat dst8;
divide(src2, 10, dst8, 1, CV_32FC1);
printMat<float>(dst8);
Mat src4 = (Mat_<float>(2, 3) << 1, 2, 3, 4, 5, 6);
Mat src5 = (Mat_<float>(3, 2) << 6, 5, 4, 3, 2, 1);
Mat dst9 = src4 * src5;
printMat<float>(dst9);
Mat src6 = (Mat_<Vec2f>(2, 1) << Vec2f(1, 2), Vec2f(3, 4));
Mat src7 = (Mat_<Vec2f>(1, 2) << Vec2f(10, 20), Vec2f(5, 15));
Mat dst10 = src6 * src7;
printMatT<Vec2f>(dst10);
Mat dst11;
gemm(src6, src7, 1, NULL, 0, dst11, 0);
printMatT<Vec2f>(dst11);
Mat dst12, dst13;
exp(src3, dst12);
printMat<float>(dst12);
log(src3, dst13);
printMat<float>(dst13);
Mat src8 = (Mat_<uchar>(2, 2) << 4, 25, 16, 49);
Mat dst14;
pow(src8, 2, dst14);
printMat<uchar>(dst14);
Mat src9 = (Mat_<float>(2, 2) << 4, 25, 16, 49);
sqrt(src9, dst14);
printMat<float>(dst14);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】博客园2025新款「AI繁忙」系列T恤上架,前往周边小店选购
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步