-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTransform.h
627 lines (569 loc) · 16.1 KB
/
Transform.h
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/**
* @file Transform.h
* @brief 图像变换模板函数,可以跨平台.
* @details 包括:图像缩放、旋转、翻转函数;图像感兴趣区域、保存、直方图函数等
* @author 袁沅祥
* @email [email protected]
* @version V1.0
* @date 2016/10/4
* Copyleft (c), All rights reserved.
* @note 务必添加此包含"algorithm.h"文件,否则在Eclipse遇到
* error: 'sort' is not a member of 'std';
* 凡是以**二级指针作为参数,函数将修改此一级指针
*/
#include "DataTypes.h"
#include <fstream>
using namespace std;
#pragma once
#pragma warning(disable : 4244)
// 将图像写入文件的文件头大小(字节)
#define HEADER_SIZE 1024
// 放大图像:会修改pfHead及更新图像信息
template <typename Type> void ZoomImage(Type** pfHead, int &nWidth, int &nHeight, int &nRowlen, int nChannel, int NewWidth, int NewHeight);
// 限制图像最大边长,当超出时修改大小
template <typename Type> void LimitImage(Type** pfHead, int &nWidth, int &nHeight, int &nRowlen, int nChannel, int nMaxSize = 320);
// 获取截屏图像中的QR码矩形
template <typename Type> void GetQrRect(const Type* pHead, int nWidth, int nHeight, int nChannel, int nRowlen, RoiRect &cutAero);
// 上下翻转图像
template <typename Type> void ImageFlipV(Type* pHead, const int nWidth, const int nHeight, const int nRowlen);
// 左右翻转图像
template <typename Type> void ImageFlipH(Type* pHead, const int nWidth, const int nHeight, const int nRowlen);
// 转置图像
template <typename Type> void ImageTranspose(Type** pHead, const int nWidth, const int nHeight, const int nRowlen);
// 向右转置图像
template <typename Type> void ImageTransposeR(Type** pHead, const int nWidth, const int nHeight, const int nRowlen);
// 提取图像感兴趣区域
template <typename Type> Type* ImageROI(const Type* pHead, int &nWidth, int &nHeight, int &nRowlen, const RoiRect &roi = 0);
// 将图像以二进制形式写入文件
template <typename Type> BOOL ImageWrite(const char* fileName, const Type* pSrc, int nWidth, int nHeight, int nRowlen);
// 彩色图像转为8位的单通道图像
template <typename Type> Type* Rgb2Gray(const Type* pHead, int nWidth, int nHeight, int nRowBytes);
// 统计灰度图像的直方图
template <typename Type> void GrayHistogram(const Type* pHead, int nRowlen, int nHist[256], const RoiRect &roi);
// 统计图像的直方图
template <typename Type> void ImageHistogram(const Type* pHead, int nWidth, int nHeight, int nRowlen, int nHist[256]);
//////////////////////////////////////////////////////////////////////////
/**
* @brief 上下翻转图像.
* @param[in] *pHead 数据头
* @param[in] nWidth 宽度
* @param[in] nHeight 高度
* @param[in] nRowlen 每行个数
@return void
*/
template <typename Type> void ImageFlipV(Type* pHead, const int nWidth, const int nHeight, const int nRowlen)
{
// 缓存数据
Type* temp = new Type[nHeight * nRowlen * sizeof(Type)];
memcpy(temp, pHead, nHeight * nRowlen * sizeof(Type));
const Type *pSrc = temp + (nHeight - 1) * nRowlen;
Type *pDst = pHead;
for (int r = 0; r < nHeight; ++r)
{
memcpy(pDst, pSrc, nRowlen * sizeof(Type));
pSrc -= nRowlen;
pDst += nRowlen;
}
SAFE_DELETE(temp);
}
/**
* @brief 左右翻转图像
* @param[in] *pHead 数据头
* @param[in] nWidth 宽度
* @param[in] nHeight 高度
* @param[in] nRowlen 每行个数
*/
template <typename Type> void ImageFlipH(Type* pHead, const int nWidth, const int nHeight, const int nRowlen)
{
Type* temp = new Type[nHeight * nRowlen];
memcpy(temp, pHead, nHeight * nRowlen * sizeof(Type));
const int nChannel = nRowlen / nWidth;
const Type *pSrc = temp + (nWidth - 1) * nChannel;
Type *pDst = pHead;
for (int r = 0; r < nHeight; ++r)
{
const Type *pSrcLine = pSrc;// 行指针
Type *pDstLine = pDst;
for (int c = 0; c < nWidth; ++c)
{
memcpy(pDstLine, pSrcLine, nChannel * sizeof(Type));
pSrcLine -= nChannel;
pDstLine += nChannel;
}
pSrc += nRowlen;
pDst += nRowlen;
}
SAFE_DELETE(temp);
}
/**
* @brief 转置图像
* @param[in] **pHead 数据头
* @param[in] nWidth 宽度
* @param[in] nHeight 高度
* @param[in] nRowlen 每行个数
* @note 函数将修改图像指针
*/
template <typename Type> void ImageTranspose(Type** pHead, const int nWidth, const int nHeight, const int nRowlen)
{
// 图像每像素元素个数
const int nChannel = nRowlen / nWidth;
// 每行元素个数
int nNewRowlen = nHeight * nChannel;
// 是否需要进行对齐处理
if (1 == sizeof(Type))
{
nNewRowlen = WIDTHBYTES(nNewRowlen * 8);
}
Type *pSrc = *pHead;
Type *temp = new Type[nWidth * nNewRowlen];
memset(temp, 0, nWidth * nNewRowlen * sizeof(Type));
#pragma omp parallel for
for (int k = 0; k < nChannel; ++k)
{
int x = 0, y1 = 0;
for (int i = 0; i < nWidth; ++i)
{
int y = 0, x1 = (nHeight - 1) * nChannel;
for (int j = 0; j < nHeight; ++j)
{
temp[k + x1 + y1] = pSrc[k + x + y];
y += nRowlen;
x1 -= nChannel;
}
x += nChannel;
y1 += nNewRowlen;
}
}
delete[] * pHead;
*pHead = temp;
temp = NULL;
}
/**
* @brief 向右转置图像(右转90度)
* @param[in] **pHead 数据头
* @param[in] nWidth 宽度
* @param[in] nHeight 高度
* @param[in] nRowlen 每行个数
* @note 函数将修改图像指针
*/
template <typename Type> void ImageTransposeR(Type** pHead, const int nWidth, const int nHeight, const int nRowlen)
{
// 图像每像素元素个数
const int nChannel = nRowlen / nWidth;
// 每行元素个数
int nNewRowlen = nHeight * nChannel;
// 是否需要进行对齐处理
if (1 == sizeof(Type))
{
nNewRowlen = WIDTHBYTES(nNewRowlen * 8);
}
Type *pSrc = *pHead;
Type *temp = new Type[nWidth * nNewRowlen];
memset(temp, 0, nWidth * nNewRowlen * sizeof(Type));
#pragma omp parallel for
for (int k = 0; k < nChannel; ++k)
{
int x = 0, y1 = (nWidth - 1) * nNewRowlen;
for (int i = 0; i < nWidth; ++i)
{
int y = 0, x1 = 0;
for (int j = 0; j < nHeight; ++j)
{
temp[k + x1 + y1] = pSrc[k + x + y];
y += nRowlen;
x1 += nChannel;
}
x += nChannel;
y1 -= nNewRowlen;
}
}
delete[] * pHead;
*pHead = temp;
temp = NULL;
}
/**
* @brief 提取图像感兴趣区域
* @param[in] **pHead 数据头
* @param[in] &nWidth 宽度
* @param[in] &nHeight 高度
* @param[in] &nRowlen 每行个数
* @param[in] &roi 图像感兴趣区域
* @return 函数返回图像感兴趣区域(指针,需要SAFE_DELETE)
* @note 函数传入图像宽度、高度等信息,输出ROI的宽度、高度等信息
*/
template <typename Type> Type* ImageROI(const Type* pHead, int &nWidth, int &nHeight, int &nRowlen, const RoiRect &roi)
{
/// 如果roi非法
if (roi.Width() <= 0 || roi.Height() <= 0 || roi.right > nWidth || roi.bottom > nHeight)
{
Type* pDst = new Type[nHeight * nRowlen];
memcpy(pDst, pHead, nHeight * nRowlen * sizeof(Type));
return pDst;
}
const int nChannel = nRowlen / nWidth;
const int nNewWidth = roi.Width();
const int nNewHeight = roi.Height();
int nNewRowlen = nNewWidth * nChannel;
// 是否需要进行对齐处理
if (1 == sizeof(Type))
{
nNewRowlen = WIDTHBYTES(nNewRowlen * 8);
}
Type* pDst = new Type[nNewHeight * nNewRowlen];
Type *pDstLine = pDst;
const Type *pSrcLine = pHead + roi.left * nChannel + (nHeight - 1 - roi.top) * nRowlen;
for (int i = 0; i < nNewHeight; ++i)
{
memcpy(pDstLine, pSrcLine, nNewRowlen * sizeof(Type));
pSrcLine -= nRowlen;
pDstLine += nNewRowlen;
}
// 更新图像信息
nWidth = nNewWidth;
nHeight = nNewHeight;
nRowlen = nNewRowlen;
return pDst;
}
/**
* @brief 将图像以二进制形式写入文件
* @param[in] *fileName 文件名称
* @param[in] *pHead 数据头
* @param[in] nWidth 宽度
* @param[in] nHeight 高度
* @param[in] nRowlen 每行个数
* @see FileIO中的WriteTxt/ReadTxt函数
*/
template <typename Type> BOOL ImageWrite(const char* fileName, const Type* pSrc, int nWidth, int nHeight, int nRowlen)
{
ofstream fout;
fout.open(fileName, ofstream::binary);
if (fout.is_open())
{
try
{
// 写文件头(大小HEADER_SIZE)
char* pHead = "yuanyuanxiang";
int nLen = strlen(pHead);
fout.write(pHead, nLen);
int nChannel = nRowlen / nWidth;
int nInt = sizeof(int);
fout.write((char*)&nRowlen, nInt);
fout.write((char*)&nHeight, nInt);
fout.write((char*)&nChannel, nInt);
fout.seekp(HEADER_SIZE);
// 写图像数据
fout.write((char*)pSrc, nHeight * nRowlen);
fout.close();
return TRUE;
}
catch (...)
{
fout.close();
return FALSE;
}
}
return FALSE;
}
/* - 放大图像 -
* @param[in] **pHead 数据头
* @param[in] &nWidth 宽度
* @param[in] &nHeight 高度
* @param[in] &nRowlen 每行个数
* @param[in] nChannel 图像通道
* @param[in] NewWidth 图像新宽度
* @param[in] NewHeight 图像新高度
* @note 函数会修改pfHead指针的内容,作为结果
*/
template <typename Type> void ZoomImage(Type** pfHead, int &nWidth, int &nHeight, int &nRowlen, int nChannel, int NewWidth, int NewHeight)
{
if (nWidth == NewWidth && nHeight == NewHeight)
return;
int NewRowlen = NewWidth * nChannel;
// 是否需要进行对齐处理
if (1 == sizeof(Type))
{
NewRowlen = WIDTHBYTES(NewRowlen * 8);
}
Type* pDst = new Type[NewRowlen * NewHeight];
float wRatio = 1.f * nWidth / NewWidth;
float hRatio = 1.f * nHeight / NewHeight;
#pragma omp parallel for
for (int nCurChannel = 0; nCurChannel < nChannel; ++nCurChannel)
{
int x = 0;
float s = 0;
for (int i = 0; i < NewWidth; ++i)
{
int y = 0;
float t = 0;
for (int j = 0; j < NewHeight; ++j)
{
int x1 = int(s), y1 = int(t), x3 = x1 + 1, y3 = y1 + 1;
// 左下角的点
const BYTE* pLB = *pfHead + nCurChannel + x1 * nChannel + y1 * nRowlen;//必须转换为BYTE*
// 对越界的处理
pDst[nCurChannel + x + y] = (x1 < 0 || x3 >= nWidth || y1 < 0 || y3 >= nHeight) ? 0 :
(*pLB * (x3 - s) + *(pLB + nChannel) * (s - x1)) * (y3 - t)
+ (*(pLB + nRowlen) * (x3 - s) + *(pLB + nChannel + nRowlen) * (s - x1)) * (t - y1);
y += NewRowlen;
t += hRatio;
}
x += nChannel;
s += wRatio;
}
}
// 修改pHead指向的内容
delete[] * pfHead;
*pfHead = pDst;
pDst = NULL;
// 更新图像信息
nWidth = NewWidth;
nHeight = NewHeight;
nRowlen = NewRowlen;
}
/* - 限制图像最大边长 -
* @param[in] **pHead 数据头
* @param[in] &nWidth 宽度
* @param[in] &nHeight 高度
* @param[in] &nRowlen 每行个数
* @param[in] nChannel 图像通道
* @param[in] nMaxSize 最大边长320
* @note 当图像有一边长度超过限制时进行缩放并更新信息
* @warning 输入图像需为字节类型,宽度、高度、每行字节数会被修改
*/
template <typename Type> void LimitImage(Type** pfHead, int &nWidth, int &nHeight, int &nRowlen, int nChannel, int nMaxSize)
{
float rate = 0.0f;
if (nWidth > nHeight)
{
if (nWidth > nMaxSize)
{
rate = 1.f * nMaxSize / nWidth;
ZoomImage(pfHead, nWidth, nHeight, nRowlen, nChannel, nMaxSize, int(rate * nHeight));
}
}
else
{
if (nHeight > nMaxSize)
{
rate = 1.f * nMaxSize / nHeight;
ZoomImage(pfHead, nWidth, nHeight, nRowlen, nChannel, int(rate * nWidth), nMaxSize);
}
}
}
// 获取图像行的梯度和[不计算边缘,计算中间1/3]
template <typename Type>
inline int RowProject(const Type* pHead, int nWidth, int nHeight, int nChannel, int nRowlen, int *row)
{
memset(row, 0, nHeight * sizeof(int));
const int nHalfW = nWidth / 2;
const int R = nWidth / 6;
const BYTE *p = (const BYTE*) pHead;
int avg = 0;
for (int r = 0; r < nHeight; ++r)
{
for (int i = nHalfW - R, y = r * nRowlen; i < nHalfW + R; ++i)
row[r] += abs(*(p + i * nChannel + y) - *(p + (i-1) * nChannel + y));
if (nHeight/3 <= r && r < nHeight * 2/3)
avg += row[r];
}
int f_avg = avg / nHeight;
return f_avg;
}
// 获取图像列的梯度和[不计算边缘,计算中间1/3]
template <typename Type>
inline int ColProject(const Type* pHead, int nWidth, int nHeight, int nChannel, int nRowlen, int *col)
{
memset(col, 0, nWidth * sizeof(int));
const int nHalfH = nHeight / 2;
const int R = nHeight / 6;
const BYTE *p = (const BYTE*) pHead;
int avg = 0;
for (int c = 0; c < nWidth; ++c)
{
for (int j = nHalfH - R, x = c * nChannel; j < nHalfH + R; ++j)
col[c] += abs(*(p + x + j * nRowlen) - *(p + x + (j+1) * nRowlen));
if (nWidth/3 <= c && c < nWidth * 2/3)
avg += col[c];
}
int f_avg = avg / nWidth;
return f_avg;
}
/* - 提取截屏图像中间的Qr码 -
* @param[in] *pHead 数据头
* @param[in] &nWidth 宽度
* @param[in] &nHeight 高度
* @param[in] &nRowlen 每行个数
* @param[in] &cutAero 裁剪比例
* @note 当图像宽高比大于等于1.5时,提取QR码
* @warning 输入图像需为字节类型,宽度、高度、每行字节数会被修改
*/
template <typename Type> void GetQrRect(const Type* pHead, int nWidth, int nHeight, int nChannel, int nRowlen, RoiRect &cutAero)
{
if (nWidth > 2048 || nHeight > 2048)
{
cutAero = RoiRect(0, 0, nWidth, nHeight);
return;
}
int nHalfW = nWidth/2, nHalfH = nHeight/2;
int difRow[2048] = {0};
int difCol[2048] = {0};
double avgRow = RowProject(pHead, nWidth, nHeight, nChannel, nRowlen, difRow);
double avgCol = ColProject(pHead, nWidth, nHeight, nChannel, nRowlen, difCol);
int Start = 0, End = 0;
for (int i = 0; i < nHalfH; ++i)// 计算top
{
if (difRow[nHalfH - i] < avgRow)
{
Start = nHalfH - 1 - i;
break;
}
}
for (int i = 0; i < nHalfH; ++i)// 计算bottom
{
if (difRow[nHalfH + i] < avgRow)
{
End = nHalfH + 1 + i;
break;
}
}
for (int i = 0; i < nHalfW; ++i)// 计算left
{
if (difCol[nHalfW - i] < avgCol)
{
cutAero.left = nHalfW - 1 - i;
break;
}
}
for (int i = 0; i < nHalfW; ++i)// 计算right
{
if (difCol[nHalfW + i] < avgCol)
{
cutAero.right = nHalfW + 1 + i;
break;
}
}
cutAero.top = nHeight - End;
cutAero.bottom = nHeight - Start;
cutAero.left -= QR_MARGIN;
cutAero.right += QR_MARGIN;
cutAero.top -= QR_MARGIN;
cutAero.bottom += QR_MARGIN;
cutAero.left = max(0, cutAero.left);
cutAero.right = min(cutAero.right, nWidth - 1);
cutAero.top = max(0, cutAero.top);
cutAero.bottom = min(cutAero.bottom, nHeight - 1);
int rW = 4, rH = 6;
if (nWidth > nHeight)
swap(rW, rH);
if (cutAero.Width() < nWidth/rW || cutAero.Height() < nHeight/rH)
{
const int N = 5;
cutAero = nHeight > nWidth ? RoiRect(QR_MARGIN, nHeight/N, nWidth-1 - QR_MARGIN, nHeight * (N-1)/N):
RoiRect(nWidth/N, QR_MARGIN, nWidth * (N-1)/N, nHeight-1 - QR_MARGIN);
}
}
/**
* @brief 彩色转黑白.
* @param[in] *pHead 图像数据
* @param[in] nWidth 图像宽度
* @param[in] nHeight 图像高度
* @param[in] nRowBytes 每行字节数
* @return 函数将返回灰度图像
* @note 仅限对char/BYTE进行处理
* @warning 需要对返回值进行delete处理.
*/
template <typename Type> Type* Rgb2Gray(const Type* pHead, int nWidth, int nHeight, int nRowBytes)
{
ASSERT(1 == sizeof(Type));
const int nChannel = nRowBytes / nWidth;
const int nChannelNew = 1;
const int nRowLenNew = WIDTHBYTES(nWidth * 8);
Type *pDst = new Type[nHeight * nRowLenNew];
switch (nChannel)
{
case 1:
memcpy(pDst, pHead, nHeight * nRowBytes);
break;
case 3:
case 4:// 3、4通道处理是一样的
{
const Type *pSrcTemp0 = pHead;
Type *pDstTemp0 = pDst;
for (int i = 0; i < nHeight; i++)
{
const Type *pSrcTemp = pSrcTemp0;
Type *pDstTemp = pDstTemp0;
for (int j = 0; j < nWidth; j++)
{
// 必须强制转换为无符号字符型数据(2016/9/12 注)
BYTE R = *(pSrcTemp + 2);
BYTE G = *(pSrcTemp + 1);
BYTE B = *pSrcTemp;
*pDstTemp = BYTE(RGB2GRAY(R, G, B));
pSrcTemp += nChannel;
pDstTemp += nChannelNew;
}
pSrcTemp0 += nRowBytes;
pDstTemp0 += nRowLenNew;
}
}
break;
default:
SAFE_DELETE(pDst);
return NULL;
}
return pDst;
}
/**
* @brief 统计灰度图像的直方图.
* @param[in] *pHead 图像指针
* @param[in] nRowlen 图像每行字节数
* @param[in] nHist 图像直方图
* @param[in] &roi 图像感兴趣区域
*/
template <typename Type> void GrayHistogram(const Type* pHead, int nRowlen, int nHist[256], const RoiRect &roi)
{
ASSERT(sizeof(Type) == 1);
memset(nHist, 0, 256 * sizeof(int));
const BYTE *pSrcLine = (const BYTE*)pHead + roi.left + roi.top * nRowlen;
for (int i = roi.top; i < roi.bottom; ++i)
{
const BYTE *pCur = pSrcLine;
for (int j = roi.left; j < roi.right; ++j)
{
BYTE cur = *pCur ++;
++ nHist[cur];
}
pSrcLine += nRowlen;
}
}
/**
* @brief 统计图像的直方图.
* @param[in] *pHead 图像指针
* @param[in] nWidth 图像宽度
* @param[in] nHeight 图像高度
* @param[in] nRowlen 图像每行字节数
* @param[in] nHist 图像直方图
*/
template <typename Type>
void ImageHistogram(const Type* pHead, int nWidth, int nHeight, int nRowlen, int nHist[256])
{
const int nChannel = nRowlen / nWidth;
if (1 == nChannel)
return GrayHistogram(pHead, nRowlen, nHist, RoiRect(0, 0, nWidth, nHeight));
ASSERT(sizeof(Type) == 1);
memset(nHist, 0, 256 * sizeof(int));
const BYTE *pSrcLine = (const BYTE*) pHead;
for (int i = 0; i < nHeight; ++i)
{
const BYTE *pCur = pSrcLine;
for (int j = 0; j < nWidth; ++j)
{
int index = RGB2GRAY(*(pCur+2), *(pCur+1), *pCur);
++ nHist[index];
pCur += nChannel;
}
pSrcLine += nRowlen;
}
}