KSquare Utilities
MorphOpSobel.cpp
Go to the documentation of this file.
1 /* MorphOpSobel.cpp -- Performs MorphOpSobel Edge Detection on a Raster image.
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #include "FirstIncludes.h"
6 
7 #include <math.h>
8 #include <vector>
9 #include <iostream>
10 
11 #include "MemoryDebug.h"
12 #include "KKBaseTypes.h"
13 
14 using namespace std;
15 using namespace KKB;
16 
17 #include "MorphOpSobel.h"
18 
19 
20 #include "Raster.h"
21 
22 
24  magnitudeSqrTable (NULL),
25  maxMagnitude (0)
26 {
27 }
28 
30 {
31  DeleteMagnitudeSqrTable ();
32 }
33 
34 void MorphOpSobel::DeleteMagnitudeSqrTable ()
35 {
36  if (magnitudeSqrTable)
37  {
38  for (kkuint32 r = 0; r < magnitudeSqrTableHeight; ++r)
39  delete magnitudeSqrTable[r];
40  delete magnitudeSqrTable;
41  magnitudeSqrTable = NULL;
42  }
43  magnitudeSqrTableHeight = 0;
44  magnitudeSqrTableWidth = 0;
45 }
46 
47 void MorphOpSobel::AllocateMagnitudeSqrTable ()
48 {
49  DeleteMagnitudeSqrTable ();
50 
51  kkuint32 r = 0;
52 
53  magnitudeSqrTableHeight = srcHeight;
54  magnitudeSqrTableWidth = srcWidth;
55 
56  magnitudeSqrTable = new kkint32*[magnitudeSqrTableHeight];
57 
58  for (r = 0; r < magnitudeSqrTableHeight; ++r)
59  magnitudeSqrTable[r] = new kkint32[magnitudeSqrTableWidth];
60 }
61 
62 RasterPtr MorphOpSobel::PerformOperation (Raster const* _image)
63 {
64  if (!_image)
65  return NULL;
66 
67  bool weOwnRaster = false;
68  if (_image->Color ())
69  {
70  _image = _image->CreateGrayScaleKLT ();
71  weOwnRaster = true;
72  }
73 
74  SetSrcRaster (_image);
75 
76  BuildMagnitudeSqrTable ();
77 
78  if (weOwnRaster)
79  delete _image;
80  _image = NULL;
81 
82  return BuildMagnitudeImage();
83 } /* PerformOperation */
84 
85 void MorphOpSobel::BuildMagnitudeSqrTable ()
86 {
87  if ((srcHeight != magnitudeSqrTableHeight) || (srcWidth != magnitudeSqrTableWidth))
88  AllocateMagnitudeSqrTable ();
89 
90  uchar* lastRow = NULL;
91  uchar* thisRow = NULL;
92  uchar* nextRow = NULL;
93 
94  kkint32 magnitude;
95 
96  kkint32 r, c;
97  kkint32 v, h;
98 
99  maxMagnitude = 0;
100 
101  for (r = 1; r < (srcHeight - 1); ++r)
102  {
103  lastRow = srcGreen[r - 1];
104  thisRow = srcGreen[r];
105  nextRow = srcGreen[r + 1];
106 
107  for (c = 1; c < (srcWidth - 1); c++)
108  {
109  v = (-1 * lastRow[c - 1]) + (-2 * lastRow[c]) + (-1 * lastRow[c + 1]) +
110  ( 1 * nextRow[c - 1]) + ( 2 * nextRow[c]) + ( 1 * nextRow[c + 1]);
111 
112  h = (-1 * lastRow[c - 1]) + (1 * lastRow[c + 1]) +
113  (-2 * thisRow[c - 1]) + (2 * thisRow[c + 1]) +
114  (-1 * nextRow[c - 1]) + (1 * nextRow[c + 1]);
115 
116  magnitude = (v * v + h * h);
117  if (magnitude > maxMagnitude)
118  maxMagnitude = magnitude;
119 
120  magnitudeSqrTable[r][c] = magnitude;
121  }
122  }
123 } /* BuildMagnitudeTable */
124 
125 
126 RasterPtr MorphOpSobel::BuildMagnitudeImage () const
127 {
128  kkint32 r = 0, c = 0;
129 
130  RasterPtr magImage = new Raster (srcHeight, srcWidth, false);
131 
132  for (r = 0; r < srcHeight; ++r)
133  {
134  magImage->SetPixelValue (r, 0, 0);
135  magImage->SetPixelValue (r, srcWidth - 1, 0);
136  }
137 
138  for (c = 0; c < srcWidth; ++c)
139  {
140  magImage->SetPixelValue (0, c, 0);
141  magImage->SetPixelValue (srcHeight - 1, c, 0);
142  }
143 
144  float adjMag = 0.0f;
145 
146  float maxMagnitudeFloat = sqrt ((float)maxMagnitude);
147 
148  for (r = 1; r < (srcHeight - 1); ++r)
149  {
150  for (c = 1; c < (srcWidth - 1); ++c)
151  {
152  if (maxMagnitude > 0.0f)
153  adjMag = 255.0f * sqrt ((float)magnitudeSqrTable[r][c]) / maxMagnitudeFloat;
154  else
155  adjMag = 0.0f;
156 
157  float pixelWorkVal = adjMag + (float)0.5;
158  if (pixelWorkVal > 255.0f)
159  pixelWorkVal = 255.0f;
160 
161  uchar pixelVal = (uchar)pixelWorkVal;
162  magImage->SetPixelValue (r, c, pixelVal);
163  }
164  }
165 
166  return magImage;
167 } /* BuildMagnitudeImage */
void SetSrcRaster(RasterConstPtr _srcRaster)
Definition: MorphOp.cpp:149
__int32 kkint32
Definition: KKBaseTypes.h:88
RasterPtr CreateGrayScaleKLT() const
Creates a image using a KLT Transform with the goal of weighting in favor the color channels with gre...
Definition: Raster.cpp:9503
void SetPixelValue(kkint32 row, kkint32 col, uchar pixVal)
Definition: Raster.cpp:1390
A class that is used by to represent a single image in memory.
Definition: Raster.h:108
bool Color() const
Definition: Raster.h:310
kkint32 srcHeight
Definition: MorphOp.h:123
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
KKTHread * KKTHreadPtr
kkint32 srcWidth
Definition: MorphOp.h:124
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
Raster(kkint32 _height, kkint32 _width, bool _color)
Constructs a blank image with given dimensions.
Definition: Raster.cpp:356
virtual RasterPtr PerformOperation(Raster const *_image)
uchar *const * srcGreen
Definition: MorphOp.h:119