目录
下面例子中的向量其实是1行n列的矩阵
1行n列的向量
逐元素-加法
例子1
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32SC1);
vector<int> x = {-1,1,2};
for (int i=0; i<x.size(); i++)
{ A.at<int>(0,i) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
A = A + 1;
for (int i=0; i<x.size(); i++)
{ cout << A.at<int>(0,i) << ", "; }
cout << endl;
}
rows:1, cols:3, channels:1
0, 2, 3,
例子2
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32SC1);
vector<int> x = {-1,1,2};
for (int i=0; i<x.size(); i++)
{ A.at<int>(0,i) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
A = A + A;
for (int i=0; i<x.size(); i++)
{ cout << A.at<int>(0,i) << ", "; }
cout << endl;
}
rows:1, cols:3, channels:1
-2, 2, 4,
乘1个数
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32SC1);
vector<int> x = {-1,1,2};
for (int i=0; i<x.size(); i++)
{ A.at<int>(0,i) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
A = A * 3;
for (int i=0; i<x.size(); i++)
{ cout << A.at<int>(0,i) << ", "; }
cout << endl;
}
rows:1, cols:3, channels:1
-3, 3, 6,
点乘
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32SC1);
vector<int> x = {-1,1,2};
for (int i=0; i<x.size(); i++)
{ A.at<int>(0,i) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
auto result = A.dot(A);
cout << result << endl;
for (int i=0; i<x.size(); i++)
{ cout << A.at<int>(0,i) << ", "; }
cout << endl;
}
rows:1, cols:3, channels:1
6
-1, 1, 2,
转置
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32SC1);
vector<int> x = {-1,1,2};
for (int i=0; i<x.size(); i++)
{ A.at<int>(0,i) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
Mat A_trans = A.t();
cout << "rows:" << A_trans.rows << ", cols:" << A_trans.cols << ", channels:" << A_trans.channels() << endl;
for (int i=0; i<x.size(); i++)
{ cout << A_trans.at<int>(i,0) << ", "; }
cout << endl;
}
rows:1, cols:3, channels:1
rows:3, cols:1, channels:1
-1, 1, 2,
统计-非0元素个数
int类型
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32SC1);
vector<int> x = {0,0,2};
for (int i=0; i<x.size(); i++)
{ A.at<int>(0,i) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
cout << cv::countNonZero(A) << endl;
}
rows:1, cols:3, channels:1
1
float类型
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {0,0.00001,2};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
cout << cv::countNonZero(A) << endl;
}
rows:1, cols:3, channels:1
2
均值/标准差
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {0,0.00001,2};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
Mat mean,stddev;
cv::meanStdDev(A,mean,stddev);
cout << "rows:" << mean.rows << ", cols:" << mean.cols << ", channels:" << mean.channels() << endl;
cout << mean << endl;
cout << mean.at<float>(0,0) << endl;
cout << mean.at<double>(0,0) << endl;
double d = 0.666;
float f = d;
cout << d << ", " << f << endl;
}
rows:1, cols:3, channels:1
rows:1, cols:1, channels:1
[0.6666699999999157]
4.96427e+11
0.66667
0.666, 0.666
减均值
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,2,3};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
Mat mean,stddev;
cv::meanStdDev(A,mean,stddev);
cout << "rows:" << mean.rows << ", cols:" << mean.cols << ", channels:" << mean.channels() << endl;
cout << "rows:" << stddev.rows << ", cols:" << stddev.cols << ", channels:" << stddev.channels() << endl;
cout << "mean: "<< mean <<", stddev: " <<stddev << endl;
cout << A << endl;
Mat process;
process = A - mean;
cout << process << endl;
cout << A - mean.at<double>(0,0) << endl;
cout << A - 2 << endl;
}
除标准差
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,2,3};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
Mat mean,stddev;
cv::meanStdDev(A,mean,stddev);
cout << "rows:" << mean.rows << ", cols:" << mean.cols << ", channels:" << mean.channels() << endl;
cout << "rows:" << stddev.rows << ", cols:" << stddev.cols << ", channels:" << stddev.channels() << endl;
cout << "mean: "<< mean <<", stddev: " <<stddev << endl;
cout << A << endl;
Mat process;
process = (A - mean)/stddev;
cout << process << endl;
cout << (A - mean.at<double>(0,0))/stddev.at<double>(0,0) << endl;
cout << (A - 2)/0.81649658 << endl;
}
rows:1, cols:3, channels:1
rows:1, cols:1, channels:1
rows:1, cols:1, channels:1
mean: [2], stddev: [0.8164965809277257]
[1, 2, 3]
[-1.2247449, 0, 1.2247449]
[-1.2247449, 0, 1.224745]
[-1.2247449, 0, 1.224745]
排序
从小到大
std::sort
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,0.00001,2};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << A << endl;
std::sort(A.begin<float>(), A.end<float>());
cout << A << endl;
}
[1, 9.9999997e-06, 2]
[9.9999997e-06, 1, 2]
std::stable_sort
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,0.00001,2};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << A << endl;
std::stable_sort(A.begin<float>(), A.end<float>());
cout << A << endl;
}
[1, 9.9999997e-06, 2]
[9.9999997e-06, 1, 2]
从大到小
std::sort
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,0.00001,2};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << A << endl;
std::sort(A.begin<float>(), A.end<float>(),greater<float>());
cout << A << endl;
}
[1, 9.9999997e-06, 2]
[2, 1, 9.9999997e-06]
std::stable_sort
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,0.00001,2};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << A << endl;
std::stable_sort(A.begin<float>(), A.end<float>(),greater<float>());
cout << A << endl;
}
[1, 9.9999997e-06, 2]
[2, 1, 9.9999997e-06]
最值
-
https://blog.csdn.net/shyjhyp11/article/details/109486647
std
max值/位置
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,0.00001,9};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << A << endl;
auto maxValue = *max_element(A.begin<float>(), A.end<float>());
auto maxLoc = max_element(A.begin<float>(),A.end<float>()) - A.begin<float>();
cout << maxValue << endl;
cout << maxLoc << endl;
}
[1, 9.9999997e-06, 9]
9
2
min值/位置
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,0.00001,9};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << A << endl;
auto minValue = *min_element(A.begin<float>(), A.end<float>());
auto minLoc = min_element(A.begin<float>(),A.end<float>()) - A.begin<float>();
cout << minValue << endl;
cout << minLoc << endl;
}
[1, 9.9999997e-06, 9]
1e-05
1
cv::minMaxLoc
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,0.00001,9};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << A << endl;
double maxValue,minValue;
cv::Point maxIndex,minIndex;
cv::minMaxLoc(A,&minValue,&maxValue,&minIndex,&maxIndex);
cout << maxValue << ", " << maxIndex << endl;
cout << minValue << ", " << minIndex << endl;
}
[1, 9.9999997e-06, 9]
9, [2, 0]
1e-05, [1, 0]
Size
-
注意:size 与 size() 返回值的区别
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,0.00001,9};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << A << endl;
cout << "------------\n";
cout << A.size << endl;
cout << A.size() << endl;
}
[1, 9.9999997e-06, 9]
------------
1 x 3
[3 x 1]
dims
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,0.00001,9};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << A << endl;
cout << "------------\n";
cout << A.dims << endl;
}
[1, 9.9999997e-06, 9]
------------
2
Mat.data指针
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat A(1,3,CV_32FC1);
vector<float> x = {1,0.00001,9};
for (int i=0; i<x.size(); i++)
{ A.at<float>(0,i) = x[i]; }
cout << A << endl;
cout << "------------\n";
cout << (float*)A.data << endl;
cout << *((float*)A.data+2) << endl;
}
[1, 9.9999997e-06, 9]
------------
0x7f9d11913e00
9