目录
加/减/乘/除1个数
例子1
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
Mat B(3,2,CV_32FC1);
Mat AB;
vector<float> x = {-1,1,2};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i];
B.at<float>(i,0) = x[i];
B.at<float>(i,1) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
cout << "rows:" << B.rows << ", cols:" << B.cols << ", channels:" << B.channels() << endl;
AB = B + 0.5;
cout << "rows:" << AB.rows << ", cols:" << AB.cols << ", channels:" << AB.channels() << endl;
for (int i=0; i<AB.rows; i++)
{
for (int j=0; j<AB.cols; j++)
{
cout << AB.at<float>(i,j) << ", ";
}
cout << endl;
}
}
rows:1, cols:3, channels:1
rows:3, cols:2, channels:1
rows:3, cols:2, channels:1
-0.5, -0.5,
1.5, 1.5,
2.5, 2.5,
例子2
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
Mat B(3,2,CV_32FC1);
Mat AB;
vector<float> x = {-1,1,2};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i];
B.at<float>(i,0) = x[i];
B.at<float>(i,1) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
cout << "rows:" << B.rows << ", cols:" << B.cols << ", channels:" << B.channels() << endl;
AB = (B + 0.5) / 10;
cout << "rows:" << AB.rows << ", cols:" << AB.cols << ", channels:" << AB.channels() << endl;
for (int i=0; i<AB.rows; i++)
{
for (int j=0; j<AB.cols; j++)
{
cout << AB.at<float>(i,j) << ", ";
}
cout << endl;
}
}
rows:1, cols:3, channels:1
rows:3, cols:2, channels:1
rows:3, cols:2, channels:1
-0.05, -0.05,
0.15, 0.15,
0.25, 0.25,
例子3
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
Mat B(3,2,CV_32FC1);
Mat AB;
vector<float> x = {-1,1,2};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i];
B.at<float>(i,0) = x[i];
B.at<float>(i,1) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
cout << "rows:" << B.rows << ", cols:" << B.cols << ", channels:" << B.channels() << endl;
AB = (B + 0.5) / 10;
cout << "rows:" << AB.rows << ", cols:" << AB.cols << ", channels:" << AB.channels() << endl;
cout << A << endl;
cout << B << endl;
cout << AB << endl;
}
rows:1, cols:3, channels:1
rows:3, cols:2, channels:1
rows:3, cols:2, channels:1
[-1, 1, 2]
[-1, -1;
1, 1;
2, 2]
[-0.050000001, -0.050000001;
0.15000001, 0.15000001;
0.25, 0.25]
叉乘
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
Mat B(3,2,CV_32FC1);
Mat AB;
vector<float> x = {-1,1,2};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i];
B.at<float>(i,0) = x[i];
B.at<float>(i,1) = x[i]; }
AB = A * B;
cout << A << endl;
cout << B << endl;
cout << AB << endl;
}
[-1, 1, 2]
[-1, -1;
1, 1;
2, 2]
[6, 6]
转置
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat B(2,3,CV_32FC1);
vector<vector<float>> x = { {1,2,3},
{4,5,6}};
for (int i=0; i<x.size(); i++)
{
for (int j=0; j<x[0].size(); j++)
{
B.at<float>(i,j) = x[i][j];
}
}
Mat B_trans;
B_trans = B.t();
cout << B << endl;
cout << "-----------\n";
cout << B_trans << endl;
}
[1, 2, 3;
4, 5, 6]
-----------
[1, 4;
2, 5;
3, 6]
统计-非0元素个数
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat B(2,3,CV_32FC1);
vector<vector<float>> x = { {1,0.00,3},
{4,0.0000001,6}};
for (int i=0; i<x.size(); i++)
{
for (int j=0; j<x[0].size(); j++)
{ B.at<float>(i,j) = x[i][j]; }
}
cout << cv::countNonZero(B) << endl;
}
5
均值/标准差
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat B(2,3,CV_32FC1);
vector<vector<float>> x = { {1,2,3},
{4,5,6}};
for (int i=0; i<x.size(); i++)
{
for (int j=0; j<x[0].size(); j++)
{ B.at<float>(i,j) = x[i][j]; }
}
Mat mean, stddev;
cv::meanStdDev(B,mean,stddev);
cout << mean << endl;
cout << stddev << endl;
Mat process = (B - mean) / stddev;
cout << process << endl;
}
[3.5]
[1.707825127659933]
[-1.4638501, -0.87831008, -0.29277003;
0.29277003, 0.87831008, 1.4638501]
最值
cv::minMaxLoc
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat B(2,3,CV_32FC1);
vector<vector<float>> x = { {2,3,1},
{4,5,6}};
for (int i=0; i<x.size(); i++)
{
for (int j=0; j<x[0].size(); j++)
{ B.at<float>(i,j) = x[i][j]; }
}
cout << B << endl;
cout << "-------------\n";
double maxValue,minValue;
cv::Point maxIndex,minIndex;
cv::minMaxLoc(B,&minValue,&maxValue,&minIndex,&maxIndex);
cout << maxValue << ", " << maxIndex << endl;
cout << minValue << ", " << minIndex << endl;
}
[2, 3, 1;
4, 5, 6]
-------------
6, [2, 1]
1, [2, 0]