天天看点

opencv vector matlab,2015-08-25-OpenCV for Matlab User (3) - Matlab 函数的对应 OpenCV / C++ 实现...

0. OpenCV 中怎么读取 MATLAB 的 .mat 文件?

这一篇博文主要是记录一些Matlab 函数对应的 OpenCV / C++ 实现,相当于一个查找表(Look-Up Table)。发现在

StackOverflow 上还有一个类似的工作,Examples of Matlab to OpenCV conversions。

1. OpenCV 中怎么读取 MATLAB 的 .mat 文件?

Stackoverflow 上有个问题就是回答这个的,[Converting a .mat file from MATLAB into cv::Mat matrix in OpenCV][Converting a .mat file from MATLAB into cv::Mat matrix in OpenCV]。在上面这个回答的基础上,我稍微改进了一下,使其可以根据输入的数据类型,自适应地调整写入到 yml 文件的数据类型,以方便后面 cv::Mat 类型的数据访问。由于篇幅太长,我将其写在了另外一篇文章里:OpenCV 读取 Matlab .mat 文件的方法。

2. MATLAB 里的 sum,OpenCV 有什么对应函数?

在 MATLAB 中,对于一个矩阵 img,我们一般用 3 中 sum 的用法:整个矩阵的和 sum(img(:)), 矩阵的每列之和 sum(img, 1),矩阵的每行之和 sum(img, 2)。

MATLAB 整个矩阵的和 sum(img(:)) 可以用 OpenCV 中的 cv::sum 函数来替代,具体用法如下:

cv::Scalar imgScalar = cv::sum(img);

int imgSum = imgScalar[0];

MATLAB 中矩阵的每列之和 sum(img, 1),矩阵的每行之和 sum(img, 2) 可以用 OpenCV 中的 cv::reduce 函数来替代,具体用法如下:

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 中队每行或者每列求平均 mean,求最大值 max,求最小值 min 也可以采用 cv::reduce 函数来, 具体的 cv::reduce 函数用法如下:

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 defined 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()).

3. MATLAB 里的 sort,OpenCV 有什么对应函数?

在 MATLAB 中使用 sort 函数来对矩阵\向量排序很方便,如下所示:

haha = magic(5)

sort(haha, 1, 'ascend'); % 1 for sort every column

% 2 for sort every row

% 'ascend' or 'descend'

在 OpenCV 中,我们同样可以用 cv::sort 来实现相同功能,示例代码和解说如下:

#include

//#include

#include

//#include

int main(int argc, char **argv)

{

cv::Mat_ haha = cv::Mat::zeros(4,4,CV_64F);

cv::randu(haha, cv::Scalar(0), cv::Scalar(256));

std::cout<

cv::Mat_ wawa;

cv::sort(haha, wawa, CV_SORT_EVERY_COLUMN);

std::cout<

std::system("PAUSE");

return 0;

}

sort

Sorts each row or each column of a matrix.

C++: void sort(InputArray src, OutputArray dst, int flags)

Python: cv2.sort(src, flags [ , dst ] ) → dst

Parameters

src – input single-channel array.

dst – output array of the same size and type as src.

flags – operation flags, 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 flag 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 flag

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 flags 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.

4. OpenCV 如何将矩阵A的某一行/列赋值给矩阵B的某一行/列?

有时,我们需要将矩阵A的某一行/列赋值给矩阵B的某一行/列,这在 MATLAB 中非常简单,示例代码如下:

haha = zeros(4,5);

wawa = ones(4,5);

wawa(2,:) = haha(1,:)

那么在 OpenCV 中该怎么做呢?我们需要一起用到 row 或 col 函数和 copyTo,示例代码如下:

#include

#include

int main (int argc, const char * argv[]) {

cv::Mat_ haha = cv::Mat::zeros(4,5,CV_64F);

cv::Mat_ wawa = cv::Mat::ones(4,5,CV_64F);

std::cout<

std::cout<

haha.row(0).copyTo(wawa.row(1));

std::cout<

std::cout<

std::system("PAUSE");

return 0;

}

需要注意的是,在 copyTo 括号里面的才是被赋值的对象。

初写于 2015-08-25,未完待续。

首发于 Yimian Dai's Homepage,转载请注明出处。