forked from zhangyiwei79/Opticks-SAR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureSegmentation.cpp
More file actions
259 lines (222 loc) · 7.6 KB
/
Copy pathTextureSegmentation.cpp
File metadata and controls
259 lines (222 loc) · 7.6 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
/*
* The information in this file is
* Copyright(c) 2007 Ball Aerospace & Technologies Corporation
* and is subject to the terms and conditions of the
* GNU Lesser General Public License Version 2.1
* The license text is available from
* http://www.gnu.org/licenses/lgpl.html
*/
#include "DataAccessor.h"
#include "DataAccessorImpl.h"
#include "DataRequest.h"
#include "DesktopServices.h"
#include "MessageLogResource.h"
#include "ObjectResource.h"
#include "PlugInArgList.h"
#include "PlugInManagerServices.h"
#include "PlugInRegistration.h"
#include "Progress.h"
#include "RasterDataDescriptor.h"
#include "RasterElement.h"
#include "RasterUtilities.h"
#include "SpatialDataView.h"
#include "SpatialDataWindow.h"
#include "switchOnEncoding.h"
#include "TextureSegmentation.h"
#include "ImageSegmentation.h"
#include <limits>
REGISTER_PLUGIN_BASIC(OpticksTutorial, TextureSegmentation);
namespace
{
#define EDGE_THRESHOLD_VALUE 0.8
#define MAXIMUM_LOOPS 30
#define LAMDA_VALUE 40
#define MU_VALUE 1000
#define NUMBER_OF_BINS 256
#define PARZEN_WINDOW_SIZE 2
#define PATCH_SIZE 7
void MakeSegmentation(DataAccessor pSrcAcc, float *pBuffer, float *pfu, unsigned int rows, unsigned int cols, EncodingType type)
{
double pixelVal;
double maxVal = 0;
float pfVecParameters[8];
unsigned int nCount = 0;
pfVecParameters[0] = rows;
pfVecParameters[1] = cols;
pfVecParameters[2] = MAXIMUM_LOOPS; /* Maximum number of loops */
pfVecParameters[3] = LAMDA_VALUE; //Lamda value
pfVecParameters[4] = MU_VALUE; // Mu value
pfVecParameters[5] = NUMBER_OF_BINS; /* Number of bins for histograms */
pfVecParameters[6] = PARZEN_WINDOW_SIZE; /* standard deviation in Parzen estimation */
pfVecParameters[7] = PATCH_SIZE; /* patch size */
for (unsigned int j=0; j<cols; j++)
{
for (unsigned int i=0; i<rows; i++)
{
pSrcAcc->toPixel(i, j);
VERIFYNRV(pSrcAcc.isValid());
pixelVal = Service<ModelServices>()->getDataValue(type, pSrcAcc->getColumn(), COMPLEX_MAGNITUDE, 0);
*(pBuffer + nCount) = pixelVal;
nCount++;
if (maxVal < pixelVal)
maxVal = pixelVal;
}
}
for (unsigned int i=0; i<nCount; i++)
{
*(pBuffer + i) = 1 + (*(pBuffer + i)/maxVal)*NUMBER_OF_BINS;
}
ImageSegmentation(pBuffer, pfu, pfVecParameters);
}
template<typename T>
void restoreSegmentationValue(T* pData, float *pSrc)
{
unsigned char pixelVal = 0;
float dVal = *pSrc;
if (pSrc != NULL)
{
if (*pSrc > EDGE_THRESHOLD_VALUE)
{
pixelVal = 1;
}
}
*pData = static_cast<T>(pixelVal);
}
};
TextureSegmentation::TextureSegmentation()
{
setDescriptorId("{21860EDB-3761-4e7f-B4DF-169369576749}");
setName("SAR Image Segmentation");
setDescription("Segmentation for SAR");
setCreator("Yiwei Zhang");
setVersion("Sample");
setCopyright("Copyright (C) 2008, Ball Aerospace & Technologies Corp.");
setProductionStatus(false);
setType("Sample");
setSubtype("Segmentation");
setMenuLocation("[SAR]/Segmentation");
setAbortSupported(true);
pBuffer = NULL;
}
TextureSegmentation::~TextureSegmentation()
{
if (pBuffer != NULL)
free(pBuffer);
}
bool TextureSegmentation::getInputSpecification(PlugInArgList*& pInArgList)
{
VERIFY(pInArgList = Service<PlugInManagerServices>()->getPlugInArgList());
pInArgList->addArg<Progress>(Executable::ProgressArg(), NULL, "Progress reporter");
pInArgList->addArg<RasterElement>(Executable::DataElementArg(), "Perform segmentation on this data element");
return true;
}
bool TextureSegmentation::getOutputSpecification(PlugInArgList*& pOutArgList)
{
VERIFY(pOutArgList = Service<PlugInManagerServices>()->getPlugInArgList());
pOutArgList->addArg<RasterElement>("Result", NULL);
return true;
}
bool TextureSegmentation::execute(PlugInArgList* pInArgList, PlugInArgList* pOutArgList)
{
StepResource pStep("SAR image segmentation", "app", "CC430C1A-9D8C-4bb5-9254-FCF7EECAFA3C");
if (pInArgList == NULL || pOutArgList == NULL)
{
return false;
}
Progress* pProgress = pInArgList->getPlugInArgValue<Progress>(Executable::ProgressArg());
RasterElement* pCube = pInArgList->getPlugInArgValue<RasterElement>(Executable::DataElementArg());
if (pCube == NULL)
{
std::string msg = "A raster cube must be specified.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
RasterDataDescriptor* pDesc = static_cast<RasterDataDescriptor*>(pCube->getDataDescriptor());
VERIFY(pDesc != NULL);
EncodingType ResultType = INT1UBYTE;
FactoryResource<DataRequest> pRequest;
pRequest->setInterleaveFormat(BSQ);
DataAccessor pSrcAcc = pCube->getDataAccessor(pRequest.release());
ModelResource<RasterElement> pResultCube(RasterUtilities::createRasterElement(pCube->getName() +
"_Segmentation_Result", pDesc->getRowCount(), pDesc->getColumnCount(), ResultType));
if (pResultCube.get() == NULL)
{
std::string msg = "A raster cube could not be created.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
FactoryResource<DataRequest> pResultRequest;
pResultRequest->setWritable(true);
DataAccessor pDestAcc = pResultCube->getDataAccessor(pResultRequest.release());
if (isAborted())
{
std::string msg = getName() + " has been aborted.";
pStep->finalize(Message::Abort, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ABORT);
}
return false;
}
if (NULL != pBuffer)
{
free(pBuffer);
}
pBuffer = (float *)malloc(sizeof(float)*pDesc->getRowCount()*pDesc->getColumnCount());
MakeSegmentation(pSrcAcc, pBuffer, pBuffer, pDesc->getRowCount(), pDesc->getColumnCount(), pDesc->getDataType());
//Output the value
unsigned int nCount = 0;
for (unsigned int j = 0; j < pDesc->getColumnCount(); j++)
{
for (unsigned int i = 0; i < pDesc->getRowCount(); i++)
{
if (!pDestAcc.isValid())
{
std::string msg = "Unable to access the cube data.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
pDestAcc->toPixel(i, j);
switchOnEncoding(ResultType, restoreSegmentationValue, pDestAcc->getColumn(), (pBuffer+nCount));
nCount++;
}
}
if (!isBatch())
{
Service<DesktopServices> pDesktop;
SpatialDataWindow* pWindow = static_cast<SpatialDataWindow*>(pDesktop->createWindow(pResultCube->getName(),
SPATIAL_DATA_WINDOW));
SpatialDataView* pView = (pWindow == NULL) ? NULL : pWindow->getSpatialDataView();
if (pView == NULL)
{
std::string msg = "Unable to create view.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
pView->setPrimaryRasterElement(pResultCube.get());
pView->createLayer(RASTER, pResultCube.get());
}
if (pProgress != NULL)
{
pProgress->updateProgress("Image segmentation is compete.", 100, NORMAL);
}
pOutArgList->setPlugInArgValue("Image segmentation result", pResultCube.release());
pStep->finalize();
return true;
}