KSquare Utilities
MorphOpBinarize.cpp
Go to the documentation of this file.
1 /* MorphOpBinarize.cpp -- Morphological operator used to Binarize image.
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 
6 #include "FirstIncludes.h"
7 #include <stdlib.h>
8 #include <iostream>
9 #include <map>
10 #include <vector>
11 #include "MemoryDebug.h"
12 using namespace std;
13 
14 #include "KKBaseTypes.h"
15 #include "MorphOp.h"
16 #include "MorphOpBinarize.h"
17 #include "Raster.h"
18 using namespace KKB;
19 
20 
21 
23  kkuint16 _pixelValueMax
24  ):
25  MorphOp (),
26  pixelValueMin (_pixelValueMin),
27  pixelValueMax (_pixelValueMax)
28 {
29 }
30 
31 
32 
34 {
35 }
36 
37 
38 
40 {
41  return sizeof (*this);
42 }
43 
44 
45 
46 bool MorphOpBinarize::Fit (kkint32 row,
47  kkint32 col
48  ) const
49 {
50 
51  if ((row < 0) || (row >= srcHeight))
52  return false;
53 
54  if ((col < 0) || (col >= srcWidth))
55  return false;
56 
57  return ((srcGreen[row][col] >= pixelValueMin) && (srcGreen[row][col] <= pixelValueMin));
58 } /* Fit */
59 
60 
61 
62 RasterPtr MorphOpBinarize::PerformOperation (RasterConstPtr _image)
63 {
64  SetSrcRaster (_image);
65 
66  kkint32 r = 0;
67  kkint32 c = 0;
68 
69  kkint32 binarizedForegroundPixelCount = 0;
70 
71  RasterPtr binarizedRaster = new Raster (srcRaster->Height (), srcRaster->Width ());
72 
73  uchar* srcRow = NULL;
74  uchar** destGreen = binarizedRaster->Green ();
75  uchar* destRow = NULL;
76 
77  uchar backgroundPixelValue = srcRaster->BackgroundPixelValue ();
78 
79  for (r = 0; r < srcHeight; r++)
80  {
81  destRow = destGreen[r];
82  srcRow = srcGreen[r];
83 
84  for (c = 0; c < srcWidth; c++)
85  {
86  uchar srcCh = srcRow[c];
87  if ((srcCh >= pixelValueMin) && (srcCh <= pixelValueMax))
88  {
89  destRow[c] = srcCh;
90  ++binarizedForegroundPixelCount;
91  }
92  else
93  {
94  destRow[c] = backgroundPixelValue;
95  }
96  } /* for (c) */
97  } /* for (r) */
98 
99  binarizedRaster->ForegroundPixelCount (binarizedForegroundPixelCount);
100 
101  return binarizedRaster;
102 } /* PerformOperation */
void SetSrcRaster(RasterConstPtr _srcRaster)
Definition: MorphOp.cpp:149
__int32 kkint32
Definition: KKBaseTypes.h:88
uchar BackgroundPixelValue() const
Definition: Raster.h:336
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
A class that is used by to represent a single image in memory.
Definition: Raster.h:108
kkint32 srcHeight
Definition: MorphOp.h:123
Raster(kkint32 _height, kkint32 _width)
Constructs a blank image with given dimensions; all pixels will be initialized to 0...
Definition: Raster.cpp:318
kkint32 Height() const
Definition: Raster.h:319
KKTHread * KKTHreadPtr
kkint32 srcWidth
Definition: MorphOp.h:124
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
virtual RasterPtr PerformOperation(RasterConstPtr _image)
kkint32 Width() const
Definition: Raster.h:324
Base class for all Morphological operations.
Definition: MorphOp.h:44
MorphOpBinarize(kkuint16 _pixelValueMin, kkuint16 _pixelValueMax)
kkint32 MemoryConsumedEstimated()
uchar *const * srcGreen
Definition: MorphOp.h:119
RasterConstPtr srcRaster
Definition: MorphOp.h:112
uchar ** Green() const
Definition: Raster.h:327
void ForegroundPixelCount(kkint32 _foregroundPixelCount)
Definition: Raster.h:344