-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpotMtThread.cpp
358 lines (319 loc) · 8.38 KB
/
SpotMtThread.cpp
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
#include "SpotMtThread.h"
// random color
#include "Graying.h"
// match class
#include "CharactVect.h"
#include "GCI.h"
/**< event id */
const long SpotMtThread::ID = wxNewId();
/**< construct */
SpotMtThread::SpotMtThread(wxEvtHandler* host, LS_DTRESULT& dt, wxArrayPtrVoid& imgs, wxArrayPtrVoid& disps, ST_MTRESULT& mt)
: wxThread(wxTHREAD_DETACHED) // 分离线程,自动释放自己的资源
, m_pHost(host)
, m_lsDtResult(dt)
, m_aryImgs(imgs)
, m_aryImgsDisp(disps)
, m_stMtResult(mt)
{
}
SpotMtThread::~SpotMtThread()
{
}
/**< thread's entry */
void* SpotMtThread::Entry()
{
int value = 0;
bool bRet = SpotMatch();
value = bRet?1:0;
// notition host, pEvt's memory auto release
wxThreadEvent* pEvt = new wxThreadEvent(wxEVT_THREAD, SpotMtThread::ID);
pEvt->SetInt(value);
m_pHost->QueueEvent(pEvt);
return nullptr;
}
/**< detroy the mt param */
bool SpotMtThread::DestroyMTParam(ST_MTPARAM* pstParm)
{
if (pstParm == nullptr)
return true;
// origin image
if (pstParm->pImg != nullptr)
{
delete [] pstParm->pImg;
pstParm->pImg = nullptr;
}
// detect result maybe come from result list
pstParm->pData = nullptr;
// attrib vector
if (pstParm->pvtAttr != nullptr)
{
for (auto it = pstParm->pvtAttr->begin(); it != pstParm->pvtAttr->end(); ++it)
{
ST_SPOT_ATTR& attr = *it;
// detect result maybe come from result list
attr.pNode = nullptr;
// release charact
if (attr.pCrt != nullptr)
{
delete attr.pCrt;
attr.pCrt = nullptr;
}
}
if (!pstParm->pvtAttr->empty())
pstParm->pvtAttr->clear();
delete pstParm->pvtAttr;
pstParm->pvtAttr = nullptr;
}
return true;
}
/**< destroy the match result */
bool SpotMtThread::DestroyMtResult(ST_MTRESULT* pRst)
{
if (pRst == nullptr)
return true;
// destroy union image
if (pRst->pImgUnion != nullptr)
{
wxImage *pImg = (wxImage *)pRst->pImgUnion;
if (pImg->IsOk())
pImg->Destroy();
delete pImg;
pRst->pImgUnion = nullptr;
}
// destroy match-pair
return CharactVect::DestroyResult(pRst);
}
bool SpotMtThread::SpotMatch()
{
bool bRet = false;
ST_MTPARAM stParamA;
ST_MTPARAM stParamB;
// release old match result
SpotMtThread::DestroyMtResult(&m_stMtResult);
// generate the parameter
InitParam(stParamA, 0);
InitParam(stParamB, 1);
// match
// CharactVect cvt;
// if (cvt.CVMain(std::make_pair(stParamA, stParamB), &m_stMtResult))
// {
// //DispMtResult_crt(stParamA, stParamB);
// DispMtResult_icp(stParamA, stParamB);
// DispMtResult_vec(stParamA, stParamB);
//
// bRet = true;
// }
GCI gci;
if (gci.Main(std::make_pair(stParamA, stParamB), &m_stMtResult))
{
bRet = true;
}
// release param
SpotMtThread::DestroyMTParam(&stParamA);
SpotMtThread::DestroyMTParam(&stParamB);
return bRet;
}
/**< fill a match param */
bool SpotMtThread::InitParam(ST_MTPARAM& stParam, int id)
{
// get the specify image
wxImage* pImg = static_cast<wxImage*>(m_aryImgs.Item(id));
unsigned char* pTmp = pImg->GetData();
// get the corresponding detection result
auto it = m_lsDtResult.begin();
for (int i = 0; i < id; ++i)
++it;
ST_DTRESULT& rst = *it;
// image's infomation
stParam.iW = pImg->GetWidth();
stParam.iH = pImg->GetHeight();
stParam.iWb = stParam.iW*3;
stParam.iN = stParam.iW*stParam.iH;
// image raw data
stParam.pImg = new ST_RGB[stParam.iN];
memcpy(stParam.pImg, pTmp, stParam.iN*3);
// detection marks
stParam.pData = rst.pData;
// spots' infomation
size_t nC = rst.pLs->size();
stParam.pvtAttr = new VT_ATTR(nC);
nC = 0u;
for(auto ir = rst.pLs->begin(); ir != rst.pLs->end(); ++ir)
{
ST_SPOT_NODE& stNode = *ir;
ST_SPOT_ATTR& attr = stParam.pvtAttr->at(nC++);
attr.pNode = &stNode;
attr.pCrt = new ST_SPOT_CHARACT;
}
wxASSERT_MSG(nC == rst.pLs->size(), _T("Fill match param's pNode failed."));
return true;
}
/**< display the match result */
bool SpotMtThread::DispMtResult_crt(ST_MTPARAM& paramA, ST_MTPARAM& paramB)
{
int block = 3; // block's width
wxImage* pImg = nullptr;
// function: draw spots character of one image
auto draw_charact = [block] (ST_MTPARAM stParam, wxImage* pImg)
{
PST_RGB pDes = (PST_RGB)pImg->GetData();
int iW = stParam.iW;
int iH = stParam.iH;
// draw spot's character
for (auto it = stParam.pvtAttr->begin(); it != stParam.pvtAttr->end(); ++it)
{
if (it->bInvalid)
continue;
ST_SPOT_NODE& spot = *(it->pNode);
ST_SPOT_CHARACT& crt = *(it->pCrt);
double dValue = crt.deep; // character
// gel spot coordinate
int x = spot.x;
int y = spot.y;
// charact's value color-map
ST_RGB clr;
Graying::ColorMap(dValue, &clr);
for (int j = -block; j <= block; ++j)
{
if (y+j < 0 || y+j >= iH)
continue;
PST_RGB pLine = pDes + (y+j)*iW;
for (int i = -block; i <= block; ++i)
{
if (x+i < 0 || x+i >= iW)
continue;
PST_RGB pPix = pLine + (x+i);
*pPix = clr;
}
}
}
};
pImg = static_cast<wxImage*>(m_aryImgsDisp.Item(0));
draw_charact(paramA, pImg);
pImg = static_cast<wxImage*>(m_aryImgsDisp.Item(1));
draw_charact(paramB, pImg);
return true;
}
bool SpotMtThread::DispMtResult_icp(ST_MTPARAM& paramA, ST_MTPARAM& paramB, int bgi)
{
int block = 3; // spot block's width
ST_RGB clr;
wxImage* pImg = nullptr;
PST_RGB pDes_A = nullptr, pDes_B = nullptr;
int iW_A = 0, iW_B = 0, iH_A = 0, iH_B = 0, iN_A = 0, iN_B = 0;
// image A
pImg = static_cast<wxImage*>(m_aryImgsDisp.Item(0));
pDes_A = (PST_RGB)pImg->GetData();
iW_A = paramA.iW;
iH_A = paramA.iH;
iN_A = paramA.iN;
// image B
pImg = static_cast<wxImage*>(m_aryImgsDisp.Item(1));
pDes_B = (PST_RGB)pImg->GetData();
iW_B = paramB.iW;
iH_B = paramB.iH;
iN_B = paramB.iN;
// copy the back-image
if ((bgi & 0x01) != 0)
memcpy(pDes_A, paramA.pImg, iN_A*3);
if ((bgi & 0x02) != 0)
memcpy(pDes_B, paramB.pImg, iN_B*3);
// function: draw a spot block
auto draw_spot = [&clr, block] (ST_SPOT_ATTR& spot, PST_RGB pDes, int iW, int iH)
{
// gel spot coordinate
int x = spot.pNode->x;
int y = spot.pNode->y;
// character
for (int j = -block; j <= block; ++j)
{
if (y+j < 0 || y+j >= iH)
continue;
PST_RGB pLine = pDes + (y+j)*iW;
for (int i = -block; i <= block; ++i)
{
if (x+i < 0 || x+i >= iW)
continue;
PST_RGB pPix = pLine + (x+i);
*pPix = clr;
}
}
};
// draw match pair
VT_SPAIR* pvtPairs = m_stMtResult.pvtSpair;
for (auto it = pvtPairs->begin(); it != pvtPairs->end(); ++it)
{
ST_SPOT_ATTR& spotA = paramA.pvtAttr->at(it->iOdA);
ST_SPOT_ATTR& spotB = paramB.pvtAttr->at(it->iOdB);
// random color
Graying::RandColor(&clr);
// draw the pair
draw_spot(spotA, pDes_A, iW_A, iH_A);
draw_spot(spotB, pDes_B, iW_B, iH_B);
}
return true;
}
bool SpotMtThread::DispMtResult_vec(ST_MTPARAM& paramA, ST_MTPARAM& paramB)
{
int iTemp = 0;
int iW = paramA.iW + paramB.iW;
int iWb = iW*3;
int iH = (paramA.iH > paramB.iH)?paramA.iH:paramB.iH;
wxImage *pImgUnion = nullptr;
unsigned char *pImg = nullptr;
unsigned char *pImgA = nullptr;
unsigned char *pImgB = nullptr;
ST_RGB clr;
wxRect rcl;
// ensure image is suitable
pImgUnion = (wxImage *)m_stMtResult.pImgUnion;
if (pImgUnion == nullptr)
pImgUnion = new wxImage(iW, iH);
else if (!pImgUnion->IsOk())
pImgUnion->Create(iW, iH);
else if (pImgUnion->GetWidth() != iW || pImgUnion->GetHeight() != iH)
{
pImgUnion->Destroy();
pImgUnion->Create(iW, iH);
}
// union image
pImg = pImgUnion->GetData();
pImgA = (unsigned char *)paramA.pImg;
pImgB = (unsigned char *)paramB.pImg;
iTemp = paramA.iWb;
for (int y = 0; y < paramA.iH; ++y)
{
memcpy(pImg, pImgA, (size_t)iTemp);
pImg += iWb;
pImgA += iTemp;
}
pImg = pImgUnion->GetData() + iTemp; // offset image A
iTemp = paramB.iWb;
for (int y = 0; y < paramB.iH; ++y)
{
memcpy(pImg, pImgB, (size_t)iTemp);
pImg += iWb;
pImgB += iTemp;
}
// maker one pair
for (auto it = m_stMtResult.pvtSpair->begin(); it != m_stMtResult.pvtSpair->end(); ++it)
{
ST_SPOT_ATTR& spotA = paramA.pvtAttr->at(it->iOdA);
ST_SPOT_ATTR& spotB = paramB.pvtAttr->at(it->iOdB);
// gel spot coordinate
rcl.x = spotA.pNode->x;
rcl.y = spotA.pNode->y;
rcl.width = spotB.pNode->x + paramA.iW - rcl.x;
rcl.height = spotB.pNode->y - rcl.y;
if (rcl.width > 0) rcl.width += 1;
else if (rcl.width < 0) rcl.width -= 1;
if (rcl.height > 0) rcl.height += 1;
else if (rcl.height < 0) rcl.height -= 1;
// random color
Graying::RandColor(&clr);
// draw pair line
img_draw_line(*pImgUnion, rcl, clr);
}
m_stMtResult.pImgUnion = pImgUnion;
return true;
}