KSquare Utilities
MorphOpStruct.cpp
Go to the documentation of this file.
1 /* MorphOpStruct.cpp -- Morphological operators use to perform erosion.
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 
12 #include "MemoryDebug.h"
13 using namespace std;
14 
15 #include "MorphOpStruct.h"
16 #include "Raster.h"
17 using namespace KKB;
18 
19 
20 
22  kkuint16 _structureSize
23  ):
24  MorphOp (),
27  structure (_structure),
28  structureSize (_structureSize)
29 {
30 }
31 
32 
33 
35 {
36 }
37 
38 
39 
41 {
42  return sizeof (*this);
43 }
44 
45 
46 
47 
48 
50  kkint32 col
51  ) const
52 {
53  kkint32 r, c;
54  kkint32 rStart = row - structureSize;
55  kkint32 rEnd = row + structureSize;
56  kkint32 cStart = col - structureSize;
57  kkint32 cEnd = col + structureSize;
58 
59  if (rStart < 0) rStart = 0;
60  if (rEnd >= srcHeight) rEnd = srcHeight - 1;
61  if (cStart < 0) cStart = 0;
62  if (cEnd >= srcWidth) cEnd = srcWidth - 1;
63 
65  {
66  for (r = rStart; r <= rEnd; r++)
67  {
68  uchar const* rowPtr = srcGreen[r];
69 
70  for (c = cStart; c <= cEnd; c++)
71  {
72  if (BackgroundPixel (rowPtr[c]))
73  return false;
74  }
75  }
76  }
77 
78  else
79  {
80  for (r = rStart; r <= rEnd; r++)
81  {
82  if (BackgroundPixel (srcGreen[r][col]))
83  return false;
84  }
85 
86  for (c = cStart; c <= cEnd; c++)
87  {
88  if (BackgroundPixel (srcGreen[row][c]))
89  return false;
90  }
91  }
92 
93  return true;
94 } /* Fit */
95 
96 
97 
98 
100  kkint32 col
101  ) const
102 {
103  kkint32 r, c;
104  kkint32 rStart = row - structureSize;
105  kkint32 rEnd = row + structureSize;
106  kkint32 cStart = col - structureSize;
107  kkint32 cEnd = col + structureSize;
108 
109  if (rStart < 0) rStart = 0;
110  if (rEnd >= srcHeight) rEnd = srcHeight - 1;
111  if (cStart < 0) cStart = 0;
112  if (cEnd >= srcWidth) cEnd = srcWidth - 1;
113 
114  int backgroundCount = 0;
115 
117  {
118  for (r = rStart; r <= rEnd; r++)
119  {
120  uchar const* rowPtr = srcGreen[r];
121 
122  for (c = cStart; c <= cEnd; c++)
123  {
124  if (BackgroundPixel (rowPtr[c]))
125  ++backgroundCount;
126  }
127  }
128  }
129 
130  else
131  {
132  for (r = rStart; r <= rEnd; r++)
133  {
134  if (BackgroundPixel (srcGreen[r][col]))
135  ++backgroundCount;
136  }
137 
138  for (c = cStart; c <= cEnd; c++)
139  {
140  if (BackgroundPixel (srcGreen[row][c]))
141  ++backgroundCount;
142  }
143  }
144 
145  return backgroundCount < backgroundCountTH;
146 } /* FitBackgroundCount */
147 
148 
149 
150 
151 
153  kkint32 col
154  ) const
155 {
156  kkint32 r, c;
157  kkint32 rStart = row - structureSize;
158  kkint32 rEnd = row + structureSize;
159  kkint32 cStart = col - structureSize;
160  kkint32 cEnd = col + structureSize;
161 
162  if (rStart < 0) rStart = 0;
163  if (rEnd >= srcHeight) rEnd = srcHeight - 1;
164  if (cStart < 0) cStart = 0;
165  if (cEnd >= srcWidth) cEnd = srcWidth - 1;
166 
167  kkint32 pixelValueTotal = 0;
168  kkint32 neighborCount = 0;
169 
171  {
172  for (r = rStart; r <= rEnd; r++)
173  {
174  uchar const* rowPtr = srcGreen[r];
175 
176  for (c = cStart; c <= cEnd; c++)
177  {
178  if (ForegroundPixel (rowPtr[c]))
179  {
180  ++neighborCount;
181  pixelValueTotal += rowPtr[c];
182  }
183  }
184  }
185  }
186 
187  else
188  {
189  for (r = rStart; r <= rEnd; r++)
190  {
191  if (ForegroundPixel (srcGreen[r][col]))
192  {
193  ++neighborCount;
194  pixelValueTotal += srcGreen[r][col];
195  }
196  }
197 
198  for (c = cStart; c <= cEnd; c++)
199  {
200  if (ForegroundPixel (srcGreen[r][c]))
201  {
202  ++neighborCount;
203  pixelValueTotal += srcGreen[r][col];
204  }
205  }
206  }
207 
208  if (neighborCount < foregroundCountTH)
209  return (uchar)0;
210  else
211  return (uchar)(0.5f + (float)pixelValueTotal / (float)neighborCount);
212 } /* HitForegroundCount */
__int32 kkint32
Definition: KKBaseTypes.h:88
kkuint16 structureSize
Definition: MorphOpStruct.h:59
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
bool BackgroundPixel(uchar pixel) const
Definition: MorphOp.cpp:171
kkint32 backgroundCountTH
Definition: MorphOpStruct.h:57
kkint32 MemoryConsumedEstimated()
kkint32 srcHeight
Definition: MorphOp.h:123
KKTHread * KKTHreadPtr
MorphOpStruct(StructureType _structure, kkuint16 _structureSize)
virtual ~MorphOpStruct()
kkint32 srcWidth
Definition: MorphOp.h:124
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
StructureType structure
Definition: MorphOpStruct.h:56
bool Fit(kkint32 row, kkint32 col) const
bool FitBackgroundCount(kkint32 row, kkint32 col) const
kkint32 foregroundCountTH
Definition: MorphOpStruct.h:58
Base class for all Morphological operations.
Definition: MorphOp.h:44
uchar *const * srcGreen
Definition: MorphOp.h:119
bool ForegroundPixel(uchar pixel) const
Definition: MorphOp.cpp:195
uchar HitForegroundCount(kkint32 row, kkint32 col) const