2015-08-25-OpenCV for Matlab User (3) - Matlab 函數(shù)的對(duì)應(yīng) OpenCV / C++ 實(shí)現(xiàn)

目錄:
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 中該怎么做呢?我們需要一起用到 rowcol 函數(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 鑒于中文語(yǔ)境下,學(xué)習(xí) OpenCV 的資料其實(shí)稀少,不是主要講解已經(jīng)過(guò)時(shí)de 1.x 版內(nèi)容《學(xué)習(xí) OpenCV...
    YimianDai閱讀 7,183評(píng)論 2 34
  • 我是 OpenCV 乃至 C++ 的初學(xué)者,過(guò)去的兩個(gè)禮拜里剛把一個(gè) Matlab 程序轉(zhuǎn)化成使用了 OpenCV...
    YimianDai閱讀 2,116評(píng)論 0 2
  • 7350英里外的第三個(gè)月。 雖說(shuō)都過(guò)了三個(gè)月,每隔一段時(shí)間就總會(huì)有那么四五天,起床就會(huì)心情壓抑,坐在房間就會(huì)神游。...
    0_5T閱讀 277評(píng)論 0 0
  • “眾芳搖落獨(dú)暄妍,占盡風(fēng)情向小園?!?秋去冬來(lái),萬(wàn)物凋零,繁華落盡,該是怎樣的寂寞。她洗盡鉛華,低調(diào)含蓄,走過(guò)了盛...
    丁香與海閱讀 536評(píng)論 2 3
  • 假如天平兩端分別是“內(nèi)向”和“外向”,那我的這個(gè)天平顯然更多的是往“內(nèi)向”的一側(cè)傾斜。 很長(zhǎng)一段時(shí)間我都認(rèn)為自己是...
    鯊魚小姐閱讀 663評(píng)論 12 26

友情鏈接更多精彩內(nèi)容