eyemEdge1d.cpp
20.5 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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include "eyemEdge1d.h"
static void findPeak(int iSize, float projectMap[], float fAmpThreshold, std::vector<int>& peaks)
{
std::vector<int> sign;
for (int i = 1; i < iSize; i++) {
auto diff = projectMap[i] - projectMap[i - 1];
if (diff < 0.0f) {
sign.push_back(-1);
}
else if (diff > 0.0f) {
sign.push_back(1);
}
else {
sign.push_back(0);
}
}
for (int j = 1; j < sign.size(); j++) {
int diff = sign[j] - sign[j - 1];
if (diff != 0 && abs(projectMap[j]) > fAmpThreshold) {
peaks.push_back(j);
}
}
}
static cv::Mat projectMap(const cv::Mat map, int threshold)
{
double height = .0, width = map.cols;
cv::Mat nmMap;
cv::normalize(map, nmMap, 0, 255, cv::NORM_MINMAX);
cv::minMaxLoc(nmMap, NULL, &height, NULL, NULL);
cv::Mat image = cv::Mat::zeros((int)height, (int)width, CV_8UC3);
image.setTo(cv::Scalar(100, 0, 0));
std::vector<cv::Point> rejectPoint;
for (int i = 0; i < nmMap.cols; i++)
{
rejectPoint.push_back(cv::Point(i, (int)(height - nmMap.at<float>(cv::Point(i, 0)))));
}
cv::polylines(image, rejectPoint, false, cv::Scalar(0, 255, 0), 1, 8, 0);
//cv::line(image, cv::Point(0, (int)MAX(height - threshold, 0)), cv::Point((int)width, (int)MAX(height - threshold, 0)), cv::Scalar(0, 255, 255), 1, 8);
return image;
}
void getSuperResolution(const cv::Mat& src, cv::Mat& dst, int size)
{
dst = cv::Mat::zeros(src.size() * size, src.type());
for (int i = 0; i < src.cols; i++)
{
for (int j = 0; j < src.rows; j++)
{
cv::Mat m(cv::Size(size, size), src.type(), cv::Scalar::all(src.at<uchar>(j, i)));
m.copyTo(dst(cv::Rect(i * size, j * size, size, size)));
}
}
}
int eyemEdge1dGenMeasureRect(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, const char* ccSubType, int iTransition, double dSigma, double dAmpThresh, IntPtr* hObject)
{
cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage);
if (image.empty()) {
return FUNC_IMAGE_NOT_EXIST;
}
const int X = image.cols, Y = image.rows;
if (tpLineSt.dX < 0 || tpLineSt.dY < 0 || tpLineSt.dX>tpImage.iWidth || tpLineSt.dY>tpImage.iHeight\
|| tpLineEd.dX<0 || tpLineEd.dY<0 || tpLineEd.dX>tpImage.iWidth || tpLineEd.dY>tpImage.iHeight)
return FUNC_ILLEGAL_ARGUMENT;
return FUNC_OK;
}
int eyemEdge1dGenPosRect(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, int iTransition, double dSigma, double dAmpThresh, IntPtr* hObject)
{
cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage);
if (image.empty()) {
return FUNC_IMAGE_NOT_EXIST;
}
const int X = image.cols, Y = image.rows;
if (tpLineSt.dX < 0 || tpLineSt.dY < 0 || tpLineSt.dX>tpImage.iWidth || tpLineSt.dY>tpImage.iHeight\
|| tpLineEd.dX<0 || tpLineEd.dY<0 || tpLineEd.dX>tpImage.iWidth || tpLineEd.dY>tpImage.iHeight)
return FUNC_ILLEGAL_ARGUMENT;
return FUNC_OK;
}
int eyemEdge1dFindLine(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iCapLength, int iCapWidth, int nCalipers, int nFilterSize, int iSearchDirec, double dAmpThreshold, const char* ccTransition, IntPtr* hObject)
{
cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
if (image.empty()) {
return FUNC_IMAGE_NOT_EXIST;
}
//灰度图
int incn = image.channels();
if (incn > 3) {
cv::cvtColor(image, image, cv::COLOR_BGRA2GRAY);
}
else if (incn == 3) {
cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
}
const int X = image.cols, Y = image.rows;
//显示用
cv::Mat cc;
cv::cvtColor(image, cc, cv::COLOR_GRAY2BGR);
//判断越界
if (tpLineSt.dX < 0 || tpLineSt.dY < 0 || tpLineSt.dX>X || tpLineSt.dY>Y || tpLineEd.dX<0 || tpLineEd.dY<0 || tpLineEd.dX>X || tpLineEd.dY>Y) {
return FUNC_ILLEGAL_ARGUMENT;
}
//主轴倾角
double t;
t = atan2(tpLineEd.dY - tpLineSt.dY, tpLineEd.dX - tpLineSt.dX);
//参数限制
iCapWidth = iCapWidth <= 0 ? iCapWidth = 0 : iCapWidth - 1;
iCapLength = iCapLength <= 0 ? 8 : iCapLength;
nCalipers = nCalipers <= 0 ? 5 : nCalipers;
//直线上的坐标
float L = (float)cv::norm(cv::Point2d(tpLineSt.dX, tpLineSt.dY) - cv::Point2d(tpLineEd.dX, tpLineEd.dY));
//步长
float plusStep = (L - (float)nCalipers * (float)iCapWidth) / ((float)nCalipers + 1.0f);
//绘制profileLine
cv::arrowedLine(cc, cv::Point(cvRound(tpLineSt.dX), cvRound(tpLineSt.dY)), cv::Point(cvRound(tpLineEd.dX), cvRound(tpLineEd.dY)), cv::Scalar(255, 153, 0), 1);
//判断极性
bool anyPolarity = strcmp("all", ccTransition) == 0;
//默认过滤一半像素
float* filterK = new float[2 * nFilterSize + 1]();
//定义滤波核
for (int n = 0; n < nFilterSize; n++) {
filterK[n] = 1;
filterK[2 * nFilterSize - n] = -1;
}
cv::Mat whalf(cv::Size(2 * nFilterSize + 1, 1), CV_32FC1, filterK);
//线采样,双线性插值
cv::Size szMap(2 * iCapLength + 1, iCapWidth + 1);
//结果
std::vector<EyemOcsDXY>* tpResults = new std::vector<EyemOcsDXY>();
for (int n = 1; n <= nCalipers; n++)
{
float* pMag = new float[szMap.width * szMap.height];
for (int m = 0; m <= iCapWidth; m++)
{
float plusX, plusY;
plusX = ((float)n * (plusStep + (float)iCapWidth) - (float)iCapWidth + m) * (float)cos(t);
plusY = ((float)n * (plusStep + (float)iCapWidth) - (float)iCapWidth + m) * (float)sin(t);
//中轴线路径上的点
cv::Point2f pLine((float)tpLineSt.dX + plusX, (float)tpLineSt.dY + plusY);
for (int iR = -iCapLength; iR <= iCapLength; iR++) {
//待插值坐标
float _plusX, _plusY;
_plusX = (float)iR * (float)cos(t + iSearchDirec * CV_PI / 2.0) + pLine.x;
_plusY = (float)iR * (float)sin(t + iSearchDirec * CV_PI / 2.0) + pLine.y;
//防止越界
if (_plusX < 1 || _plusX >= X - 2 || _plusY < 1 || _plusY >= Y - 2) {
pMag[(iCapLength + iR) + m * szMap.width] = -1;
continue;
}
#ifdef _DEBUG
//drawPoint("", cv::Point2f(_plusX, _plusY), cv::Scalar(36, 127, 255), 1);
//画像素
float bb = (float)cos(t) * 0.5f;
float aa = (float)sin(t) * 0.5f;
cv::Point2f pt(_plusX, _plusY);
cv::Point2f pts[4];
pts[0].x = (float)(pt.x - aa - bb);
pts[0].y = (float)(pt.y + bb - aa);
pts[1].x = (float)(pt.x + aa - bb);
pts[1].y = (float)(pt.y - bb - aa);
pts[2].x = (float)(2 * pt.x - pts[0].x);
pts[2].y = (float)(2 * pt.y - pts[0].y);
pts[3].x = (float)(2 * pt.x - pts[1].x);
pts[3].y = (float)(2 * pt.y - pts[1].y);
for (int j = 0; j < 4; j++)
{
//drawLine("", pts[j], pts[(j + 1) % 4], cv::Scalar(255, 153, 0));
}
#endif
//整数部分
int x = cvRound(_plusX), y = cvRound(_plusY);
//小数部分
float u = abs(_plusX - ((float)x + 0.5f));
float v = abs(_plusY - ((float)y - 1.0f + 0.5f));
//插值计算灰度值
float gv = (1.0f - v) * (image.ptr<uint8_t>(y - 1)[x] * (1.0f - u) + image.ptr<uint8_t>(y - 1)[x - 1] * u)
+ v * (image.ptr<uint8_t>(y)[x] * (1.0f - u) + image.ptr<uint8_t>(y)[x - 1] * u);
//填入灰度值
pMag[(iCapLength + iR) + m * szMap.width] = gv;
}
}
//采样位置
cv::Point2f midLine((float)tpLineSt.dX + ((float)n * (plusStep + (float)iCapWidth) - (float)iCapWidth + (float)iCapWidth / 2.0f) * (float)cos(t),
(float)tpLineSt.dY + ((float)n * (plusStep + (float)iCapWidth) - (float)iCapWidth + (float)iCapWidth / 2.0f) * (float)sin(t));
//各位置采样路径
cv::Point2f midLineStart, midLineEnd;
midLineStart = cv::Point2f(-iCapLength * (float)cos(t + iSearchDirec * CV_PI / 2.0) + midLine.x, -iCapLength * (float)sin(t + iSearchDirec * CV_PI / 2.0) + midLine.y);
midLineEnd = cv::Point2f(iCapLength * (float)cos(t + iSearchDirec * CV_PI / 2) + midLine.x, iCapLength * (float)sin(t + iSearchDirec * CV_PI / 2.0) + midLine.y);
//采样图像
cv::Mat interMap(szMap, CV_32FC1, pMag);
//计算投影
cv::Mat projectedMap;
cv::reduce(interMap, projectedMap, 0, cv::REDUCE_AVG, CV_32F);
//差分过滤(TODO:加高斯滤波)
float* pFilteredMap = new float[szMap.width];
cv::Mat filteredMap(cv::Size(szMap.width, 1), CV_32FC1, pFilteredMap);
cv::Mat kY = cv::Mat::ones(1, 1, CV_32F);
cv::sepFilter2D(projectedMap, filteredMap, CV_32F, whalf, kY);
//投影峰值查找
std::vector<int> peeks;
findPeak(szMap.width, pFilteredMap, (float)dAmpThreshold, peeks);
//存在满足幅度值的边缘
if (!peeks.empty()) {
float dist = 0; bool found = false;
//判断灰度值过滤类型
if (anyPolarity) {
//不分极性
float maxDist = 0; int maxPos = 0; found = true;
for (auto& peek : peeks) {
if (abs(pFilteredMap[peek]) > maxDist) {
maxDist = abs(pFilteredMap[peek]);
maxPos = peek;
}
}
dist = (float)maxPos;
}
else if (strcmp("positive", ccTransition) == 0) {
int maxPos = 0;
for (auto& peek : peeks) {
if (pFilteredMap[peek] > 0) {
maxPos = peek;
found = true;
break;
}
}
dist = (float)maxPos;
}
else if (strcmp("negative", ccTransition) == 0) {
int maxPos = 0;
for (auto& peek : peeks) {
if (pFilteredMap[peek] < 0) {
maxPos = peek;
found = true;
break;
}
}
dist = (float)maxPos;
}
//阈值限制,计算亚像素坐标
if (found) {
int l, m, r; float a, b, c, u;
m = (int)dist;
l = m - 1 < 0 ? m : m - 1;
r = m + 1 >= szMap.width ? m : m + 1;
a = pFilteredMap[l]; b = pFilteredMap[m];
c = pFilteredMap[r];
u = 0.5f * (a - c) / (a - b - b + c);
//定位结果
float dstX, dstY;
dstX = midLineStart.x + (dist + u) * (float)cos(t + iSearchDirec * CV_PI / 2.0);
dstY = midLineStart.y + (dist + u) * (float)sin(t + iSearchDirec * CV_PI / 2.0);
EyemOcsDXY tpResult;
tpResult.dX = dstX; tpResult.dY = dstY;
tpResults->push_back(tpResult);
}
}
#ifdef _DEBUG
cv::arrowedLine(cc, midLineStart, midLineEnd, cv::Scalar(0, 255, 0), 1);
//画卡尺
float bb = (float)cos(t) * 0.5f;
float aa = (float)sin(t) * 0.5f;
cv::Point2f pt(midLine.x, midLine.y);
cv::Point2f pts[4];
pts[0].x = (float)(pt.x - aa * szMap.width - bb * szMap.height);
pts[0].y = (float)(pt.y + bb * szMap.width - aa * szMap.height);
pts[1].x = (float)(pt.x + aa * szMap.width - bb * szMap.height);
pts[1].y = (float)(pt.y - bb * szMap.width - aa * szMap.height);
pts[2].x = (float)(2 * pt.x - pts[0].x);
pts[2].y = (float)(2 * pt.y - pts[0].y);
pts[3].x = (float)(2 * pt.x - pts[1].x);
pts[3].y = (float)(2 * pt.y - pts[1].y);
for (int j = 0; j < 4; j++)
{
//drawLine("", pts[j], pts[(j + 1) % 4], cv::Scalar(255, 153, 0), 2);
cv::line(cc, pts[j], pts[(j + 1) % 4], cv::Scalar(255, 153, 0));
}
#endif
//释放资源
delete[] pMag;
pMag = NULL;
}
#ifdef _DEBUG
//if (!tpResults->empty()) {
// //拟合直线
// EyemOcsDABC tpLine;
// eyemFitLine((int)tpResults->size(), tpResults->data(), 3, tpLine);
// //画直线y=kx+b--->kx-y+b=0
// //(0,P1)、(X,P2)、(P3,0)、(P4,Y)
// cv::Mat dst;
// getSuperResolution(image, dst, 10);
// cv::cvtColor(dst, dst, cv::COLOR_GRAY2BGR);
// for (int i = 0; i < tpResults->size(); i++)
// {
// cv::circle(dst, cv::Point(int(round(tpResults->at(i).dX * 10)), int(round(tpResults->at(i).dY * 10))), 1, cv::Scalar(0, 255, 0), -1);
// }
//}
#endif
//释放资源(Tips:当存在越界时候在用free释放时会报错)
delete[] filterK;
filterK = NULL;
//输出结果
*hObject = reinterpret_cast<IntPtr>(tpResults);
return FUNC_OK;
}
int eyemEdge1dFindCircle(EyemImage tpImage, EyemOcsDXY tpPoint, int iRadius, int iCapLength, int iCapWidth, int nCalipers, int nFilterSize, int iSearchDirec, double dAmpThreshold, const char* ccTransition, IntPtr* hObject)
{
cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
if (image.empty()) {
return FUNC_IMAGE_NOT_EXIST;
}
//灰度图
int incn = image.channels();
if (incn > 3) {
cv::cvtColor(image, image, cv::COLOR_BGRA2GRAY);
}
else if (incn == 3) {
cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
}
const int X = image.cols, Y = image.rows;
//判断越界
if (tpPoint.dX < 0 || tpPoint.dY < 0 || tpPoint.dX>X || tpPoint.dY>Y) {
return 0;
}
cv::Mat cc;
cv::cvtColor(image, cc, cv::COLOR_GRAY2BGR);
//步长
float plusStep = float(TWO_PI / (float)nCalipers);
//绘制profileLine
cv::circle(cc, cv::Point(cvRound(tpPoint.dX), cvRound(tpPoint.dY)), iRadius, cv::Scalar(), 1);
//判断极性
bool anyPolarity = strcmp("all", ccTransition) == 0;
//默认过滤一半像素
float* filterK = new float[2 * nFilterSize + 1]();
//定义滤波核
for (int n = 0; n < nFilterSize; n++) {
filterK[n] = 1;
filterK[2 * nFilterSize - n] = -1;
}
cv::Mat whalf(cv::Size(2 * nFilterSize + 1, 1), CV_32FC1, filterK);
//参数限制
iCapWidth = iCapWidth <= 0 ? 0 : iCapWidth - 1;
iCapLength = iCapLength <= 0 ? 12 : iCapLength;
nCalipers = nCalipers <= 0 ? 10 : nCalipers;
//线采样
cv::Size szMap(2 * iCapLength + 1, iCapWidth + 1);
//结果
std::vector<EyemOcsDXY>* tpResults = new std::vector<EyemOcsDXY>();
for (float t = -(float)PI; t < PI; t += plusStep)
{
int m = 0;
//路径上的点
float x = float(tpPoint.dX + (float)iRadius * cos(t));
float y = float(tpPoint.dY + (float)iRadius * sin(t));
//采样图像
float* pMag = new float[szMap.width * szMap.height];
for (float n = -(float)iCapWidth / 2.0f; n <= (float)iCapWidth / 2.0f; n += 1.0f, m++)
{
for (int iR = -iCapLength; iR <= iCapLength; iR++) {
//待插值坐标
float _plusX, _plusY;
_plusX = (float)iR * iSearchDirec * (float)cos(t) + x + n * (float)cos(t + CV_PI / 2);
_plusY = (float)iR * iSearchDirec * (float)sin(t) + y + n * (float)sin(t + CV_PI / 2);
//防止越界
if (_plusX < 1 || _plusX >= X - 2 || _plusY < 1 || _plusY >= Y - 2) {
continue;
}
//drawPoint("", cv::Point2f(_plusX, _plusY), cv::Scalar(36, 127, 255), 1);
//整数部分
int x = cvRound(_plusX), y = cvRound(_plusY);
//小数部分
float u = abs(_plusX - ((float)x + 0.5f));
float v = abs(_plusY - ((float)y - 1.0f + 0.5f));
//插值计算灰度值
float gv = (1.0f - v) * (image.ptr<uint8_t>(y - 1)[x] * (1.0f - u) + image.ptr<uint8_t>(y - 1)[x - 1] * u)
+ v * (image.ptr<uint8_t>(y)[x] * (1.0f - u) + image.ptr<uint8_t>(y)[x - 1] * u);
//填入灰度值
pMag[(iCapLength + iR) + m * szMap.width] = gv;
}
}
//采样位置中线
cv::Point2f midLineStart, midLineEnd;
midLineStart = cv::Point2f(-iCapLength * iSearchDirec * (float)cos(t) + x, -iCapLength * iSearchDirec * (float)sin(t) + y);
midLineEnd = cv::Point2f(iCapLength * iSearchDirec * (float)cos(t) + x, iCapLength * iSearchDirec * (float)sin(t) + y);
cv::arrowedLine(cc, cv::Point(), cv::Point(), cv::Scalar(0, 255, 0), 1);
//采样图像
cv::Mat interMap(szMap, CV_32FC1, pMag);
//计算投影
cv::Mat projectedMap;
cv::reduce(interMap, projectedMap, 0, cv::REDUCE_AVG, CV_32F);
//差分过滤(TODO:加高斯滤波)
float* pFilteredMap = new float[szMap.width];
cv::Mat filteredMap(cv::Size(szMap.width, 1), CV_32FC1, pFilteredMap);
cv::sepFilter2D(projectedMap, filteredMap, CV_32F, whalf, cv::Mat::ones(1, 1, CV_32F));
//投影峰值查找
std::vector<int> peeks;
findPeak(szMap.width, pFilteredMap, (float)dAmpThreshold, peeks);
//存在满足幅度值的边缘
if (!peeks.empty()) {
float dist = 0; bool found = false;
//判断灰度值过滤类型
if (anyPolarity) {
//不分极性
float maxDist = 0; int maxPos = 0;
for (auto& peek : peeks) {
if (abs(pFilteredMap[peek]) > maxDist) {
maxDist = abs(pFilteredMap[peek]);
maxPos = peek;
}
}
found = true;
dist = (float)maxPos;
}
else if (strcmp("positive", ccTransition) == 0) {
int maxPos = 0;
for (auto& peek : peeks) {
if (pFilteredMap[peek] > 0) {
maxPos = peek;
found = true;
break;
}
}
dist = (float)maxPos;
}
else if (strcmp("negative", ccTransition) == 0) {
int maxPos = 0;
for (auto& peek : peeks) {
if (pFilteredMap[peek] < 0) {
maxPos = peek;
found = true;
break;
}
}
dist = (float)maxPos;
}
//阈值限制,计算亚像素坐标
if (found) {
int l, m, r; float a, b, c, u;
m = (int)dist;
l = m - 1 < 0 ? m : m - 1;
r = m + 1 >= szMap.width ? m : m + 1;
a = pFilteredMap[l]; b = pFilteredMap[m];
c = pFilteredMap[r];
u = 0.5f * (a - c) / (a - b - b + c);
//定位结果
float dstX, dstY;
dstX = midLineStart.x + iSearchDirec * (dist + u) * (float)cos(t);
dstY = midLineStart.y + iSearchDirec * (dist + u) * (float)sin(t);
EyemOcsDXY tpResult;
tpResult.dX = dstX; tpResult.dY = dstY;
tpResults->push_back(tpResult);
}
}
#ifdef _DEBUG
//画卡尺
float bb = (float)cos(t + CV_PI / 2) * 0.5f;
float aa = (float)sin(t + CV_PI / 2) * 0.5f;
cv::Point2f pt(x, y);
cv::Point2f pts[4];
pts[0].x = (float)(pt.x - aa * szMap.width - bb * szMap.height);
pts[0].y = (float)(pt.y + bb * szMap.width - aa * szMap.height);
pts[1].x = (float)(pt.x + aa * szMap.width - bb * szMap.height);
pts[1].y = (float)(pt.y - bb * szMap.width - aa * szMap.height);
pts[2].x = (float)(2 * pt.x - pts[0].x);
pts[2].y = (float)(2 * pt.y - pts[0].y);
pts[3].x = (float)(2 * pt.x - pts[1].x);
pts[3].y = (float)(2 * pt.y - pts[1].y);
for (int j = 0; j < 4; j++)
{
cv::line(cc, pts[j], pts[(j + 1) % 4], cv::Scalar(255, 153, 0));
}
#endif
//释放资源
delete[] pMag;
pMag = NULL;
}
//释放资源
delete[] filterK;
filterK = NULL;
//输出结果
*hObject = reinterpret_cast<IntPtr>(tpResults);
return FUNC_OK;
}
int eyemEdge1dGenArc(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, int iEdgeDirec, EyemOcsDXY* tpEdge)
{
return FUNC_OK;
}
int eyemPolarTrans(EyemImage tpImage, EyemOcsDXY tpCenter, int iRadius, int iSapWidth)
{
cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
if (image.empty()) {
return FUNC_IMAGE_NOT_EXIST;
}
//灰度图
int incn = image.channels();
if (incn > 3) {
cv::cvtColor(image, image, cv::COLOR_BGRA2GRAY);
}
else if (incn == 3) {
cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
}
const int X = image.cols, Y = image.rows;
//判断越界
if (tpCenter.dX < 0 || tpCenter.dY < 0 || tpCenter.dX>X || tpCenter.dY>Y) {
return 0;
}
//计算目标尺寸
float C = 2.0f * (float)CV_PI * (float)(iRadius + iSapWidth);
//步长(角度)
float plusStep = 2.0f * asinf((1.0f + (C - (float)cvRound(C)) / (float)cvRound(C)) / (2.0f * (float)(iRadius + +iSapWidth)));
//长度
int iSapLength = cvRound(2.0f * CV_PI / plusStep);
//结果图像
cv::Mat polarMat(cv::Size(iSapLength, 2 * iSapWidth), CV_8UC1, cv::Scalar(0));
for (int n = iSapWidth; n > -iSapWidth; n--)
{
//周长
C = 2.0f * (float)CV_PI * (float)(iRadius + n);
//步长(角度)
plusStep = 2.0f * asinf((1.0f + (C - (float)cvRound(C)) / (float)cvRound(C)) / (2.0f * (float)(iRadius + n)));
//长度
iSapLength = cvRound(2.0f * CV_PI / plusStep);
//线采样
cv::Size szMap(iSapLength, 1);
//采样图像
int m = 0; float* pPolarBuf = new float[szMap.width * szMap.height * sizeof(float_t)];
for (double t = -CV_PI; t < CV_PI; t += plusStep, m++)
{
//路径上的点
float _plusX = float(tpCenter.dX + (float)(iRadius + n) * cos(t));
float _plusY = float(tpCenter.dY + (float)(iRadius + n) * sin(t));
//防止越界
if (_plusX < 1 || _plusX >= X - 2 || _plusY < 1 || _plusY >= Y - 2) {
continue;
}
//整数部分
int x = cvRound(_plusX), y = cvRound(_plusY);
//小数部分
float u = abs(_plusX - ((float)x + 0.5f));
float v = abs(_plusY - ((float)y - 1.0f + 0.5f));
//插值计算灰度值
float gv = (1.0f - v) * (image.ptr<uint8_t>(y - 1)[x] * (1.0f - u) + image.ptr<uint8_t>(y - 1)[x - 1] * u)
+ v * (image.ptr<uint8_t>(y)[x] * (1.0f - u) + image.ptr<uint8_t>(y)[x - 1] * u);
//填入灰度值
pPolarBuf[m] = gv;
}
//仅支持8U类型
cv::Mat polarMat1(szMap, CV_32FC1, pPolarBuf);
polarMat1.convertTo(polarMat1, CV_8UC1);
//插值,默认双线性插值
cv::Mat polarMat2;
cv::resize(polarMat1, polarMat2, cv::Size(polarMat.cols, 1));
//保存到大图
polarMat2.copyTo(polarMat(cv::Rect(0, iSapWidth - n, polarMat.cols, 1)));
//释放资源
delete[] pPolarBuf;
pPolarBuf = NULL;
}
//绘制profileLine
//drawCircle("", tpCenter, iRadius - iSapWidth, cv::Scalar(255, 153, 0), 2);
//drawCircle("", tpCenter, iRadius + iSapWidth, cv::Scalar(255, 153, 0), 2);
return FUNC_OK;
}
bool eyemEdge1dGenFree(IntPtr hObject)
{
std::vector<EyemOcsDXY>* tpEdges = reinterpret_cast<std::vector<EyemOcsDXY>*>(hObject);
delete tpEdges;
tpEdges = NULL;
return true;
}