最近把一个MATLAB的代码转C++,然后MATLAB里一句这样的代码,发现用opencv里两个函数结合下可以实现,本来想自己写两个for来遍历的,这样我觉得还行,挺方便,分享一下。
% matlan code
[x,~]=find(img(:,i)==max(img(:,i)));
1、提取mat数组中的行或列
Mat A = (Mat_<float>(3, 3) << 1, -2, 0, -1, 3, 1, 0, -1, 1);
cout<< "all matrix:" << endl;
cout<< A<< endl;
Mat row = A.rowRange(0, 1).clone(); // 提取第0行
cout<< "Row range:" << endl;
cout<< row<< endl;
Mat col = A.colRange(1, 2).clone(); // 提取第1列
cout<< "Col range:" << endl;
cout<< col<< endl;
2、求行列的最大值,最小值
Mat A = (Mat_<float>(3, 3) << 1, -2, 0, -1, 3, 1, 0, -1, 1);
cout << "all matrix:" << endl;
cout << A << endl;
Mat row = A.rowRange(1, 3).clone(); // 提取第1、2行
cout << "Row range:" << endl;
cout << row << endl;
Mat cols = A.colRange(1, 2).clone(); // 提取第1列
cout << "Col range:" << endl;
cout << cols << endl;
double maxvalue=0, minvalue=0;
Point max_ind , min_ind;
/* minMaxLoc 求取最大值
Mat数组,最小值,最大值,最小值位置,最大值位置
*/
minMaxLoc(cols, &minvalue, &maxvalue, &min_ind, &max_ind);
cout << max_ind<<"max:" << maxvalue << " "<<min_ind<<" min:" << minvalue << endl;