-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmat_mwarray_convert.cpp
More file actions
75 lines (70 loc) · 1.87 KB
/
Copy pathmat_mwarray_convert.cpp
File metadata and controls
75 lines (70 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "mat_mwarray_convert.h"
mwArray mat2mwArray(const cv::Mat imgSrc)
{
cv::Mat imgSrcs = imgSrc.clone();
int h, w, c;
h = imgSrcs.rows;
w = imgSrcs.cols;
c = imgSrcs.channels();
if(c == 1) // gray
{
mwSize dims[2] = {w, h};
mwArray pMat(2,dims,mxDOUBLE_CLASS);
UINT8_T* x = (UINT8_T*)imgSrcs.data;
pMat.SetData(x, h*w);
return pMat;
}
else if(c == 3)
{
mwSize dims[3] = {h, w, c};
mwArray pMat(3, dims, mxUINT8_CLASS);
for (int i = 0; i < h; ++i)
{
for (int j = 0; j < w; ++j)
{
pMat(i+1,j+1,1) = (UINT8_T)imgSrcs.ptr<uchar>(i)[j+2];
pMat(i+1,j+1,2) = (UINT8_T)imgSrcs.ptr<uchar>(i)[j+1];
pMat(i+1,j+1,3) = (UINT8_T)imgSrcs.ptr<uchar>(i)[j+0];
}
}
return pMat;
}
}
cv::Mat mwArray2mat(const mwArray data)
{
mwArray dims = data.GetDimensions();
int n = data.NumberOfDimensions();
int M = dims(1);
int N = dims(2);
if (n == 2)
{
int h = M;
int w = N;
cv::Mat image(h, w, CV_8UC1);
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
int index = data(i+1,j+1);
image.row(i).col(j).data[0] = index;
}
}
return image;
}
else if (n == 3)
{
int h = M;
int w = N;
cv::Mat image(h, w, CV_8UC3);
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
(UINT8_T)imgSrcs.ptr<uchar>(i)[j+0]; = (int)data.Get(3,i+1,j+1,3);
(UINT8_T)imgSrcs.ptr<uchar>(i)[j+1]; = (int)data.Get(3,i+1,j+1,2);
(UINT8_T)imgSrcs.ptr<uchar>(i)[j+2]; = (int)data.Get(3,i+1,j+1,1);
}
}
return image;
}
}