天天看點

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,轉載請注明出處。