目錄:
0. 前言
1. OpenCV 中怎么讀取 MATLAB 的 .mat 文件?
2. MATLAB 里的 sum,OpenCV 有什么對(duì)應(yīng)函數(shù)?
3. MATLAB 里的 sort,OpenCV 有什么對(duì)應(yīng)函數(shù)?
4. OpenCV 如何將矩陣A的某一行/列賦值給矩陣B的某一行/列?
<div id="Section0">0. OpenCV 中怎么讀取 MATLAB 的 .mat 文件?</div>
這一篇博文主要是記錄一些Matlab 函數(shù)對(duì)應(yīng)的 OpenCV / C++ 實(shí)現(xiàn),相當(dāng)于一個(gè)查找表(Look-Up Table)。發(fā)現(xiàn)在
StackOverflow 上還有一個(gè)類似的工作,Examples of Matlab to OpenCV conversions。
<div id="Section1">1. OpenCV 中怎么讀取 MATLAB 的 .mat 文件?</div>
Stackoverflow 上有個(gè)問(wèn)題就是回答這個(gè)的,[Converting a .mat file from MATLAB into cv::Mat matrix in OpenCV][Converting a .mat file from MATLAB into cv::Mat matrix in OpenCV]。在上面這個(gè)回答的基礎(chǔ)上,我稍微改進(jìn)了一下,使其可以根據(jù)輸入的數(shù)據(jù)類型,自適應(yīng)地調(diào)整寫入到 yml 文件的數(shù)據(jù)類型,以方便后面 cv::Mat 類型的數(shù)據(jù)訪問(wèn)。由于篇幅太長(zhǎng),我將其寫在了另外一篇文章里:OpenCV 讀取 Matlab .mat 文件的方法。
<div id="Section2">2. MATLAB 里的 sum,OpenCV 有什么對(duì)應(yīng)函數(shù)?</div>
在 MATLAB 中,對(duì)于一個(gè)矩陣 img,我們一般用 3 中 sum 的用法:整個(gè)矩陣的和 sum(img(:)), 矩陣的每列之和 sum(img, 1),矩陣的每行之和 sum(img, 2)。
- MATLAB 整個(gè)矩陣的和
sum(img(:))可以用 OpenCV 中的cv::sum函數(shù)來(lái)替代,具體用法如下:
cv::Scalar imgScalar = cv::sum(img);
int imgSum = imgScalar[0];
- MATLAB 中矩陣的每列之和
sum(img, 1),矩陣的每行之和sum(img, 2)可以用 OpenCV 中的cv::reduce函數(shù)來(lái)替代,具體用法如下:
cv::Mat colSumArr;
cv::Mat rowSumVec;
cv::reduce(img, colSumArr, 0, CV_REDUCE_SUM, CV_32SC1); // sum(img, 1)
cv::reduce(img, rowSumVec, 1, CV_REDUCE_SUM, CV_32SC1); // sum(img, 2)
除了求和 sum 外,MATLAB 中隊(duì)每行或者每列求平均 mean,求最大值 max,求最小值 min 也可以采用 cv::reduce 函數(shù)來(lái), 具體的 cv::reduce 函數(shù)用法如下:
reduce:Reduces a matrix to a vector.
void reduce(InputArray src, OutputArray dst, int dim, int rtype, int dtype=-1 )
Parameters
src – input 2D matrix.
dst – output vector. Its size and type is de?ned by dim and dtype parameters.
dim – dimension index along which the matrix is reduced. 0 means that the matrix is reduced to a single row. 1 means that the matrix is reduced to a single column.
rtype – reduction operation that could be one of the following:
– CV_REDUCE_SUM: the output is the sum of all rows/columns of the matrix.
– CV_REDUCE_AVG: the output is the mean vector of all rows/columns of the matrix.
– CV_REDUCE_MAX: the output is the maximum (column/row-wise) of all
rows/columns of the matrix.
– CV_REDUCE_MIN: the output is the minimum (column/row-wise) of all rows/columns
of the matrix.
dtype – when negative, the output vector will have the same type as the input matrix, other-
wise, its type will be CV_MAKE_TYPE(CV_MAT_DEPTH(dtype), src.channels()).
<div id="Section3">3. MATLAB 里的 sort,OpenCV 有什么對(duì)應(yīng)函數(shù)?</div>
在 MATLAB 中使用 sort 函數(shù)來(lái)對(duì)矩陣\向量排序很方便,如下所示:
haha = magic(5)
sort(haha, 1, 'ascend'); % 1 for sort every column
% 2 for sort every row
% 'ascend' or 'descend'
在 OpenCV 中,我們同樣可以用 cv::sort 來(lái)實(shí)現(xiàn)相同功能,示例代碼和解說(shuō)如下:
#include <opencv2/opencv.hpp>
//#include <opencv2/contrib/contrib.hpp>
#include <iostream>
//#include <map>
int main(int argc, char **argv)
{
cv::Mat_<double> haha = cv::Mat::zeros(4,4,CV_64F);
cv::randu(haha, cv::Scalar(0), cv::Scalar(256));
std::cout<<"haha = "<<std::endl<<haha<<std::endl;
cv::Mat_<double> wawa;
cv::sort(haha, wawa, CV_SORT_EVERY_COLUMN);
std::cout<<"wawa = "<<std::endl<<wawa<<std::endl;
std::system("PAUSE");
return 0;
}
sort
Sorts each row or each column of a matrix.
C++: void sort(InputArray src, OutputArray dst, int ?ags)
Python: cv2.sort(src, ?ags [ , dst ] ) → dst
Parameters
src – input single-channel array.
dst – output array of the same size and type as src.
?ags – operation ?ags, a combination of the following values:
– CV_SORT_EVERY_ROW each matrix row is sorted independently.
– CV_SORT_EVERY_COLUMN each matrix column is sorted independently; this ?ag and the previous one are mutually exclusive.
– CV_SORT_ASCENDING each matrix row is sorted in the ascending order.
– CV_SORT_DESCENDING each matrix row is sorted in the descending order; this ?ag
and the previous one are also mutually exclusive.
The function sort sorts each matrix row or each matrix column in ascending or descending order. So you should pass
two operation ?ags to get desired behaviour. If you want to sort matrix rows or columns lexicographically, you can
use STL std::sort generic function with the proper comparison predicate.
<div id="Section4">4. OpenCV 如何將矩陣A的某一行/列賦值給矩陣B的某一行/列?</div>
有時(shí),我們需要將矩陣A的某一行/列賦值給矩陣B的某一行/列,這在 MATLAB 中非常簡(jiǎn)單,示例代碼如下:
haha = zeros(4,5);
wawa = ones(4,5);
wawa(2,:) = haha(1,:)
那么在 OpenCV 中該怎么做呢?我們需要一起用到 row 或 col 函數(shù)和 copyTo,示例代碼如下:
#include <opencv2/opencv.hpp>
#include <iostream>
int main (int argc, const char * argv[]) {
cv::Mat_<double> haha = cv::Mat::zeros(4,5,CV_64F);
cv::Mat_<double> wawa = cv::Mat::ones(4,5,CV_64F);
std::cout<<"haha = "<<std::endl<<haha<<std::endl;
std::cout<<"wawa = "<<std::endl<<wawa<<std::endl;
haha.row(0).copyTo(wawa.row(1));
std::cout<<"haha = "<<std::endl<<haha<<std::endl;
std::cout<<"wawa = "<<std::endl<<wawa<<std::endl;
std::system("PAUSE");
return 0;
}
需要注意的是,在 copyTo 括號(hào)里面的才是被賦值的對(duì)象。
初寫于 2015-08-25,未完待續(xù)。
首發(fā)于 Yimian Dai's Homepage,轉(zhuǎn)載請(qǐng)注明出處。
[Converting a .mat file from MATLAB into cv::Mat matrix in OpenCV]: http://stackoverflow.com/questions/11550021/converting-a-mat-file-from-matlab-into-cvmat-matrix-in-opencv