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

目录:
0. 前言
1. OpenCV 中怎么读取 MATLAB 的 .mat 文件?
2. MATLAB 里的 sum,OpenCV 有什么对应函数?
3. MATLAB 里的 sort,OpenCV 有什么对应函数?
4. OpenCV 如何将矩阵A的某一行/列赋值给矩阵B的某一行/列?

<div id="Section0">0. OpenCV 中怎么读取 MATLAB 的 .mat 文件?</div>

这一篇博文主要是记录一些Matlab 函数对应的 OpenCV / C++ 实现,相当于一个查找表(Look-Up Table)。发现在
StackOverflow 上还有一个类似的工作,Examples of Matlab to OpenCV conversions

<div id="Section1">1. OpenCV 中怎么读取 MATLAB 的 .mat 文件?</div>

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 文件的方法

<div id="Section2">2. MATLAB 里的 sum,OpenCV 有什么对应函数?</div>

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

<div id="Section3">3. MATLAB 里的 sort,OpenCV 有什么对应函数?</div>

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

<div id="Section4">4. OpenCV 如何将矩阵A的某一行/列赋值给矩阵B的某一行/列?</div>

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

haha = zeros(4,5);
wawa = ones(4,5);

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

那么在 OpenCV 中该怎么做呢?我们需要一起用到 rowcol 函数和 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 括号里面的才是被赋值的对象。
初写于 2015-08-25,未完待续。
首发于 Yimian Dai's Homepage,转载请注明出处。


[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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容