-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageProcessing.h
More file actions
executable file
·686 lines (620 loc) · 24.8 KB
/
ImageProcessing.h
File metadata and controls
executable file
·686 lines (620 loc) · 24.8 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
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#ifndef _ImageProcessing_h
#define _ImageProcessing_h
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include <typeinfo>
//----------------------------------------------------------------------------------
// class to handle basic image processing functions
// this is a collection of template functions. These template functions are
// used in other image classes such as BiImage, IntImage and FImage
//----------------------------------------------------------------------------------
class ImageProcessing
{
public:
ImageProcessing(void);
~ImageProcessing(void);
public:
// basic functions
template <class T>
static inline T EnforceRange(const T& x,const int& MaxValue) {return __min(__max(x,0),MaxValue-1);};
//---------------------------------------------------------------------------------
// function to interpolate the image plane
//---------------------------------------------------------------------------------
template <class T1,class T2>
static inline void BilinearInterpolate(const T1* pImage,int width,int height,int nChannels,double x,double y,T2* result);
template <class T1>
static inline T1 BilinearInterpolate(const T1* pImage,int width,int height,double x,double y);
// the transpose of bilinear interpolation
template <class T1,class T2>
static inline void BilinearInterpolate_transpose(const T1* pImage,int width,int height,int nChannels,double x,double y,T2* result);
template <class T1>
static inline T1 BilinearInterpolate_transpose(const T1* pImage,int width,int height,double x,double y);
template <class T1,class T2>
static void ResizeImage(const T1* pSrcImage,T2* pDstImage,int SrcWidth,int SrcHeight,int nChannels,double Ratio);
template <class T1,class T2>
static void ResizeImage(const T1* pSrcImage,T2* pDstImage,int SrcWidth,int SrcHeight,int nChannels,int DstWidth,int DstHeight);
//---------------------------------------------------------------------------------
// functions for 1D filtering
//---------------------------------------------------------------------------------
template <class T1,class T2>
static void hfiltering(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter1D,int fsize);
template <class T1,class T2>
static void vfiltering(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter1D,int fsize);
template <class T1,class T2>
static void hfiltering_transpose(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter1D,int fsize);
template <class T1,class T2>
static void vfiltering_transpose(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter1D,int fsize);
//---------------------------------------------------------------------------------
// functions for 2D filtering
//---------------------------------------------------------------------------------
template <class T1,class T2>
static void filtering(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter2D,int fsize);
template <class T1,class T2>
static void filtering_transpose(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter2D,int fsize);
template <class T1,class T2>
static void Laplacian(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels);
//---------------------------------------------------------------------------------
// functions for sample a patch from the image
//---------------------------------------------------------------------------------
template <class T1,class T2>
static void getPatch(const T1* pSrcImgae,T2* pPatch,int width,int height,int nChannels,double x,double y,int wsize);
//---------------------------------------------------------------------------------
// function to warp image
//---------------------------------------------------------------------------------
template <class T1,class T2>
static void warpImage(T1* pWarpIm2,const T1* pIm1,const T1* pIm2,const T2* pVx,const T2* pVy,int width,int height,int nChannels);
template <class T1,class T2>
static void warpImageFlow(T1* pWarpIm2,const T1* pIm1,const T1* pIm2,const T2* pFlow,int width,int height,int nChannels);
template <class T1,class T2>
static void warpImage(T1* pWarpIm2,const T1* pIm2,const T2* pVx,const T2* pVy,int width,int height,int nChannels);
template <class T1,class T2>
static void warpImage_transpose(T1* pWarpIm2,const T1* pIm2,const T2* pVx,const T2* pVy,int width,int height,int nChannels);
template <class T1,class T2>
static void warpImage(T1* pWarpIm2,const T1* pIm2,const T2*flow,int width,int height,int nChannels);
template <class T1,class T2>
static void warpImage_transpose(T1* pWarpIm2,const T1* pIm2,const T2* flow,int width,int height,int nChannels);
template <class T1,class T2,class T3>
static void warpImage(T1 *pWarpIm2, T3* pMask,const T1 *pIm1, const T1 *pIm2, const T2 *pVx, const T2 *pVy, int width, int height, int nChannels);
//---------------------------------------------------------------------------------
// function to crop an image
//---------------------------------------------------------------------------------
template <class T1,class T2>
static void cropImage(const T1* pSrcImage,int SrcWidth,int SrcHeight,int nChannels,T2* pDstImage,int Left,int Top,int DstWidth,int DstHeight);
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
// function to generate a 2D Gaussian
//---------------------------------------------------------------------------------
template <class T>
static void generate2DGaussian(T*& pImage,int wsize,double sigma=-1);
template <class T>
static void generate1DGaussian(T*& pImage,int wsize,double sigma=-1);
};
//--------------------------------------------------------------------------------------------------
// function to interplate multi-channel image plane for (x,y)
// --------------------------------------------------------------------------------------------------
template <class T1,class T2>
inline void ImageProcessing::BilinearInterpolate(const T1* pImage,int width,int height,int nChannels,double x,double y,T2* result)
{
int xx,yy,m,n,u,v,l,offset;
xx=x;
yy=y;
double dx,dy,s;
dx=__max(__min(x-xx,1),0);
dy=__max(__min(y-yy,1),0);
for(m=0;m<=1;m++)
for(n=0;n<=1;n++)
{
u=EnforceRange(xx+m,width);
v=EnforceRange(yy+n,height);
offset=(v*width+u)*nChannels;
s=fabs(1-m-dx)*fabs(1-n-dy);
for(l=0;l<nChannels;l++)
result[l]+=pImage[offset+l]*s;
}
}
template <class T1>
inline T1 ImageProcessing::BilinearInterpolate(const T1* pImage,int width,int height,double x,double y)
{
int xx,yy,m,n,u,v,l,offset;
xx=x;
yy=y;
double dx,dy,s;
dx=__max(__min(x-xx,1),0);
dy=__max(__min(y-yy,1),0);
T1 result=0;
for(m=0;m<=1;m++)
for(n=0;n<=1;n++)
{
u=EnforceRange(xx+m,width);
v=EnforceRange(yy+n,height);
offset=v*width+u;
s=fabs(1-m-dx)*fabs(1-n-dy);
result+=pImage[offset]*s;
}
return result;
}
//--------------------------------------------------------------------------------------------------
// function to interplate multi-channel image plane for (x,y)
// --------------------------------------------------------------------------------------------------
template <class T1,class T2>
inline void ImageProcessing::BilinearInterpolate_transpose(const T1* pInput,int width,int height,int nChannels,double x,double y,T2* pDstImage)
{
int xx,yy,m,n,u,v,l,offset;
xx=x;
yy=y;
double dx,dy,s;
dx=__max(__min(x-xx,1),0);
dy=__max(__min(y-yy,1),0);
for(m=0;m<=1;m++)
for(n=0;n<=1;n++)
{
u=EnforceRange(xx+m,width);
v=EnforceRange(yy+n,height);
offset=(v*width+u)*nChannels;
s=fabs(1-m-dx)*fabs(1-n-dy);
for(l=0;l<nChannels;l++)
pDstImage[offset+l] += pInput[l]*s;
}
}
//------------------------------------------------------------------------------------------------------------
// this is the most general function for reszing an image with a varying nChannels
// bilinear interpolation is used for now. It might be replaced by other (bicubic) interpolation methods
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::ResizeImage(const T1* pSrcImage,T2* pDstImage,int SrcWidth,int SrcHeight,int nChannels,double Ratio)
{
int DstWidth,DstHeight;
DstWidth=(double)SrcWidth*Ratio;
DstHeight=(double)SrcHeight*Ratio;
memset(pDstImage,0,sizeof(T2)*DstWidth*DstHeight*nChannels);
double x,y;
for(int i=0;i<DstHeight;i++)
for(int j=0;j<DstWidth;j++)
{
x=(double)(j+1)/Ratio-1;
y=(double)(i+1)/Ratio-1;
// bilinear interpolation
BilinearInterpolate(pSrcImage,SrcWidth,SrcHeight,nChannels,x,y,pDstImage+(i*DstWidth+j)*nChannels);
}
}
template <class T1,class T2>
void ImageProcessing::ResizeImage(const T1 *pSrcImage, T2 *pDstImage, int SrcWidth, int SrcHeight, int nChannels, int DstWidth, int DstHeight)
{
double xRatio=(double)DstWidth/SrcWidth;
double yRatio=(double)DstHeight/SrcHeight;
memset(pDstImage,sizeof(T2)*DstWidth*DstHeight*nChannels,0);
double x,y;
for(int i=0;i<DstHeight;i++)
for(int j=0;j<DstWidth;j++)
{
x=(double)(j+1)/xRatio-1;
y=(double)(i+1)/yRatio-1;
// bilinear interpolation
BilinearInterpolate(pSrcImage,SrcWidth,SrcHeight,nChannels,x,y,pDstImage+(i*DstWidth+j)*nChannels);
}
}
//------------------------------------------------------------------------------------------------------------
// horizontal direction filtering
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::hfiltering(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter1D,int fsize)
{
memset(pDstImage,0,sizeof(T2)*width*height*nChannels);
T2* pBuffer;
double w;
int i,j,l,k,offset,jj;
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
offset=i*width*nChannels;
pBuffer=pDstImage+offset+j*nChannels;
for(l=-fsize;l<=fsize;l++)
{
w=pfilter1D[l+fsize];
jj=EnforceRange(j+l,width);
for(k=0;k<nChannels;k++)
pBuffer[k]+=pSrcImage[offset+jj*nChannels+k]*w;
}
}
}
//------------------------------------------------------------------------------------------------------------
// horizontal direction filtering transpose
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::hfiltering_transpose(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter1D,int fsize)
{
memset(pDstImage,0,sizeof(T2)*width*height*nChannels);
const T1* pBuffer;
double w;
int i,j,l,k,offset,jj;
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
int offset0=i*width*nChannels;
pBuffer=pSrcImage+(i*width+j)*nChannels;
for(l=-fsize;l<=fsize;l++)
{
w=pfilter1D[l+fsize];
jj=EnforceRange(j+l,width);
offset = offset0 + jj*nChannels;
for(k=0;k<nChannels;k++)
pDstImage[offset+k] += pBuffer[k]*w;
}
}
}
//------------------------------------------------------------------------------------------------------------
// fast filtering algorithm for laplacian
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::Laplacian(const T1 *pSrcImage, T2 *pDstImage, int width, int height, int nChannels)
{
int LineWidth=width*nChannels;
int nElements=width*height*nChannels;
// first treat the corners
for(int k=0;k<nChannels;k++)
{
pDstImage[k]=pSrcImage[k]*2-pSrcImage[nChannels+k]-pSrcImage[LineWidth+k];
pDstImage[LineWidth-nChannels+k]=pSrcImage[LineWidth-nChannels+k]*2-pSrcImage[LineWidth-2*nChannels+k]-pSrcImage[2*LineWidth-nChannels+k];
pDstImage[nElements-LineWidth+k]=pSrcImage[nElements-LineWidth+k]*2-pSrcImage[nElements-LineWidth+nChannels+k]-pSrcImage[nElements-2*LineWidth+k];
pDstImage[nElements-nChannels+k]=pSrcImage[nElements-nChannels+k]*2-pSrcImage[nElements-2*nChannels+k]-pSrcImage[nElements-LineWidth-nChannels+k];
}
// then treat the borders
for(int i=1;i<width-1;i++)
for(int k=0;k<nChannels;k++)
{
pDstImage[i*nChannels+k]=pSrcImage[i*nChannels+k]*3-pSrcImage[(i-1)*nChannels+k]-pSrcImage[(i+1)*nChannels+k]-pSrcImage[i*nChannels+LineWidth+k];
pDstImage[nElements-LineWidth+i*nChannels+k]=pSrcImage[nElements-LineWidth+i*nChannels+k]*3-pSrcImage[nElements-LineWidth+(i-1)*nChannels+k]-pSrcImage[nElements-LineWidth+(i+1)*nChannels+k]-pSrcImage[nElements-2*LineWidth+i*nChannels+k];
}
for(int i=1;i<height-1;i++)
for(int k=0;k<nChannels;k++)
{
pDstImage[i*LineWidth+k]=pSrcImage[i*LineWidth+k]*3-pSrcImage[i*LineWidth+nChannels+k]-pSrcImage[(i-1)*LineWidth+k]-pSrcImage[(i+1)*LineWidth+k];
pDstImage[(i+1)*LineWidth-nChannels+k]=pSrcImage[(i+1)*LineWidth-nChannels+k]*3-pSrcImage[(i+1)*LineWidth-2*nChannels+k]-pSrcImage[i*LineWidth-nChannels+k]-pSrcImage[(i+2)*LineWidth-nChannels+k];
}
// now the interior
for(int i=1;i<height-1;i++)
for(int j=1;j<width-1;j++)
{
int offset=(i*width+j)*nChannels;
for(int k=0;k<nChannels;k++)
pDstImage[offset+k]=pSrcImage[offset+k]*4-pSrcImage[offset+nChannels+k]-pSrcImage[offset-nChannels+k]-pSrcImage[offset-LineWidth+k]-pSrcImage[offset+LineWidth+k];
}
}
//------------------------------------------------------------------------------------------------------------
// vertical direction filtering
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::vfiltering(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter1D,int fsize)
{
memset(pDstImage,0,sizeof(T2)*width*height*nChannels);
T2* pBuffer;
double w;
int i,j,l,k,offset,ii;
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
pBuffer=pDstImage+(i*width+j)*nChannels;
for(l=-fsize;l<=fsize;l++)
{
w=pfilter1D[l+fsize];
ii=EnforceRange(i+l,height);
for(k=0;k<nChannels;k++)
pBuffer[k]+=pSrcImage[(ii*width+j)*nChannels+k]*w;
}
}
}
//------------------------------------------------------------------------------------------------------------
// vertical direction filtering transpose
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::vfiltering_transpose(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter1D,int fsize)
{
memset(pDstImage,0,sizeof(T2)*width*height*nChannels);
const T1* pBuffer;
double w;
int i,j,l,k,offset,ii;
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
pBuffer=pSrcImage+(i*width+j)*nChannels;
for(l=-fsize;l<=fsize;l++)
{
w=pfilter1D[l+fsize];
ii=EnforceRange(i+l,height);
offset = (ii*width+j)*nChannels;
for(k=0;k<nChannels;k++)
//pBuffer[k]+=pSrcImage[(ii*width+j)*nChannels+k]*w;
pDstImage[offset+k] += pBuffer[k]*w;
}
}
}
//------------------------------------------------------------------------------------------------------------
// 2d filtering
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::filtering(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter2D,int fsize)
{
double w;
int i,j,u,v,k,ii,jj,wsize,offset;
wsize=fsize*2+1;
double* pBuffer=new double[nChannels];
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
for(k=0;k<nChannels;k++)
pBuffer[k]=0;
for(u=-fsize;u<=fsize;u++)
for(v=-fsize;v<=fsize;v++)
{
w=pfilter2D[(u+fsize)*wsize+v+fsize];
ii=EnforceRange(i+u,height);
jj=EnforceRange(j+v,width);
offset=(ii*width+jj)*nChannels;
for(k=0;k<nChannels;k++)
pBuffer[k]+=pSrcImage[offset+k]*w;
}
offset=(i*width+j)*nChannels;
for(k=0;k<nChannels;k++)
pDstImage[offset+k]=pBuffer[k];
}
delete pBuffer;
}
//------------------------------------------------------------------------------------------------------------
// 2d filtering transpose
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::filtering_transpose(const T1* pSrcImage,T2* pDstImage,int width,int height,int nChannels,const double* pfilter2D,int fsize)
{
double w;
int i,j,u,v,k,ii,jj,wsize,offset;
wsize=fsize*2+1;
memset(pDstImage,0,sizeof(T2)*width*height*nChannels);
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
int offset0 = (i*width+j)*nChannels;
for(u=-fsize;u<=fsize;u++)
for(v=-fsize;v<=fsize;v++)
{
w=pfilter2D[(u+fsize)*wsize+v+fsize];
ii=EnforceRange(i+u,height);
jj=EnforceRange(j+v,width);
int offset=(ii*width+jj)*nChannels;
for(k=0;k<nChannels;k++)
pDstImage[offset+k]+=pSrcImage[offset0+k]*w;
}
}
}
//------------------------------------------------------------------------------------------------------------
// function to sample a patch from the source image
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::getPatch(const T1* pSrcImage,T2* pPatch,int width,int height,int nChannels,double x0,double y0,int wsize)
{
// suppose pPatch has been allocated and cleared before calling the function
int wlength=wsize*2+1;
double x,y;
for(int i=-wsize;i<=wsize;i++)
for(int j=-wsize;j<=wsize;j++)
{
y=y0+i;
x=x0+j;
if(x<0 || x>width-1 || y<0 || y>height-1)
continue;
BilinearInterpolate(pSrcImage,width,height,nChannels,x,y,pPatch+((i+wsize)*wlength+j+wsize)*nChannels);
}
}
//------------------------------------------------------------------------------------------------------------
// function to warp an image with respect to flow field
// pWarpIm2 has to be allocated before hands
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::warpImage(T1 *pWarpIm2, const T1 *pIm1, const T1 *pIm2, const T2 *pVx, const T2 *pVy, int width, int height, int nChannels)
{
memset(pWarpIm2,0,sizeof(T1)*width*height*nChannels);
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
{
int offset=i*width+j;
double x,y;
y=i+pVy[offset];
x=j+pVx[offset];
offset*=nChannels;
if(x<0 || x>width-1 || y<0 || y>height-1)
{
for(int k=0;k<nChannels;k++)
pWarpIm2[offset+k]=pIm1[offset+k];
continue;
}
BilinearInterpolate(pIm2,width,height,nChannels,x,y,pWarpIm2+offset);
}
}
template <class T1,class T2>
void ImageProcessing::warpImageFlow(T1 *pWarpIm2, const T1 *pIm1, const T1 *pIm2, const T2 *pFlow, int width, int height, int nChannels)
{
memset(pWarpIm2,0,sizeof(T1)*width*height*nChannels);
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
{
int offset=i*width+j;
double x,y;
y=i+pFlow[offset*2+1];
x=j+pFlow[offset*2];
offset*=nChannels;
if(x<0 || x>width-1 || y<0 || y>height-1)
{
for(int k=0;k<nChannels;k++)
pWarpIm2[offset+k]=pIm1[offset+k];
continue;
}
BilinearInterpolate(pIm2,width,height,nChannels,x,y,pWarpIm2+offset);
}
}
template <class T1,class T2>
void ImageProcessing::warpImage(T1 *pWarpIm2,const T1 *pIm2, const T2 *pVx, const T2 *pVy, int width, int height, int nChannels)
{
memset(pWarpIm2,0,sizeof(T1)*width*height*nChannels);
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
{
int offset=i*width+j;
double x,y;
y=i+pVy[offset];
x=j+pVx[offset];
offset*=nChannels;
if(x<0 || x>width-1 || y<0 || y>height-1)
continue;
BilinearInterpolate(pIm2,width,height,nChannels,x,y,pWarpIm2+offset);
}
}
template <class T1,class T2>
void ImageProcessing::warpImage_transpose(T1 *pWarpIm2,const T1 *pIm2, const T2 *pVx, const T2 *pVy, int width, int height, int nChannels)
{
memset(pWarpIm2,0,sizeof(T1)*width*height*nChannels);
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
{
int offset=i*width+j;
double x,y;
y=i+pVy[offset];
x=j+pVx[offset];
offset*=nChannels;
if(x<0 || x>width-1 || y<0 || y>height-1)
continue;
//BilinearInterpolate(pIm2,width,height,nChannels,x,y,pWarpIm2+offset);
BilinearInterpolate_transpose(pIm2+offset,width,height,nChannels,x,y,pWarpIm2);
}
}
//////////////////////////////////////////////////////////////////////////////////////
// different format
//////////////////////////////////////////////////////////////////////////////////////
template <class T1,class T2>
void ImageProcessing::warpImage(T1 *pWarpIm2,const T1 *pIm2, const T2 *flow, int width, int height, int nChannels)
{
memset(pWarpIm2,0,sizeof(T1)*width*height*nChannels);
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
{
int offset=i*width+j;
double x,y;
y=i+flow[offset*2+1];
x=j+flow[offset*2];
offset*=nChannels;
if(x<0 || x>width-1 || y<0 || y>height-1)
continue;
BilinearInterpolate(pIm2,width,height,nChannels,x,y,pWarpIm2+offset);
}
}
template <class T1,class T2>
void ImageProcessing::warpImage_transpose(T1 *pWarpIm2,const T1 *pIm2, const T2 *flow, int width, int height, int nChannels)
{
memset(pWarpIm2,0,sizeof(T1)*width*height*nChannels);
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
{
int offset=i*width+j;
double x,y;
y=i+flow[offset*2+1];
x=j+flow[offset*2];
offset*=nChannels;
if(x<0 || x>width-1 || y<0 || y>height-1)
continue;
//BilinearInterpolate(pIm2,width,height,nChannels,x,y,pWarpIm2+offset);
BilinearInterpolate_transpose(pIm2+offset,width,height,nChannels,x,y,pWarpIm2);
}
}
template <class T1,class T2,class T3>
void ImageProcessing::warpImage(T1 *pWarpIm2, T3* pMask,const T1 *pIm1, const T1 *pIm2, const T2 *pVx, const T2 *pVy, int width, int height, int nChannels)
{
memset(pWarpIm2,0,sizeof(T1)*width*height*nChannels);
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
{
int offset=i*width+j;
double x,y;
y=i+pVy[offset];
x=j+pVx[offset];
offset*=nChannels;
if(x<0 || x>width-1 || y<0 || y>height-1)
{
for(int k=0;k<nChannels;k++)
pWarpIm2[offset+k]=pIm1[offset+k];
pMask[i*width+j]=0;
continue;
}
pMask[i*width+j]=1;
BilinearInterpolate(pIm2,width,height,nChannels,x,y,pWarpIm2+offset);
}
}
//------------------------------------------------------------------------------------------------------------
// function to crop an image from the source
// assume that pDstImage has been allocated
// also Left and Top must be valid, DstWidth and DstHeight should ensure that the image lies
// inside the image boundary
//------------------------------------------------------------------------------------------------------------
template <class T1,class T2>
void ImageProcessing::cropImage(const T1 *pSrcImage, int SrcWidth, int SrcHeight, int nChannels, T2 *pDstImage, int Left, int Top, int DstWidth, int DstHeight)
{
if(typeid(T1)==typeid(T2))
{
for(int i=0;i<DstHeight;i++)
memcpy(pDstImage+i*DstWidth*nChannels,pSrcImage+((i+Top)*SrcWidth+Left)*nChannels,sizeof(T1)*DstWidth*nChannels);
return;
}
int offsetSrc,offsetDst;
for(int i=0;i<DstHeight;i++)
for(int j=0;j<DstWidth;j++)
{
offsetSrc=((i+Top)*SrcWidth+Left+j)*nChannels;
offsetDst=(i*DstWidth+j)*nChannels;
for(int k=0;k<nChannels;k++)
pDstImage[offsetDst+k]=pSrcImage[offsetSrc+k];
}
}
//------------------------------------------------------------------------------------------------------------
// function to generate a 2D Gaussian image
// pImage must be allocated before calling the function
//------------------------------------------------------------------------------------------------------------
template <class T>
void ImageProcessing::generate2DGaussian(T*& pImage, int wsize, double sigma)
{
if(sigma==-1)
sigma=wsize/2;
double alpha=1/(2*sigma*sigma);
int winlength=wsize*2+1;
if(pImage==NULL)
pImage=new T[winlength*winlength];
double total = 0;
for(int i=-wsize;i<=wsize;i++)
for(int j=-wsize;j<=wsize;j++)
{
pImage[(i+wsize)*winlength+j+wsize]=exp(-(double)(i*i+j*j)*alpha);
total += pImage[(i+wsize)*winlength+j+wsize];
}
for(int i = 0;i<winlength*winlength;i++)
pImage[i]/=total;
}
//------------------------------------------------------------------------------------------------------------
// function to generate a 1D Gaussian image
// pImage must be allocated before calling the function
//------------------------------------------------------------------------------------------------------------
template <class T>
void ImageProcessing::generate1DGaussian(T*& pImage, int wsize, double sigma)
{
if(sigma==-1)
sigma=wsize/2;
double alpha=1/(2*sigma*sigma);
int winlength=wsize*2+1;
if(pImage==NULL)
pImage=new T[winlength];
double total = 0;
for(int i=-wsize;i<=wsize;i++)
{
pImage[i+wsize]=exp(-(double)(i*i)*alpha);
total += pImage[i+wsize];
}
for(int i = 0;i<winlength;i++)
pImage[i]/=total;
}
#endif