This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
match_template.cpp
549 lines (469 loc) · 13.4 KB
/
match_template.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include <stdio.h>
#include <math.h>
#include "match_template.h"
int MatchTemplate(const IplImage* pTemplate,
const IplImage* pSource,
double dAngle,
double dScore,
ShiftValue& iResult)
{
// check the input params
if (pTemplate == NULL || pSource == NULL)
{
printf("MatchTemplate input params is NULL!\n");
return -1;
}
const CvSize iTemplateSize = cvGetSize(pTemplate);
const CvSize iSrcSize = cvGetSize(pSource);
if (iTemplateSize.height > iSrcSize.height
|| iTemplateSize.width > iSrcSize.width)
{
printf("MatchTemplate Template size is larger than Source Image!\n");
return -1;
}
if (dAngle < 0 || dAngle > 180)
{
printf("MatchTemplate input angle is out of range!\n");
return -1;
}
// check the input params ends
// pyrdown the source image
// pyrdown times
// get nPyrdownCount self-adaptively
int nPyrdownCount = GetPyrdownTime(iTemplateSize, iSrcSize, 20);
printf("PyrDown count is %d\n", nPyrdownCount);
// pyrdowned source image
IplImage* pPyrDownedSource = GetPyrDownImage(pSource, nPyrdownCount);
// pyrdowned template image
IplImage* pPyrDownedTemplate = GetPyrDownImage(pTemplate, nPyrdownCount);
// pyrdown the source image ends
// roughly match template
double dFirstStep = 0.5;
MatchWithAngle(pPyrDownedTemplate,
pPyrDownedSource,
dAngle,
dFirstStep,
iResult);
// roughly match template ends
// convert to init scale
// rotate source image with iResult.dA
IplImage* pRotatedImage = RotateImage(pSource, iResult.dAngle);
// set image ROI
iResult.dX *= pow(2, nPyrdownCount);
iResult.dY *= pow(2, nPyrdownCount);
//
ShiftValue iRoughlyResult = iResult;
printf("roughly matched result:\n");
printf("X: %f\nY: %f\nAngle: %f\n",
iRoughlyResult.dX,
iRoughlyResult.dY,
iRoughlyResult.dAngle);
// The top-left point of the ROI
CvPoint iTopLeftPoint;
double dExpand = 0.2;
iResult.dX += iSrcSize.width / 2;
iResult.dY += iSrcSize.height / 2;
SetImageROIWithCenterPoint(pRotatedImage,
iTemplateSize,
cvPoint(iResult.dX, iResult.dY),
dExpand,
iTopLeftPoint);
// convert to init scale ends
IplImage* pCanniedTemplate = cvCloneImage(pTemplate);
IplImage* pCanniedSource = cvCloneImage(pRotatedImage);
ExpandEdge(pTemplate, pCanniedTemplate, 2);
ExpandEdge(pRotatedImage, pCanniedSource, 2);
// precisly match template
MatchWithAngle(pCanniedTemplate,
pCanniedSource,
dFirstStep * 1.5,
dFirstStep * 0.1,
iResult);
ShiftValue iTmpResult = iResult;
printf("precisly matched result:\n");
printf("X: %f\nY: %f\nAngle: %f\n",
iTmpResult.dX,
iTmpResult.dY,
iTmpResult.dAngle);
// add roughly match result and precisly match result to final result
AddShiftParams(iRoughlyResult, iTmpResult, iResult);
printf("final matched result:\n");
printf("X: %f\nY: %f\nAngle: %f\n",
iResult.dX,
iResult.dY,
iResult.dAngle);
iResult.dX += iSrcSize.width / 2;
iResult.dY += iSrcSize.height / 2;
/*
iTmpResult.dAngle += iRoughlyResult.dAngle;
TranferCoordinate(iSrcSize.width / 2,
iSrcSize.height / 2,
iTmpResult,
iResult);
*/
// precisly match template ends
cvReleaseImage(&pRotatedImage);
cvReleaseImage(&pPyrDownedSource);
cvReleaseImage(&pPyrDownedTemplate);
cvReleaseImage(&pCanniedSource);
cvReleaseImage(&pCanniedTemplate);
return 1;
}
IplImage* GetPyrDownImage(const IplImage* pSource, int nTime)
{
// check input params
if (pSource == NULL)
{
printf("GetPyrDownImage Source Image is NULL:\n");
return NULL;
}
if (nTime < 1)
{
printf("GetPyrDownImage pyrdown time is error!\n");
return NULL;
}
// check input params ends
// source image size
CvSize iSourceSize = cvGetSize(pSource);
CvSize iPyrDownSize = cvSize(iSourceSize.width / 2, iSourceSize.height / 2);
// temp pyrdowned image
IplImage* pPyrDownedImage = cvCreateImage(iPyrDownSize, IPL_DEPTH_8U, 1);
cvPyrDown(pSource, pPyrDownedImage, CV_GAUSSIAN_5x5);
// result image
IplImage* pResult;
if (nTime == 1)
{
pResult = cvCloneImage(pPyrDownedImage);
}
else
{
pResult = GetPyrDownImage(pPyrDownedImage, --nTime);
}
cvReleaseImage(&pPyrDownedImage);
return pResult;
}
int MatchWithAngle(const IplImage* pTemplate,
const IplImage* pSource,
double dAngle,
double dStep,
ShiftValue& iShift)
{
// check input params
if (pTemplate == NULL || pSource == NULL)
{
printf("MatchWithAngle input image is NULL!\n");
return -1;
}
if (dAngle < 0 || dStep < 0 || dStep > dAngle)
{
printf("MatchWithAngle input angle and step is error!\n");
}
CvSize iTemplateSize = cvGetSize(pTemplate);
CvSize iSourceSize = cvGetSize(pSource);
if (iTemplateSize.width > iSourceSize.width ||
iTemplateSize.height > iSourceSize.height)
{
printf("MatchWithAngle input image size is error!\n");
return -1;
}
// check input params ends
IplImage* pRotateImage = cvCloneImage(pSource);
// ratete mat used in cvGetQuadrangleSubPix()
CvMat* pRotateMat = cvCreateMat(2, 3, CV_32FC1);
// cvMatchTemplate result mat
CvMat* pMatchedResult
= cvCreateMat(iSourceSize.height - iTemplateSize.height + 1,
iSourceSize.width - iTemplateSize.width + 1,
CV_32FC1);
CvPoint iMinPos, iMaxPos;
double dMinValue, dMaxValue;
double dCmpValue = -1;
// get the best matching result in this loop
for (double dA = -dAngle; dA <dAngle + dStep; dA += dStep)
{
// make sure the angle is not larger than dAngle
if (dA > dAngle)
{
dA = dAngle;
}
// get the rotate mat
double dCos = cos(dA * CV_PI / 180.0);
double dSin = sin(dA * CV_PI / 180.0);
cvmSet(pRotateMat, 0, 0, dCos);
cvmSet(pRotateMat, 0, 1, dSin);
cvmSet(pRotateMat, 0, 2, iSourceSize.width / 2);
cvmSet(pRotateMat, 1, 0, -dSin);
cvmSet(pRotateMat, 1, 1, dCos);
cvmSet(pRotateMat, 1, 2, iSourceSize.height / 2);
// ratete the source image whit angle dA
cvGetQuadrangleSubPix(pSource, pRotateImage, pRotateMat);
// get the best match
cvMatchTemplate(pRotateImage,
pTemplate,
pMatchedResult,
CV_TM_CCOEFF_NORMED);
cvMinMaxLoc(pMatchedResult,
&dMinValue,
&dMaxValue,
&iMinPos,
&iMaxPos,
NULL);
if (dCmpValue < dMaxValue)
{
dCmpValue = dMaxValue;
iShift.dX = iMaxPos.x;
iShift.dY = iMaxPos.y;
iShift.dAngle = dA;
}
/*
if (dCmpValue > dMinValue)
{
dCmpValue = dMinValue;
iShift.dX = iMinPos.x;
iShift.dY = iMinPos.y;
iShift.dAngle = dA;
}
*/
}
iShift.dX += iTemplateSize.width / 2 - iSourceSize.width / 2;
iShift.dY += iTemplateSize.height / 2 - iSourceSize.height / 2;
cvReleaseImage(&pRotateImage);
cvReleaseMat(&pRotateMat);
cvReleaseMat(&pRotateMat);
return 1;
}
IplImage* RotateImage(const IplImage* pSource, double dAngle)
{
if (pSource == NULL)
{
printf("RotateImage input image is NULL!\n");
return NULL;
}
if (dAngle == 0)
{
return cvCloneImage(pSource);
}
CvSize iSize = cvGetSize(pSource);
double dCos = cos(dAngle * CV_PI / 180.0);
double dSin = sin(dAngle * CV_PI / 180.0);
CvMat* pRotateMat = cvCreateMat(2, 3, CV_32FC1);
cvmSet(pRotateMat, 0, 0, dCos);
cvmSet(pRotateMat, 0, 1, dSin);
cvmSet(pRotateMat, 0, 2, iSize.width / 2);
cvmSet(pRotateMat, 1, 0, -dSin);
cvmSet(pRotateMat, 1, 1, dCos);
cvmSet(pRotateMat, 1, 2, iSize.height / 2);
IplImage* pRotatedImage = cvCloneImage(pSource);
cvGetQuadrangleSubPix(pSource, pRotatedImage, pRotateMat);
cvReleaseMat(&pRotateMat);
return pRotatedImage;
}
void TranferCoordinate(double dX, double dY,
const ShiftValue& iSrcShiftParam,
ShiftValue& iDstShiftParam)
{
double dOriginX = iSrcShiftParam.dX - dX;
double dOriginY = iSrcShiftParam.dY - dY;
// get dOriginX and dOriginY
CvPoint iTmpPoint;
GetRotatedPoint(cvPoint(dOriginX, dOriginY),
iSrcShiftParam.dAngle,
iTmpPoint);
dOriginX = iTmpPoint.x + dX;
dOriginY = iTmpPoint.y + dY;
double dShiftX = iSrcShiftParam.dX;
double dShiftY = iSrcShiftParam.dY;
// get dShiftX and dShiftY
GetRotatedPoint(cvPoint(dShiftX, dShiftY),
iSrcShiftParam.dAngle,
iTmpPoint);
// iDstShiftParam.dX = dOriginX + iTmpPoint.x;
// iDstShiftParam.dY = dOriginY + iTmpPoint.y;
iDstShiftParam.dX = dOriginX;
iDstShiftParam.dY = dOriginY;
iDstShiftParam.dAngle = iSrcShiftParam.dAngle;
}
int GetRotatedPoint(const CvPoint& iSrcPoint,
double dAngle,
CvPoint& iDstPoint)
{
if (dAngle > 180 || dAngle < -180)
{
printf("GetRotatedPoint input dAngle is out of range!\n");
return -1;
}
if (dAngle == 0)
{
iDstPoint.x = iSrcPoint.x;
iDstPoint.y = iSrcPoint.y;
return 1;
}
double dCos = cos(dAngle * CV_PI / 180.0);
double dSin = sin(dAngle * CV_PI / 180.0);
iDstPoint.x = dCos * iSrcPoint.x + dSin * iSrcPoint.y;
iDstPoint.y = -dSin * iSrcPoint.x + dCos * iSrcPoint.y;
return 1;
}
int SetImageROIWithCenterPoint(IplImage* pSourceImage,
CvSize iTemplateSize,
CvPoint iCenterPoint,
double dExpand,
CvPoint& iTopLeftPoint)
{
// check input param
if (pSourceImage == NULL)
{
printf("SetImageROIWithCenterPoint input source image is NULL!\n");
return -1;
}
if (dExpand < 0)
{
printf("SetImageROIWithCenterPoint expand is error!\n");
return -1;
}
CvSize iSize = cvGetSize(pSourceImage);
if (iCenterPoint.x < 0
|| iCenterPoint.y < 0
|| iCenterPoint.x > iSize.width
|| iCenterPoint.y > iSize.height)
{
printf("SetImageROIWithCenterPoint left-top Point is error!\n");
return -1;
}
// check input param ends
CvRect iRect;
iRect.x = iCenterPoint.x - iTemplateSize.width * (dExpand + 0.5);
iRect.y = iCenterPoint.y - iTemplateSize.height * (dExpand + 0.5);
iRect.height = iTemplateSize.height * (1 + 2 * dExpand);
iRect.width = iTemplateSize.width * (1 + 2 * dExpand);
if (iRect.x < 0)
{
iRect.x = 0;
}
if (iRect.y < 0)
{
iRect.y = 0;
}
if (iRect.x + iRect.width > iSize.width)
{
iRect.x = iSize.width - iRect.width;
}
if (iRect.y + iRect.height > iSize.height)
{
iRect.y = iSize.height - iRect.height;
}
cvSetImageROI(pSourceImage, iRect);
iTopLeftPoint.x = iRect.x;
iTopLeftPoint.y = iRect.y;
return 1;
}
void DrawResult(IplImage* pSourceImage,
CvSize iTemplateSize,
ShiftValue iShiftParam,
double dColor)
{
if (pSourceImage == NULL)
{
printf("DrawResult input SourceImage is NULL");
return;
}
double dX = iShiftParam.dX;
double dY = iShiftParam.dY;
double dWidth = iTemplateSize.width;
double dHeight = iTemplateSize.height;
CvPoint iTopLeft, iTopRight, iBottomLeft, iBottomRight;
iTopLeft = cvPoint(-dWidth / 2, -dHeight / 2);
iTopRight = cvPoint(dWidth / 2, -dHeight / 2);
iBottomLeft = cvPoint(-dWidth / 2, dHeight / 2);
iBottomRight = cvPoint(dWidth / 2, dHeight / 2);
CvPoint iTmpPoint;
GetRotatedPoint(iTopLeft, iShiftParam.dAngle, iTmpPoint);
iTopLeft = iTmpPoint;
GetRotatedPoint(iTopRight, iShiftParam.dAngle, iTmpPoint);
iTopRight = iTmpPoint;
GetRotatedPoint(iBottomLeft, iShiftParam.dAngle, iTmpPoint);
iBottomLeft = iTmpPoint;
GetRotatedPoint(iBottomRight, iShiftParam.dAngle, iTmpPoint);
iBottomRight = iTmpPoint;
iTopLeft.x += dX;
iTopRight.x += dX;
iBottomLeft.x += dX;
iBottomRight.x += dX;
iTopLeft.y += dY;
iTopRight.y += dY;
iBottomLeft.y += dY;
iBottomRight.y += dY;
cvCircle(pSourceImage, cvPoint(dX, dY), 2, cvScalarAll(dColor));
cvCircle(pSourceImage, iTopLeft, 2, cvScalarAll(dColor));
cvCircle(pSourceImage, iTopRight, 2, cvScalarAll(dColor));
cvCircle(pSourceImage, iBottomLeft, 2, cvScalarAll(dColor));
cvCircle(pSourceImage, iBottomRight, 2, cvScalarAll(dColor));
cvLine(pSourceImage, iTopLeft, iTopRight, cvScalarAll(dColor));
cvLine(pSourceImage, iTopRight, iBottomRight, cvScalarAll(dColor));
cvLine(pSourceImage, iBottomRight, iBottomLeft, cvScalarAll(dColor));
cvLine(pSourceImage, iBottomLeft, iTopLeft, cvScalarAll(dColor));
}
void AddShiftParams(ShiftValue& iA, ShiftValue& iB, ShiftValue& iC)
{
CvPoint iPointA, iPointB, iTmpPoint;
iPointA = cvPoint(iA.dX, iA.dY);
GetRotatedPoint(iPointA, iA.dAngle, iTmpPoint);
iC.dAngle = iA.dAngle;
iC.dX = iTmpPoint.x;
iC.dY = iTmpPoint.y;
iPointB = cvPoint(iB.dX, iB.dY);
GetRotatedPoint(iPointB, iB.dAngle + iA.dAngle, iTmpPoint);
iC.dAngle += iB.dAngle;
iC.dX += iTmpPoint.x;
iC.dY += iTmpPoint.y;
}
int ExpandEdge(const IplImage* pSourceImage, IplImage* pEdge, int nWidth)
{
if (pSourceImage == NULL || pEdge == NULL)
{
printf("ExpandEdge input image is NULL!\n");
return -1;
}
CvSize iSourceSize = cvGetSize(pSourceImage);
CvSize iEdgeSize = cvGetSize(pEdge);
if (iSourceSize.height != iEdgeSize.height
|| iSourceSize.width != iEdgeSize.width)
{
printf("ExpandEdge input image size error!\n");
return -1;
}
cvCanny(pSourceImage, pEdge, 50, 150, 3);
cvSmooth(pEdge, pEdge, CV_BLUR, nWidth, nWidth, 0, 0);
cvThreshold(pEdge, pEdge, 0, 200, CV_THRESH_BINARY);
return 1;
}
int GetPyrdownTime(CvSize iTemplateSize,
CvSize iSourceSize,
int nMinLength)
{
if (iTemplateSize.height > iSourceSize.height
|| iTemplateSize.width > iSourceSize.width)
{
printf("GetPyrdownTime input size error!\n");
return -1;
}
if (iTemplateSize.height == iSourceSize.height
|| iTemplateSize.width == iSourceSize.width
|| iTemplateSize.height <= nMinLength
|| iTemplateSize.width <= nMinLength)
{
return 0;
}
int nRet = 0;
while (iTemplateSize.height < iSourceSize.height
&& iTemplateSize.width < iSourceSize.width
&& iTemplateSize.height > nMinLength
&& iTemplateSize.width > nMinLength)
{
iTemplateSize.height /= 2;
iTemplateSize.width /= 2;
++nRet;
}
--nRet;
return nRet;
}