Repository navigation
Expand file tree
/
Copy pathImage.h
More file actions
2842 lines (2499 loc) · 83.2 KB
/
Copy pathImage.h
File metadata and controls
2842 lines (2499 loc) · 83.2 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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "project.h"
#include "stdio.h"
#include "memory.h"
#include "ImageProcessing.h"
#include <iostream>
#include <fstream>
#include <typeinfo>
#include "Vector.h"
#include "Stochastic.h"
#ifndef _MATLAB
#include "ImageIO.h"
#else
#include "mex.h"
#endif
using namespace std;
enum collapse_type{collapse_average,collapse_max,collapse_min};
enum color_type{RGB,BGR,DATA,GRAY};
// template class for image
template <class T>
class Image
{
public:
T* pData;
protected:
int imWidth,imHeight,nChannels;
int nPixels,nElements;
bool IsDerivativeImage;
color_type colorType;
public:
Image(void);
Image(int width,int height,int nchannels=1);
Image(const T& value,int _width,int _height,int _nchannels=1);
Image(const Image<T>& other);
~Image(void);
virtual Image<T>& operator=(const Image<T>& other);
virtual inline void computeDimension(){nPixels=imWidth*imHeight;nElements=nPixels*nChannels;};
virtual void allocate(int width,int height,int nchannels=1);
template <class T1>
void allocate(const Image<T1>& other);
virtual void clear();
virtual void reset();
virtual void copyData(const Image<T>& other);
void setValue(const T& value);
void setValue(const T& value,int _width,int _height,int _nchannels=1);
T immax() const
{
T Max=pData[0];
for(int i=1;i<nElements;i++)
Max=__max(Max,pData[i]);
return Max;
};
T immin() const{
T Min=pData[0];
for(int i=1;i<nElements;i++)
Min=__min(Min,pData[i]);
return Min;
}
template <class T1>
void copy(const Image<T1>& other);
void im2double();
// function to access the member variables
inline const T& operator [] (int index) const {return pData[index];};
inline T& operator[](int index) {return pData[index];};
inline T*& data(){return pData;};
inline const T*& data() const{return (const T*&)pData;};
inline int width() const {return imWidth;};
inline int height() const {return imHeight;};
inline int nchannels() const {return nChannels;};
inline int npixels() const {return nPixels;};
inline int nelements() const {return nElements;};
inline bool isDerivativeImage() const {return IsDerivativeImage;};
inline color_type colortype() const{return colorType;};
bool IsFloat () const;
bool IsEmpty() const {if(nElements==0) return true;else return false;};
bool IsInImage(int x,int y) const {if(x>=0 && x<imWidth && y>=0 && y<imHeight) return true; else return false;};
template <class T1>
bool matchDimension (const Image<T1>& image) const;
bool matchDimension (int width,int height,int nchannels) const;
inline void setDerivative(bool isDerivativeImage=true){IsDerivativeImage=isDerivativeImage;};
bool BoundaryCheck() const;
// function to move this image to another one
template <class T1>
void moveto(Image<T1>& image,int x,int y,int width=0,int height=0);
// function of basic image operations
virtual bool imresize(double ratio);
template <class T1>
void imresize(Image<T1>& result,double ratio) const;
void imresize(int dstWidth,int dstHeight);
template <class T1>
void imresize(Image<T1>& result,int dstWidth,int dstHeight) const;
template <class T1>
void upSampleNN(Image<T1>& result,int ratio) const;
// image IO's
virtual bool saveImage(const char* filename) const;
virtual bool loadImage(const char* filename);
virtual bool saveImage(ofstream& myfile) const;
virtual bool loadImage(ifstream& myfile);
#ifndef _MATLAB
virtual bool imread(const char* filename);
virtual bool imwrite(const char* filename) const;
virtual bool imwrite(const char* filename,ImageIO::ImageType) const;
//virtual bool imread(const QString& filename);
//virtual void imread(const QImage& image);
//virtual bool imwrite(const QString& filename,int quality=100) const;
//virtual bool imwrite(const QString& filename,ImageIO::ImageType imagetype,int quality=100) const;
//virtual bool imwrite(const QString& fileanme,T min,T max,int quality=100) const;
#else
virtual bool imread(const char* filename) const {return true;};
virtual bool imwrite(const char* filename) const {return true;};
#endif
template <class T1>
Image<T1> dx (bool IsAdvancedFilter=false) const;
template <class T1>
void dx(Image<T1>& image,bool IsAdvancedFilter=false) const;
template<class T1>
Image<T1> dy(bool IsAdvancedFilter=false) const;
template <class T1>
void dy(Image<T1>& image,bool IsAdvancedFilter=false) const;
template <class T1>
void dxx(Image<T1>& image) const;
template <class T1>
void dyy(Image<T1>& image) const;
template <class T1>
void laplacian(Image<T1>& image) const;
template <class T1>
void gradientmag(Image<T1>& image) const;
void GaussianSmoothing(double sigma,int fsize);
template <class T1>
void GaussianSmoothing(Image<T1>& image,double sigma,int fsize) const;
template <class T1>
void GaussianSmoothing_transpose(Image<T1>& image,double sigma,int fsize) const;
template <class T1>
void smoothing(Image<T1>& image,double factor=4);
template <class T1>
Image<T1> smoothing(double factor=4);
void smoothing(double factor=4);
// funciton for filtering
template <class T1>
void imfilter(Image<T1>& image,const double* filter,int fsize) const;
template <class T1,class T2>
void imfilter(Image<T1>& image,const Image<T2>& kernel) const;
template <class T1>
Image<T1> imfilter(const double* filter,int fsize) const;
template <class T1>
void imfilter_h(Image<T1>& image,double* filter,int fsize) const;
template <class T1>
void imfilter_v(Image<T1>& image,double* filter,int fsize) const;
template <class T1>
void imfilter_hv(Image<T1>& image,const double* hfilter,int hfsize,const double* vfilter,int vfsize) const;
template<class T1>
void imfilter_hv(Image<T1>& image,const Image<double>& hfilter,const Image<double>& vfilter) const;
// funciton for filtering transpose
template <class T1>
void imfilter_transpose(Image<T1>& image,const double* filter,int fsize) const;
template <class T1,class T2>
void imfilter_transpose(Image<T1>& image,const Image<T2>& kernel) const;
template <class T1>
Image<T1> imfilter_transpose(const double* filter,int fsize) const;
template <class T1>
void imfilter_h_transpose(Image<T1>& image,double* filter,int fsize) const;
template <class T1>
void imfilter_v_transpose(Image<T1>& image,double* filter,int fsize) const;
template <class T1>
void imfilter_hv_transpose(Image<T1>& image,const double* hfilter,int hfsize,const double* vfilter,int vfsize) const;
template<class T1>
void imfilter_hv_transpose(Image<T1>& image,const Image<double>& hfilter,const Image<double>& vfilter) const;
// function to desaturating
template <class T1>
void desaturate(Image<T1>& image) const;
void desaturate();
template <class T1>
void collapse(Image<T1>& image,collapse_type type = collapse_average) const;
void collapse(collapse_type type = collapse_average);
void flip_horizontal(Image<T>& image);
void flip_horizontal();
// function to concatenate images
template <class T1,class T2>
void concatenate(Image<T1>& destImage,const Image<T2>& addImage) const;
template <class T1,class T2>
void concatenate(Image<T1>& destImage,const Image<T2>& addImage,double ratio) const;
template <class T1>
Image<T> concatenate(const Image<T1>& addImage) const;
// function to separate the channels of the image
template <class T1,class T2>
void separate(unsigned firstNChannels,Image<T1>& image1,Image<T2>& image2) const;
// function to sample patch
template <class T1>
void getPatch(Image<T1>& patch,double x,double y,int fsize) const;
// function to crop the image
template <class T1>
void crop(Image<T1>& patch,int Left,int Top,int Width,int Height) const;
// basic numerics of images
template <class T1,class T2>
void Multiply(const Image<T1>& image1,const Image<T2>& image2);
template <class T1,class T2>
void MultiplyAcross(const Image<T1>& image1,const Image<T2>& image2);
template <class T1,class T2,class T3>
void Multiply(const Image<T1>& image1,const Image<T2>& image2,const Image<T3>& image3);
template <class T1>
void Multiplywith(const Image<T1>& image1);
template <class T1>
void MultiplywithAcross(const Image<T1>& image1);
void Multiplywith(double value);
template <class T1,class T2>
void Add(const Image<T1>& image1,const Image<T2>& image2);
template <class T1,class T2>
void Add(const Image<T1>& image1,const Image<T2>& image2,double ratio);
void Add(const T value);
template <class T1>
void Add(const Image<T1>& image1,const double value);
template <class T1>
void Add(const Image<T1>& image1);
template <class T1,class T2>
void Subtract(const Image<T1>& image1,const Image<T2>& image2);
// arestmetic operators
void square();
// exp
void Exp(double sigma = 1);
// function to normalize an image
void normalize(Image<T>& image);
// function to threshold an image
void threshold();
// function to compute the statistics of the image
double norm2() const;
double sum() const;
template <class T1>
double innerproduct(Image<T1>& image) const;
// function to bilateral smooth flow field
template <class T1>
void BilateralFiltering(Image<T1>& other,int fsize,double filter_signa,double range_sigma);
// function to bilateral smooth an image
//Image<T> BilateralFiltering(int fsize,double filter_sigma,double range_sigma);
void imBilateralFiltering(Image<T>& result,int fsize,double filter_sigma,double range_sigma);
template <class T1,class T2>
int kmeansIndex(int pixelIndex,T1& minDistance,const T2* pDictionary,int nVocabulary, int nDim);
// convert an image into visual words based on a dictionary
template <class T1,class T2>
void ConvertToVisualWords(Image<T1>& result,const T2* pDictionary,int nDim,int nVocabulary);
// get the histogram of an image region
// the range is [0,imWidth] (x) and [0,imHeight] (y)
template <class T1>
Vector<T1> histogramRegion(int nBins,double left,double top,double right,double bottom) const;
// function for bicubic image interpolation
template <class T1>
inline void BicubicCoeff(double a[][4],const T* pIm,const T1* pImDx,const T1* pImDy,const T1* pImDxDy,const int offsets[][2]) const;
template <class T1,class T2>
void warpImageBicubic(Image<T>& output,const Image<T1>& imdx,const Image<T1>& imdy, const Image<T1>& imdxdy,const Image<T2>& vx,const Image<T2>& vy) const;
template <class T1>
void warpImageBicubic(Image<T>& output,const Image<T1>& vx,const Image<T1>& vy) const;
template <class T1>
void warpImageBicubicCoeff(Image<T1>& Coeff) const;
template <class T1,class T2>
void warpImageBicubic(Image<T>& output,const Image<T1>& coeff,const Image<T2>& vx,const Image<T2>& vy) const;
template <class T1,class T2>
void warpImageBicubicRef(const Image<T>& ref,Image<T>& output,const Image<T1>& imdx,const Image<T1>& imdy, const Image<T1>& imdxdy,const Image<T2>& vx,const Image<T2>& vy) const;
template <class T1>
void warpImageBicubicRef(const Image<T>& ref,Image<T>& output,const Image<T1>& vx,const Image<T1>& vy) const;
template <class T1,class T2>
void warpImageBicubicRef(const Image<T>& ref,Image<T>& output,const Image<T1>& coeff,const Image<T2>& vx,const Image<T2>& vy) const;
template <class T1>
void warpImageBicubicRef(const Image<T>& ref,Image<T>& output,const Image<T1>& flow) const;
template <class T1>
void DissembleFlow(Image<T1>& vx,Image<T1>& vy) const;
// function for image warping
template <class T1>
void warpImage(Image<T>& output,const Image<T1>& vx,const Image<T1>& vy) const;
// function for image warping transpose
template <class T1>
void warpImage_transpose(Image<T>& output,const Image<T1>& vx,const Image<T1>& vy) const;
// function for image warping
template <class T1>
void warpImage(Image<T>& output,const Image<T1>& flow) const;
// function for image warping transpose
template <class T1>
void warpImage_transpose(Image<T>& output,const Image<T1>& flow) const;
// function to get the max
T max() const;
// function to get min
T min() const;
void generate2DGuasisan(int winsize,double sigma)
{
clear();
imWidth = imHeight = winsize*2+1;
nChannels = 1;
computeDimension();
ImageProcessing::generate2DGaussian(pData,winsize,sigma);
}
void generate1DGaussian(int winsize,double sigma)
{
clear();
imWidth = winsize*2+1;
imHeight = 1;
nChannels = 1;
computeDimension();
ImageProcessing::generate1DGaussian(pData,winsize,sigma);
}
template <class T1>
void subSampleKernelBy2(Image<T1>& output) const
{
int winsize = (imWidth-1)/2;
int winsize_s = winsize/2;
int winlen_s = winsize_s*2+1;
if(!output.matchDimension(winlen_s,1,1))
output.allocate(winlen_s,1,1);
output.pData[winsize_s] = pData[winsize];
for(int i = 0;i<winsize_s;i++)
{
output.pData[winsize_s+1+i] = pData[winsize+2+2*i];
output.pData[winsize_s-1-i] = pData[winsize-2-2*i];
}
output.Multiplywith(1/output.sum());
}
void addAWGN(double noiseLevel = 0.05)
{
for(int i = 0;i<nElements;i++)
pData[i] += CStochastic::GaussianSampling()*noiseLevel;
}
// file IO
#ifndef _MATLAB
//bool writeImage(QFile& file) const;
//bool readImage(QFile& file);
//bool writeImage(const QString& filename) const;
//bool readImage(const QString& filename);
#endif
#ifdef _MATLAB
bool LoadMatlabImage(const mxArray* image,bool IsImageScaleCovnersion=true);
template <class T1>
void LoadMatlabImageCore(const mxArray* image,bool IsImageScaleCovnersion=true);
template <class T1>
void ConvertFromMatlab(const T1* pMatlabPlane,int _width,int _height,int _nchannels);
void OutputToMatlab(mxArray*& matrix) const;
template <class T1>
void ConvertToMatlab(T1* pMatlabPlane) const;
#endif
};
typedef Image<unsigned char> BiImage;
typedef Image<unsigned char> UCImage;
typedef Image<short int> IntImage;
typedef Image<float> FImage;
typedef Image<double> DImage;
//------------------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------------------
template <class T>
Image<T>::Image()
{
pData=NULL;
imWidth=imHeight=nChannels=nPixels=nElements=0;
IsDerivativeImage=false;
}
//------------------------------------------------------------------------------------------
// constructor with specified dimensions
//------------------------------------------------------------------------------------------
template <class T>
Image<T>::Image(int width,int height,int nchannels)
{
imWidth=width;
imHeight=height;
nChannels=nchannels;
computeDimension();
pData=NULL;
pData=new T[nElements];
if(nElements>0)
memset(pData,0,sizeof(T)*nElements);
IsDerivativeImage=false;
}
template <class T>
Image<T>::Image(const T& value,int _width,int _height,int _nchannels)
{
pData=NULL;
allocate(_width,_height,_nchannels);
setValue(value);
}
#ifndef _MATLAB
//template <class T>
//Image<T>::Image(const QImage& image)
//{
// pData=NULL;
// imread(image);
//}
#endif
template <class T>
void Image<T>::allocate(int width,int height,int nchannels)
{
clear();
imWidth=width;
imHeight=height;
nChannels=nchannels;
computeDimension();
pData=NULL;
if(nElements>0)
{
pData=new T[nElements];
memset(pData,0,sizeof(T)*nElements);
}
}
template <class T>
template <class T1>
void Image<T>::allocate(const Image<T1> &other)
{
allocate(other.width(),other.height(),other.nchannels());
IsDerivativeImage = other.isDerivativeImage();
colorType = other.colortype();
}
//------------------------------------------------------------------------------------------
// copy constructor
//------------------------------------------------------------------------------------------
template <class T>
Image<T>::Image(const Image<T>& other)
{
imWidth=imHeight=nChannels=nElements=0;
pData=NULL;
copyData(other);
}
//------------------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------------------
template <class T>
Image<T>::~Image()
{
if(pData!=NULL)
delete []pData;
}
//------------------------------------------------------------------------------------------
// clear the image
//------------------------------------------------------------------------------------------
template <class T>
void Image<T>::clear()
{
if(pData!=NULL)
delete []pData;
pData=NULL;
imWidth=imHeight=nChannels=nPixels=nElements=0;
}
//------------------------------------------------------------------------------------------
// reset the image (reset the buffer to zero)
//------------------------------------------------------------------------------------------
template <class T>
void Image<T>::reset()
{
if(pData!=NULL)
memset(pData,0,sizeof(T)*nElements);
}
template <class T>
void Image<T>::setValue(const T &value)
{
for(int i=0;i<nElements;i++)
pData[i]=value;
}
template <class T>
void Image<T>::setValue(const T& value,int _width,int _height,int _nchannels)
{
if(imWidth!=_width || imHeight!=_height || nChannels!=_nchannels)
allocate(_width,_height,_nchannels);
setValue(value);
}
//------------------------------------------------------------------------------------------
// copy from other image
//------------------------------------------------------------------------------------------
template <class T>
void Image<T>::copyData(const Image<T>& other)
{
imWidth=other.imWidth;
imHeight=other.imHeight;
nChannels=other.nChannels;
nPixels=other.nPixels;
IsDerivativeImage=other.IsDerivativeImage;
colorType = other.colorType;
if(nElements!=other.nElements)
{
nElements=other.nElements;
if(pData!=NULL)
delete []pData;
pData=NULL;
pData=new T[nElements];
}
if(nElements>0)
memcpy(pData,other.pData,sizeof(T)*nElements);
}
template <class T>
template <class T1>
void Image<T>::copy(const Image<T1>& other)
{
clear();
imWidth=other.width();
imHeight=other.height();
nChannels=other.nchannels();
computeDimension();
IsDerivativeImage=other.isDerivativeImage();
colorType = other.colortype();
pData=NULL;
pData=new T[nElements];
const T1*& srcData=other.data();
for(int i=0;i<nElements;i++)
pData[i]=srcData[i];
}
template <class T>
void Image<T>::im2double()
{
if(IsFloat())
for(int i=0;i<nElements;i++)
pData[i]/=255;
}
//------------------------------------------------------------------------------------------
// override equal operator
//------------------------------------------------------------------------------------------
template <class T>
Image<T>& Image<T>::operator=(const Image<T>& other)
{
copyData(other);
return *this;
}
template <class T>
bool Image<T>::IsFloat() const
{
if(typeid(T)==typeid(float) || typeid(T)==typeid(double) || typeid(T)==typeid(long double))
return true;
else
return false;
}
template <class T>
template <class T1>
bool Image<T>::matchDimension(const Image<T1>& image) const
{
if(imWidth==image.width() && imHeight==image.height() && nChannels==image.nchannels())
return true;
else
return false;
}
template <class T>
bool Image<T>::matchDimension(int width, int height, int nchannels) const
{
if(imWidth==width && imHeight==height && nChannels==nchannels)
return true;
else
return false;
}
//------------------------------------------------------------------------------------------
// function to move this image to a dest image at (x,y) with specified width and height
//------------------------------------------------------------------------------------------
template <class T>
template <class T1>
void Image<T>::moveto(Image<T1>& image,int x0,int y0,int width,int height)
{
if(width==0)
width=imWidth;
if(height==0)
height=imHeight;
int NChannels=__min(nChannels,image.nchannels());
int x,y;
for(int i=0;i<height;i++)
{
y=y0+i;
if(y>=image.height())
break;
for(int j=0;j<width;j++)
{
x=x0+j;
if(x>=image.width())
break;
for(int k=0;k<NChannels;k++)
image.data()[(y*image.width()+x)*image.nchannels()+k]=pData[(i*imWidth+j)*nChannels+k];
}
}
}
//------------------------------------------------------------------------------------------
// resize the image
//------------------------------------------------------------------------------------------
template <class T>
bool Image<T>::imresize(double ratio)
{
if(pData==NULL)
return false;
T* pDstData;
int DstWidth,DstHeight;
DstWidth=(double)imWidth*ratio;
DstHeight=(double)imHeight*ratio;
pDstData=new T[DstWidth*DstHeight*nChannels];
ImageProcessing::ResizeImage(pData,pDstData,imWidth,imHeight,nChannels,ratio);
delete []pData;
pData=pDstData;
imWidth=DstWidth;
imHeight=DstHeight;
computeDimension();
return true;
}
template <class T>
template <class T1>
void Image<T>::imresize(Image<T1>& result,double ratio) const
{
int DstWidth,DstHeight;
DstWidth=(double)imWidth*ratio;
DstHeight=(double)imHeight*ratio;
if(result.width()!=DstWidth || result.height()!=DstHeight || result.nchannels()!=nChannels)
result.allocate(DstWidth,DstHeight,nChannels);
else
result.reset();
ImageProcessing::ResizeImage(pData,result.data(),imWidth,imHeight,nChannels,ratio);
}
template <class T>
template <class T1>
void Image<T>::imresize(Image<T1>& result,int DstWidth,int DstHeight) const
{
if(result.width()!=DstWidth || result.height()!=DstHeight || result.nchannels()!=nChannels)
result.allocate(DstWidth,DstHeight,nChannels);
else
result.reset();
ImageProcessing::ResizeImage(pData,result.data(),imWidth,imHeight,nChannels,DstWidth,DstHeight);
}
template <class T>
void Image<T>::imresize(int dstWidth,int dstHeight)
{
DImage foo(dstWidth,dstHeight,nChannels);
ImageProcessing::ResizeImage(pData,foo.data(),imWidth,imHeight,nChannels,dstWidth,dstHeight);
copyData(foo);
}
template <class T>
template <class T1>
void Image<T>::upSampleNN(Image<T1>& output,int ratio) const
{
int width = imWidth*ratio;
int height = imHeight*ratio;
if(!output.matchDimension(width,height,nChannels))
output.allocate(width,height,nChannels);
for(int i = 0; i <imHeight; i++)
for(int j = 0; j<imWidth; j++)
{
int offset = (i*imWidth+j)*nChannels;
for(int ii = 0 ;ii<ratio;ii++)
for(int jj=0;jj<ratio;jj++)
{
int offset1 = ((i*ratio+ii)*width+j*ratio+jj)*nChannels;
for(int k = 0; k<nChannels; k++)
output.data()[offset1+k] = pData[offset+k];
}
}
}
//------------------------------------------------------------------------------------------
// function of reading or writing images (uncompressed)
//------------------------------------------------------------------------------------------
template <class T>
bool Image<T>::saveImage(const char *filename) const
{
ofstream myfile(filename,ios::out | ios::binary);
if(myfile.is_open())
{
bool foo = saveImage(myfile);
myfile.close();
return foo;
}
else
return false;
}
template <class T>
bool Image<T>::saveImage(ofstream& myfile) const
{
char type[16];
sprintf(type,"%s",typeid(T).name());
myfile.write(type,16);
myfile.write((char *)&imWidth,sizeof(int));
myfile.write((char *)&imHeight,sizeof(int));
myfile.write((char *)&nChannels,sizeof(int));
myfile.write((char *)&IsDerivativeImage,sizeof(bool));
myfile.write((char *)pData,sizeof(T)*nElements);
return true;
}
template <class T>
bool Image<T>::loadImage(const char *filename)
{
ifstream myfile(filename, ios::in | ios::binary);
if(myfile.is_open())
{
bool foo = loadImage(myfile);
myfile.close();
return foo;
}
else
return false;
}
template <class T>
bool Image<T>::loadImage(ifstream& myfile)
{
char type[16];
myfile.read(type,16);
#ifdef _LINUX_MAC
if(strcasecmp(type,"uint16")==0)
sprintf(type,"unsigned short");
if(strcasecmp(type,"uint32")==0)
sprintf(type,"unsigned int");
if(strcasecmp(type,typeid(T).name())!=0)
{
cout<<"The type of the image is different from the type of the object!"<<endl;
return false;
}
#else
if(strcmpi(type,"uint16")==0)
sprintf(type,"unsigned short");
if(strcmpi(type,"uint32")==0)
sprintf(type,"unsigned int");
if(strcmpi(type,typeid(T).name())!=0)
{
cout<<"The type of the image is different from the type of the object!"<<endl;
return false;
}
#endif
int width,height,nchannels;
myfile.read((char *)&width,sizeof(int));
myfile.read((char *)&height,sizeof(int));
myfile.read((char *)&nchannels,sizeof(int));
if(!matchDimension(width,height,nchannels))
allocate(width,height,nchannels);
myfile.read((char *)&IsDerivativeImage,sizeof(bool));
myfile.read((char *)pData,sizeof(T)*nElements);
return true;
}
//------------------------------------------------------------------------------------------
// function to load the image
//------------------------------------------------------------------------------------------
#ifndef _MATLAB
template <class T>
bool Image<T>::imread(const char* filename)
{
clear();
if(ImageIO::loadImage(filename,pData,imWidth,imHeight,nChannels))
{
computeDimension();
colorType = BGR; // when we use qt or opencv to load the image, it's often BGR
return true;
}
return false;
}
//template <class T>
//bool Image<T>::imread(const QString &filename)
//{
// clear();
// if(ImageIO::loadImage(filename,pData,imWidth,imHeight,nChannels))
// {
// computeDimension();
// return true;
// }
// return false;
//}
//
//template <class T>
//void Image<T>::imread(const QImage& image)
//{
// clear();
// ImageIO::loadImage(image,pData,imWidth,imHeight,nChannels);
// computeDimension();
//}
//
//------------------------------------------------------------------------------------------
// function to write the image
//------------------------------------------------------------------------------------------
template <class T>
bool Image<T>::imwrite(const char* filename) const
{
ImageIO::ImageType type;
if(IsDerivativeImage)
type=ImageIO::derivative;
else
type=ImageIO::standard;
return ImageIO::saveImage(filename,pData,imWidth,imHeight,nChannels,type);
}
template <class T>
bool Image<T>::imwrite(const char* filename,ImageIO::ImageType type) const
{
return ImageIO::saveImage(filename,pData,imWidth,imHeight,nChannels,type);
}
//template <class T>
//bool Image<T>::imwrite(const QString &filename, ImageIO::ImageType imagetype, int quality) const
//{
// return ImageIO::writeImage(filename,(const T*&)pData,imWidth,imHeight,nChannels,imagetype,quality);
//}
//
//template <class T>
//bool Image<T>::imwrite(const QString &filename, T min, T max, int quality) const
//{
// return ImageIO::writeImage(filename,(const T*&)pData,imWidth,imHeight,nChannels,min,max,quality);
//}
#endif
//------------------------------------------------------------------------------------------
// function to get x-derivative of the image
//------------------------------------------------------------------------------------------
template <class T>
template <class T1>
void Image<T>::dx(Image<T1>& result,bool IsAdvancedFilter) const
{
if(matchDimension(result)==false)
result.allocate(imWidth,imHeight,nChannels);
result.reset();
result.setDerivative();
T1*& data=result.data();
int i,j,k,offset;
if(IsAdvancedFilter==false)
for(i=0;i<imHeight;i++)
for(j=0;j<imWidth-1;j++)
{
offset=i*imWidth+j;
for(k=0;k<nChannels;k++)
data[offset*nChannels+k]=(T1)pData[(offset+1)*nChannels+k]-pData[offset*nChannels+k];
}
else
{
double xFilter[5]={1,-8,0,8,-1};
for(i=0;i<5;i++)
xFilter[i]/=12;
ImageProcessing::hfiltering(pData,data,imWidth,imHeight,nChannels,xFilter,2);
}
}
template <class T>
template <class T1>
Image<T1> Image<T>::dx(bool IsAdvancedFilter) const
{
Image<T1> result;
dx<T1>(result,IsAdvancedFilter);
return result;
}
//------------------------------------------------------------------------------------------
// function to get y-derivative of the image
//------------------------------------------------------------------------------------------
template <class T>
template <class T1>
void Image<T>::dy(Image<T1>& result,bool IsAdvancedFilter) const
{
if(matchDimension(result)==false)
result.allocate(imWidth,imHeight,nChannels);
result.setDerivative();
T1*& data=result.data();
int i,j,k,offset;
if(IsAdvancedFilter==false)
for(i=0;i<imHeight-1;i++)
for(j=0;j<imWidth;j++)
{
offset=i*imWidth+j;
for(k=0;k<nChannels;k++)
data[offset*nChannels+k]=(T1)pData[(offset+imWidth)*nChannels+k]-pData[offset*nChannels+k];
}