如何将mwArray转成opencv里的Mat?? 50
1个回答
展开全部
你的mwArray是什么,Mat有个成员data,你把数据按要求组织好,赋到这里就好了。这是我一个工程了用到的,给你参考下
Mat matrix2Mat( const matrix<>& src,bool color=false)
{
unsigned long height =src.getSizex() ;
unsigned long width = src.getSizey();
Mat dst;
if(color)
{
dst=Mat(height,width,CV_8UC3);
for(unsigned long h=0; h<height; h++)
{
uchar *ptr = (uchar*)(dst.data+h*dst.step);
for(unsigned long w=0; w<width; w++)
{
ptr[w] =src(h,w);
}
}
}
else
{
dst=Mat(height,width,CV_8UC1);
for(unsigned long h=0; h<height; h++)
{
for(unsigned long w=0; w<width; w++)
{
dst.at<uchar>(h,w)= src(h,w);
}
}
}
return dst;
}
matrix<> to_matrix(const mxArray *a) {
unsigned long mrows = static_cast<unsigned long>(mxGetM(a));
unsigned long ncols = static_cast<unsigned long>(mxGetN(a));
double *data = mxGetPr(a);
matrix<> m(mrows, ncols);
for (unsigned long r = 0; r < mrows; r++) {
for (unsigned long c = 0; c < ncols; c++) {
m(r,c) = data[(c*mrows) + r];
}
}
return m;
}
反向
mxArray* to_mxArray(const matrix<>& m) {
unsigned long mrows = m.size(0);
unsigned long ncols = m.size(1);
mxArray *a = mxCreateDoubleMatrix(
static_cast<int>(mrows),
static_cast<int>(ncols),
mxREAL
);
double *data = mxGetPr(a);
for (unsigned long r = 0; r < mrows; r++) {
for (unsigned long c = 0; c < ncols; c++) {
data[(c*mrows) + r] = m(r,c);
}
}
return a;
}
Mat matrix2Mat( const matrix<>& src,bool color=false)
{
unsigned long height =src.getSizex() ;
unsigned long width = src.getSizey();
Mat dst;
if(color)
{
dst=Mat(height,width,CV_8UC3);
for(unsigned long h=0; h<height; h++)
{
uchar *ptr = (uchar*)(dst.data+h*dst.step);
for(unsigned long w=0; w<width; w++)
{
ptr[w] =src(h,w);
}
}
}
else
{
dst=Mat(height,width,CV_8UC1);
for(unsigned long h=0; h<height; h++)
{
for(unsigned long w=0; w<width; w++)
{
dst.at<uchar>(h,w)= src(h,w);
}
}
}
return dst;
}
matrix<> to_matrix(const mxArray *a) {
unsigned long mrows = static_cast<unsigned long>(mxGetM(a));
unsigned long ncols = static_cast<unsigned long>(mxGetN(a));
double *data = mxGetPr(a);
matrix<> m(mrows, ncols);
for (unsigned long r = 0; r < mrows; r++) {
for (unsigned long c = 0; c < ncols; c++) {
m(r,c) = data[(c*mrows) + r];
}
}
return m;
}
反向
mxArray* to_mxArray(const matrix<>& m) {
unsigned long mrows = m.size(0);
unsigned long ncols = m.size(1);
mxArray *a = mxCreateDoubleMatrix(
static_cast<int>(mrows),
static_cast<int>(ncols),
mxREAL
);
double *data = mxGetPr(a);
for (unsigned long r = 0; r < mrows; r++) {
for (unsigned long c = 0; c < ncols; c++) {
data[(c*mrows) + r] = m(r,c);
}
}
return a;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询