-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVImage.h
1152 lines (899 loc) · 32.9 KB
/
CVImage.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
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
/*****************************************************************************
********************************* CVImage.h **********************************
*****************************************************************************/
#if !defined(__CVIMAGE_H__)
#define __CVIMAGE_H__
#pragma once
/*****************************************************************************
****************************** I N C L U D E *******************************
*****************************************************************************/
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
//#include "DConfig.h"
/*****************************************************************************
******************************* class DCVImage *******************************
*****************************************************************************/
class DCVImage : public cv::Mat
{
public :
// Image combination styles
enum class ECombine
{
eSideBySide,
eStacked
};
DCVImage() = default;
DCVImage(int nWidth, int nHeight, int nType)
: cv::Mat(nHeight, nWidth, nType)
{
return;
}
DCVImage(const DCVImage& src) : cv::Mat(src)
{
return;
}
DCVImage(const cv::Mat& src) : cv::Mat(src)
{
return;
}
DCVImage(const DCVImage* pImage1, const DCVImage* pImage2, ECombine eCombine = ECombine::eSideBySide,
int nDivider = 0, cv::Scalar FillColor = CV_RGB(0, 0, 0))
{
Combine(pImage1, pImage2, eCombine, nDivider, FillColor);
return;
}
~DCVImage() = default;
// Assignment operators
DCVImage& operator=(const DCVImage& rhs)
{
cv::Mat::operator=(rhs);
return (*this);
}
DCVImage& operator=(const cv::Mat& rhs)
{
cv::Mat::operator=(rhs);
return (*this);
}
DCVImage& operator=(const cv::MatExpr& rhs)
{
cv::Mat::operator=(rhs);
return (*this);
}
DCVImage& operator=(const cv::Scalar& s)
{
cv::Mat::operator=(s);
return (*this);
}
// Create the image if it hasn't been already
void Create(int nWidth, int nHeight, int nType)
{
cv::Mat::create(nHeight, nWidth, nType);
return;
}
// Handle a deep copy
cv::Mat Clone() const
{
return (cv::Mat::clone());
}
bool IsEmpty() const
{
return (empty());
}
bool IsValid() const
{
return (!IsEmpty());
}
// Copy this image to another one converting to this type
// plus possible scaling and shifting
void ConvertScale(cv::Mat& Dst, int nType, double dScale = 1.0, double dShift = 0.0) const
{
cv::Mat::convertTo(Dst, nType, dScale, dShift);
return;
}
// Copy this image to another and convert the color space
void CvtColor(cv::Mat& Dst, int nConversion, int nDstCn = 0)
{
// !!!TTC This has a new parameter, check it out
cv::cvtColor(*this, Dst, nConversion, nDstCn);
return;
}
/*
Copy this image to the destination and add a border of the specified
type. If the image is to be centered, the size of this image should be
increased by twice the offset values.
*/
enum class EBorderType
{
eBorderConstant = IPL_BORDER_CONSTANT,
eBorderReplicate = IPL_BORDER_REPLICATE,
#if 0 // Not implemented currently
eBorderReflect = IPL_BORDER_REFLECT,
eBorderWrap = IPL_BORDER_WRAP,
#endif
};
#if 0
void CopyMakeBorder(cv::Mat& Dst, int nTop, int nBottom, int nLeft,
int nRight, EBorderType eType,
const cv::Scalar& Value = cv::Scalar()) const
{
cv::copyMakeBorder(*this, Dst, nTop, nBottom, nLeft, nRight, eType,
Value);
return;
}
#endif
/***********************************************************************
*************************** File Operations ****************************
***********************************************************************/
enum EReadParam
{
eReadUnchanged = -1,
eReadGrayScale = CV_LOAD_IMAGE_GRAYSCALE,
eReadColor = CV_LOAD_IMAGE_COLOR,
eReadAnyDepth = CV_LOAD_IMAGE_ANYDEPTH,
eReadAnyColor = 4,
};
bool ReadImage(const std::string& szFileName, int nFlags = EReadParam::eReadUnchanged)
{
DCVImage Temp = cv::imread(szFileName, nFlags);
bool bRet = Temp.IsValid();
// Only modify the current image if read was successful
if (bRet)
{
*this = Temp;
} // end if
return (bRet);
}
bool ReadImage(const char* szFileName, int nFlags = eReadColor)
{
return (ReadImage(std::string(szFileName), nFlags));
}
enum EWriteParam
{
eJPGQuality = cv::IMWRITE_JPEG_QUALITY,
ePNGCompression = cv::IMWRITE_PNG_COMPRESSION,
ePXMBinary = cv::IMWRITE_PXM_BINARY,
};
/***********************************************************************
************************** class DWriteParams **************************
***********************************************************************/
class DWriteParams : public std::vector<int>
{
public :
DWriteParams() : std::vector<int>()
{
return;
}
DWriteParams(const DWriteParams& src) : std::vector<int>(src)
{
return;
}
~DWriteParams() = default;
DWriteParams& operator=(const DWriteParams& rhs)
{
std::vector<int>::operator=(rhs);
return (*this);
}
bool IsValid() const
{
// Size must be even including zero
return ((size() & 1) == 0);
}
// Parameter "name" is in odd index followed by value
void SetParameter(EWriteParam Param, int nValue)
{
push_back(Param);
push_back(nValue);
return;
}
}; // End of class DWriteParams
/***********************************************************************
************************ class DJPGWriteParams *************************
***********************************************************************/
class DJPGWriteParams : public DWriteParams
{
public :
DJPGWriteParams()
{
// Make the OpenCV default obvious
SetQuality(95);
return;
}
DJPGWriteParams(int nQuality)
{
SetQuality(nQuality);
return;
}
DJPGWriteParams(const DJPGWriteParams& src) : DWriteParams(src)
{
return;
}
~DJPGWriteParams() = default;
DJPGWriteParams& operator=(const DJPGWriteParams& rhs)
{
DWriteParams::operator=(rhs);
return (*this);
}
void SetQuality(int nQuality)
{
SetParameter(eJPGQuality, nQuality);
return;
}
}; // End of class DJPGWriteParams
/***********************************************************************
************************ class DPNGWriteParams *************************
***********************************************************************/
class DPNGWriteParams : public DWriteParams
{
public :
DPNGWriteParams()
{
// Make the OpenCV default obvious
SetCompression(0);
return;
}
DPNGWriteParams(int nCompression)
{
SetCompression(nCompression);
return;
}
DPNGWriteParams(const DPNGWriteParams& src) : DWriteParams(src)
{
return;
}
~DPNGWriteParams() = default;
DPNGWriteParams& operator=(const DPNGWriteParams& rhs)
{
DWriteParams::operator=(rhs);
return (*this);
}
void SetCompression(int nCompression)
{
SetParameter(ePNGCompression, nCompression);
return;
}
}; // End of class DPNGWriteParams
bool WriteImage(const std::string& szFileName, const std::vector<int>& Params = std::vector<int>()) const
{
return (cv::imwrite(szFileName, *this, Params));
}
bool WriteImage(const char* szFileName, const std::vector<int>& Params = std::vector<int>()) const
{
return (WriteImage(std::string(szFileName), Params));
}
/***********************************************************************
*************************** Image Properties ***************************
***********************************************************************/
int GetHeight() const
{
return (rows);
}
int GetNumRows() const
{
return (rows);
}
int GetWidth() const
{
return (cols);
}
int GetNumCols() const
{
return (cols);
}
size_t GetWidthStep() const
{
return (step);
}
// Returns CV_8U, CV_8S, CV_16U,....
int GetDepth() const
{
return (depth());
}
// Returns CV_8UC1, CV_8SC3, CV_16UC1, CV_MAKETYPE(depth, cn), ....
int GetType() const
{
return (type());
}
int GetNumChannels() const
{
return (channels());
}
bool IsPixelDataSigned() const
{
int nDepth = GetDepth();
return (!((nDepth == CV_8U) || (nDepth == CV_16U)));
}
// Number of bits in each color channel
size_t GetPixelDepth() const
{
return ((elemSize() / GetNumChannels()) * 8);
}
// Pixel size in bytes
size_t GetPixelSize() const
{
return (elemSize());
}
// Data size in bytes
size_t GetDataSize() const
{
return (GetNumRows() * GetWidthStep());
}
// Total number of elements in matrix or pixels in image
size_t GetNumElements() const
{
return (total());
}
/***********************************************************************
***************************** Pixel Access *****************************
***********************************************************************/
/* Deprecated in favor of the ptr template functions built into cv::Mat */
unsigned char* GetPixels()
{
return (data);
}
const unsigned char* GetPixels() const
{
return (data);
}
unsigned char* GetRow(int nRow)
{
return (GetPixels() + nRow * GetWidthStep());
}
const unsigned char* GetRow(int nRow) const
{
return (GetPixels() + nRow * GetWidthStep());
}
unsigned char* GetPixel(int nRow, int nCol)
{
return (GetRow(nRow) + nCol * GetPixelSize());
}
const unsigned char* GetPixel(int nRow, int nCol) const
{
return (GetRow(nRow) + nCol * GetPixelSize());
}
unsigned char* GetPixel(unsigned char* pRow, int nCol)
{
return (pRow + nCol * GetPixelSize());
}
const unsigned char* GetPixel(const unsigned char* pRow, int nCol) const
{
return (pRow + nCol * GetPixelSize());
}
bool CopyPixels(const IplImage* pImage)
{
bool bRet = (pImage->imageData != nullptr);
if (bRet)
{
memcpy(GetPixels(), pImage->imageData, GetDataSize());
} // end if
return (bRet);
}
// Copy BRG to RGB (primarily for display support)
void CopyPixelsToRGB(unsigned char* pPixelsOut) const;
/***********************************************************************
************************* Algebraic Operations *************************
***********************************************************************/
/* Generally deprecated in favor of cv::Mat native operations */
// This matrix contains the result of adding two matrices
void Add(const cv::Mat& Mat1, const cv::Mat& Mat2, const cv::Mat& Mask = Mat())
{
cv::add(Mat1, Mat2, *this, Mask);
return;
}
// Add a matrix to this matrix
void Add(const cv::Mat& Mat, const cv::Mat& Mask = Mat())
{
cv::add(Mat, *this, *this, Mask);
return;
}
// This matrix contains the results of adding a scalar to a matrix
void Add(const cv::Mat& Mat, const cv::Scalar& Value, const cv::Mat& Mask = Mat())
{
cv::add(Mat, Value, *this, Mask);
return;
}
// Add a scalar to this matrix
void Add(const cv::Scalar& Value, const cv::Mat& Mask = Mat())
{
cv::add(*this, Value, *this, Mask);
return;
}
// This matrix contains the results of a weighted and scaled addition
// of two matrices
void AddWeighted(const cv::Mat& Mat1, double dAlpha, const cv::Mat& Mat2, double dBeta, double dGamma)
{
cv::addWeighted(Mat1, dAlpha, Mat2, dBeta, dGamma, *this);
return;
}
// This matrix contains the result of subtracting two matrices
void Subtract(const cv::Mat& Mat1, const cv::Mat& Mat2, const cv::Mat& Mask = Mat())
{
cv::subtract(Mat1, Mat2, *this, Mask);
return;
}
// Subtract a matrix to this matrix
void Subtract(const cv::Mat& Mat, const cv::Mat& Mask = Mat())
{
cv::subtract(Mat, *this, *this, Mask);
return;
}
// This matrix contains the results of subtracting a scalar to a matrix
void Subtract(const cv::Mat& Mat, const cv::Scalar& Value, const cv::Mat& Mask = Mat())
{
cv::subtract(Mat, Value, *this, Mask);
return;
}
// Subtract a scalar to this matrix
void Subtract(const cv::Scalar& Value, const cv::Mat& Mask = Mat())
{
cv::subtract(*this, Value, *this, Mask);
return;
}
/***********************************************************************
************************** Logical Operations **************************
***********************************************************************/
// This image is computed from the logical and of the inputs plus mask
void And(const cv::Mat& Mat1, const cv::Mat& Mat2, const cv::Mat& Mask = Mat())
{
cv::bitwise_and(Mat1, Mat2, *this, Mask);
return;
}
// This image is anded with the supplied image and mask
void And(const cv::Mat& Mat, const cv::Mat& Mask = Mat())
{
cv::bitwise_and(*this, Mat, *this, Mask);
return;
}
// This matrix contins the result of logical anding a matrix and a
// scalar
void And(const cv::Mat& Mat, const cv::Scalar& Value, const cv::Mat& Mask = Mat())
{
cv::bitwise_and(Mat, Value, *this, Mask);
return;
}
// Logically and this matrix and a scalar
void And(const cv::Scalar& Value, const cv::Mat& Mask = Mat())
{
cv::bitwise_and(*this, Value, *this, Mask);
return;
}
// This image is computed from the logical or of the inputs plus mask
void Or(const cv::Mat& Mat1, const cv::Mat& Mat2, const cv::Mat& Mask = Mat())
{
cv::bitwise_or(Mat1, Mat2, *this, Mask);
return;
}
// This image is ored with the supplied image and mask
void Or(const cv::Mat& Mat, const cv::Mat& Mask = Mat())
{
cv::bitwise_or(*this, Mat, *this, Mask);
return;
}
// This matrix contins the result of logical oring a matrix and a
// scalar
void Or(const cv::Mat& Mat, const cv::Scalar& Value, const cv::Mat& Mask = Mat())
{
cv::bitwise_or(Mat, Value, *this, Mask);
return;
}
// Logically or this matrix and a scalar
void Or(const cv::Scalar& Value, const cv::Mat& Mask = Mat())
{
cv::bitwise_or(*this, Value, *this, Mask);
return;
}
#
// This image is computed from the logical xor of the inputs plus mask
void Xor(const cv::Mat& Mat1, const cv::Mat& Mat2, const cv::Mat& Mask = Mat())
{
cv::bitwise_xor(Mat1, Mat2, *this, Mask);
return;
}
// This image is xored with the supplied image and mask
void Xor(const cv::Mat& Mat, const cv::Mat& Mask = Mat())
{
cv::bitwise_xor(*this, Mat, *this, Mask);
return;
}
// This matrix contins the result of logical xoring a matrix and a
// scalar
void Xor(const cv::Mat& Mat, const cv::Scalar& Value, const cv::Mat& Mask = Mat())
{
cv::bitwise_xor(Mat, Value, *this, Mask);
return;
}
// Logically xor this matrix and a scalar
void Xor(const cv::Scalar& Value, const cv::Mat& Mask = Mat())
{
cv::bitwise_xor(*this, Value, *this, Mask);
return;
}
// This matrix contains the bitwise not of the source matrix
void Not(const cv::Mat& Mat)
{
cv::bitwise_not(Mat, *this);
return;
}
/***********************************************************************
************************ Block Memory Operations ***********************
***********************************************************************/
// Copy the matrix contents to the destination
void CopyTo(cv::Mat& Dst) const
{
copyTo(Dst);
return;
}
void CopyTo(cv::Mat& Dst, const cv::Mat& Mask) const
{
copyTo(Dst, Mask);
return;
}
// Clear the image to black
void Clear()
{
memset(GetPixels(), 0, GetDataSize());
return;
}
// Flip the layout of this matrix and store in the destination
enum EFlipMode { eFlipX = 0, eFlipY = 1, eFlipXY = -1 };
void Flip(cv::Mat& Dst, EFlipMode eMode)
{
cv::flip(*this, Dst, eMode);
return;
}
/***********************************************************************
************************** Drawing Functions ***************************
***********************************************************************/
void Line(cv::Point pt1, cv::Point pt2, const cv::Scalar& Color,
int nThickness = 1, int nLineType = 8, int nShift = 0)
{
cv::line(*this, pt1, pt2, Color, nThickness, nLineType, nShift);
return;
}
void Rectangle(cv::Point pt1, cv::Point pt2, const cv::Scalar& Color,
int nThickness = 1, int nLineType = 8, int nShift = 0)
{
cv::rectangle(*this, pt1, pt2, Color, nThickness, nLineType, nShift);
return;
}
void Rectangle(cv::Rect rect, const cv::Scalar& Color,
int nThickness = 1, int nLineType = 8, int nShift = 0)
{
cv::rectangle(*this, rect, Color, nThickness, nLineType, nShift);
return;
}
void RectangleFilled(cv::Point Pt1, cv::Point Pt2,
const cv::Scalar& Color, int nLineType = 8, int nShift = 0)
{
Rectangle(Pt1, Pt2, Color, CV_FILLED, nLineType, nShift);
return;
}
void Circle(cv::Point Center, int nRadius, const cv::Scalar& Color,
int nThickness = 1, int nLineType = 8, int nShift = 0)
{
cv::circle(*this, Center, nRadius, Color, nThickness, nLineType, nShift);
return;
}
void CircleFilled(cv::Point Center, int nRadius, const cv::Scalar& Color, int nLineType = 8, int nShift = 0)
{
Circle(Center, nRadius, Color, CV_FILLED, nLineType, nShift);
return;
}
// Angles are in degrees
void Ellipse(cv::Point Center, cv::Size Axes, double dAngle, double dStartAngle, double dEndAngle,
const cv::Scalar& Color, int nThickness = 1, int nLineType = 8, int nShift = 0)
{
cv::ellipse(*this, Center, Axes, dAngle, dStartAngle, dEndAngle, Color, nThickness, nLineType, nShift);
return;
}
void EllipseFilled(cv::Point Center, cv::Size Axes, double dAngle, double dStartAngle, double dEndAngle,
const cv::Scalar& Color, int nLineType = 8, int nShift = 0)
{
Ellipse(Center, Axes, dAngle, dStartAngle, dEndAngle, Color, CV_FILLED, nLineType, nShift);
return;
}
void PolygonsFilled(const cv::Point** ppPts, const int* pnPts, int nContours, const cv::Scalar& Color,
int nLineType = 8, int nShift = 0, cv::Point Offset = cv::Point())
{
cv::fillPoly(*this, ppPts, pnPts, nContours, Color, nLineType, nShift, Offset);
return;
}
void PolygonConvexFilled(const cv::Point* pPts, int nPts, const cv::Scalar& Color, int nLineType = 8,
int nShift = 0)
{
cv::fillConvexPoly(*this, pPts, nPts, Color, nLineType, nShift);
return;
}
void PolyLines(const cv::Point** ppPts, const int* pnPts, int nContours, bool bIsClosed, const cv::Scalar& Color,
int nThickness, int nLineType = 8, int nShift = 0)
{
cv::polylines(*this, ppPts, pnPts, nContours, bIsClosed, Color, nThickness, nLineType, nShift);
return;
}
// Origin is the lower left corner of the bounding box
void PutText(const std::string& szText, cv::Point Origin, int nFontFace, double dFontScale,
const cv::Scalar& Color, int nThickness = 1, int nLineType = 8, bool bBottomLeftOrigin = false)
{
cv::putText(*this, szText, Origin, nFontFace, dFontScale, Color, nThickness, nLineType, bBottomLeftOrigin);
return;
}
#if 0
enum EInterpolation
{
eInterNearestNeighbor = cv::INTER_NN,
eInterBilinear = cv::INTER_LINEAR,
eInterArea = cv::INTER_AREA,
eInterBicubic = cv::INTER_CUBIC,
};
/***********************************************************************
********************** Gaussian Pyramid Operations *********************
***********************************************************************/
void PyramidUp(cv::Mat& Dst, const cv::Size& DstSize= cv::Size())
{
cv::pyrUp(*this, Dst, DstSize);
return;
}
void PyramidDown(cv::Mat& Dst, const cv::Size& DstSize= cv::Size())
{
cv::pyrDown(*this, Dst, DstSize);
return;
}
#endif
// Color order for color images
enum EColor { eBlue, eGreen, eRed };
bool Combine(const DCVImage* pImage1, const DCVImage* pImage2, ECombine eCombine = ECombine::eSideBySide,
int nDivider = 0, cv::Scalar FillColor = CV_RGB(0, 0, 0));
protected :
// Pixel conversion functions
// (8 bit unsigned is the most common display)
static unsigned char S8ToU8(signed char nIn)
{
short int nTemp = static_cast<short int>(nIn) + 128;
return (static_cast<unsigned char>(nTemp));
}
static unsigned char S16ToU8(signed short nIn)
{
int nTemp = static_cast<int>(nIn) + 32768;
return (static_cast<unsigned char>(nTemp / 256));
}
static unsigned char U16ToU8(unsigned short nIn)
{
return (static_cast<unsigned char>(nIn / 256));
}
private :
}; // End of class DCVImage
/*****************************************************************************
**************************** class DCVBinaryImage ****************************
*****************************************************************************/
/*
Binary image class. Basically add some useful functions for dealing with
binary images to DCVImage. No checking is done that the image is actually
a binary image!!!
*/
class DCVBinaryImage : public DCVImage
{
public :
enum EColor { eBlack, eWhite };
DCVBinaryImage() : DCVImage()
{
return;
}
DCVBinaryImage(int nWidth, int nHeight)
: DCVImage(nWidth, nHeight, CV_8UC1)
{
return;
}
DCVBinaryImage(const DCVBinaryImage& src) : DCVImage(src)
{
return;
}
~DCVBinaryImage() = default;
DCVBinaryImage& operator=(const DCVBinaryImage& rhs)
{
DCVImage::operator=(rhs);
return (*this);
}
// Is the specified pixel black?
bool IsBlack(int nRow, int nCol) const
{
return (*GetPixel(nRow, nCol) == eBlack);
}
// Is the specified pixel black?
bool IsBlack(unsigned char nPixel) const
{
return (nPixel == eBlack);
}
// Is the specified pixel white? Little latitude here
bool IsWhite(int nRow, int nCol) const
{
return (*GetPixel(nRow, nCol) != eBlack);
}
// Is the specified pixel white? Little latitude here
bool IsWhite(unsigned char nPixel) const
{
return (nPixel != eBlack);
}
// Count pixel runs
int CountBlackRight(int nRow, int nStartCol) const
{
int nCount = 0;
const unsigned char* pPixel = GetPixel(nRow, nStartCol);
while ((nStartCol < GetWidth()) && IsBlack(*pPixel))
{
nStartCol++;
pPixel++;
nCount++;
} // end while
return (nCount);
}
int CountBlackLeft(int nRow, int nStartCol) const
{
int nCount = 0;
const unsigned char* pPixel = GetPixel(nRow, nStartCol);
while ((nStartCol >= 0) && IsBlack(*pPixel))
{
nStartCol--;
pPixel--;
nCount++;
} // end while
return (nCount);
}
int CountWhiteRight(int nRow, int nStartCol) const
{
int nCount = 0;
const unsigned char* pPixel = GetPixel(nRow, nStartCol);
while ((nStartCol < GetWidth()) && IsWhite(*pPixel))
{
nStartCol++;
pPixel++;
nCount++;
} // end while
return (nCount);
}
int CountWhiteLeft(int nRow, int nStartCol) const
{
int nCount = 0;
const unsigned char* pPixel = GetPixel(nRow, nStartCol);
while ((nStartCol >= 0) && IsWhite(*pPixel))
{
nStartCol--;
pPixel--;
nCount++;
} // end while
return (nCount);
}
int CountBlackDown(int nStartRow, int nCol) const
{
int nCount = 0;
const unsigned char* pPixel = GetPixel(nStartRow, nCol);
while ((nStartRow < GetHeight()) && IsBlack(*pPixel))
{
nStartRow++;
pPixel += GetWidthStep();
nCount++;
} // end while
return (nCount);
}
int CountBlackUp(int nStartRow, int nCol) const
{
int nCount = 0;
const unsigned char* pPixel = GetPixel(nStartRow, nCol);
while ((nStartRow >= 0) && IsBlack(*pPixel))
{