opencv:提取某些列或某些行最大值、最小值

     最近把一个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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值