-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencv.cpp
229 lines (177 loc) · 6.78 KB
/
opencv.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "opencv.h"
#include <QDebug>
namespace opencv
{
void flip(cv::InputArray src, cv::OutputArray dst, int flip_mode)
{
CV_Assert(flip_mode >= 0 && flip_mode < src.dims());
if (src.dims() <= 2)
{
cv::flip(src, dst, flip_mode);
return;
}
else if (src.empty())
{
return;
}
cv::Mat srcMat = src.getMat();
int type = srcMat.type();
dst.create(srcMat.dims, srcMat.size.p, type);
cv::Mat dstMat = dst.getMat();
if (srcMat.size.p[flip_mode] == 1)
{
src.copyTo(dst);
return;
}
const uchar *srcFront = srcMat.ptr();
const uchar *srcBack;
uchar *dstFront = dstMat.ptr();
uchar *dstBack;
const size_t stepAbove = (flip_mode == srcMat.dims - 1) ? 1 : srcMat.step[flip_mode + 1];
const size_t stepAt = srcMat.step[flip_mode];
const size_t stepBelow = (flip_mode == 0) ? 1 : srcMat.step[flip_mode - 1];
//for (int i = 0; i < srcMat.dims; ++i)
// qInfo() << "Dim " << i << ": Size=" << srcMat.size[i] << " Step=" << srcMat.step[i];
const int flipCount = (srcMat.size.p[flip_mode] + 1) / 2;
int npages = 1;
for (int i = 0; i < flip_mode; ++i)
npages *= srcMat.size.p[i];
const size_t pageInc = flipCount * stepAt;
for (int i = 0; i < npages; ++i, srcFront += pageInc, srcBack += pageInc,
dstFront += pageInc, dstBack += pageInc)
{
srcBack = srcFront + (srcMat.size.p[flip_mode] - 1) * stepAt;
dstBack = dstFront + (dstMat.size.p[flip_mode] - 1) * stepAt;
for (int j = 0; j < flipCount; ++j, srcFront += stepAt, srcBack -= stepAt,
dstFront += stepAt, dstBack -= stepAt)
{
int k = 0;
if (((size_t)srcFront | (size_t)dstFront | (size_t)srcBack | (size_t)dstBack) % sizeof(int) == 0)
{
for ( ; k <= (int)stepAt - 16; k += 16)
{
int t0 = ((int *)(srcFront + k))[0];
int t1 = ((int *)(srcBack + k))[0];
((int *)(dstFront + k))[0] = t1;
((int *)(dstBack + k))[0] = t0;
t0 = ((int *)(srcFront + k))[1];
t1 = ((int *)(srcBack + k))[1];
((int *)(dstFront + k))[1] = t1;
((int *)(dstBack + k))[1] = t0;
t0 = ((int *)(srcFront + k))[2];
t1 = ((int *)(srcBack + k))[2];
((int *)(dstFront + k))[2] = t1;
((int *)(dstBack + k))[2] = t0;
t0 = ((int *)(srcFront + k))[3];
t1 = ((int *)(srcBack + k))[3];
((int *)(dstFront + k))[3] = t1;
((int *)(dstBack + k))[3] = t0;
}
for ( ; k <= (int)stepAt - 4; k += 4)
{
int t0 = ((int *)(srcFront + k))[0];
int t1 = ((int *)(srcBack + k))[0];
((int *)(dstFront + k))[0] = t1;
((int *)(dstBack + k))[0] = t0;
}
}
for ( ; k < stepAt; k++)
{
uchar t0 = srcFront[k];
uchar t1 = srcBack[k];
dstFront[k] = t1;
dstBack[k] = t0;
}
}
}
}
template<typename T> static void
FindNonZeroFunc_(const T* src, size_t *idx_ptr, int len, size_t startIdx)
{
for(int i = 0; i < len; i++)
{
T val = src[i];
if (val != (T)0)
*idx_ptr++ = startIdx + i;
}
}
static void FindNonZeroFunc_8u(const uchar* src, size_t *idx_ptr, int len, size_t startidx)
{ FindNonZeroFunc_(src, idx_ptr, len, startidx ); }
static void FindNonZeroFunc_8s(const schar* src, size_t *idx_ptr, int len, size_t startidx)
{ FindNonZeroFunc_(src, idx_ptr, len, startidx ); }
static void FindNonZeroFunc_16u(const ushort* src, size_t *idx_ptr, int len, size_t startidx)
{ FindNonZeroFunc_(src, idx_ptr, len, startidx ); }
static void FindNonZeroFunc_16s(const short* src, size_t *idx_ptr, int len, size_t startidx)
{ FindNonZeroFunc_(src, idx_ptr, len, startidx ); }
static void FindNonZeroFunc_32s(const int* src, size_t *idx_ptr, int len, size_t startidx)
{ FindNonZeroFunc_(src, idx_ptr, len, startidx ); }
static void FindNonZeroFunc_32f(const float* src, size_t *idx_ptr, int len, size_t startidx)
{ FindNonZeroFunc_(src, idx_ptr, len, startidx ); }
static void FindNonZeroFunc_64f(const double* src, size_t *idx_ptr, int len, size_t startidx)
{ FindNonZeroFunc_(src, idx_ptr, len, startidx ); }
typedef void (*FindNonZeroFunc)(const uchar*, size_t*, int, size_t);
static FindNonZeroFunc getFindNonZeroTab(int depth)
{
static FindNonZeroFunc findNonZeroTab[] =
{
// Use GET_OPTIMIZED macro if I submit code to OpenCV
(FindNonZeroFunc)(FindNonZeroFunc_8u), (FindNonZeroFunc)(FindNonZeroFunc_8s),
(FindNonZeroFunc)(FindNonZeroFunc_16u), (FindNonZeroFunc)(FindNonZeroFunc_16s),
(FindNonZeroFunc)(FindNonZeroFunc_32s),
(FindNonZeroFunc)(FindNonZeroFunc_32f), (FindNonZeroFunc)(FindNonZeroFunc_64f),
0
};
return findNonZeroTab[depth];
}
// ALREADY DEFINED IN stat.cpp for OpenCV! If I submit this code into OpenCV, remove this code
static void ofs2idx(const cv::Mat& a, size_t ofs, int* idx)
{
int i, d = a.dims;
if( ofs > 0 )
{
ofs--;
for( i = d-1; i >= 0; i-- )
{
int sz = a.size[i];
idx[i] = (int)(ofs % sz);
ofs /= sz;
}
}
else
{
for( i = d-1; i >= 0; i-- )
idx[i] = -1;
}
}
void findNonZero(cv::InputArray _src, cv::OutputArray _idx)
{
int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
cv::Mat src = _src.getMat();
int n = cv::countNonZero(src);
if(n == 0)
{
_idx.release();
return;
}
if(_idx.kind() == cv::_InputArray::MAT && !_idx.getMatRef().isContinuous())
_idx.release();
_idx.create(n, 1, CV_MAKETYPE(CV_32S, src.dims)); // Number of channels depends on number of dims of cv::Mat
cv::Mat idx = _idx.getMat();
CV_Assert(idx.isContinuous());
FindNonZeroFunc func = getFindNonZeroTab(depth);
CV_Assert( func != 0 );
const cv::Mat* arrays[] = {&src, 0};
uchar* ptrs[1];
cv::NAryMatIterator it(arrays, ptrs);
int planeSize = (int)it.size*cn;
size_t startidx = 1;
// More efficient way to do this? Is allocating the best?
size_t *ofs = new size_t[n];
for( size_t i = 0; i < it.nplanes; i++, ++it, startidx += planeSize )
func( ptrs[0], ofs, planeSize, startidx );
int *idx_ptr = reinterpret_cast<int *>(idx.ptr());
for (int i = 0; i < n; ++i, idx_ptr += src.dims)
ofs2idx(src, ofs[i], idx_ptr);
delete ofs;
}
}