-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
imavmarker.c
134 lines (107 loc) · 3.68 KB
/
imavmarker.c
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
/*
* Copyright (C) 2015
*
* This file is part of paparazzi
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file "modules/computer_vision/blob/imavmarker.c"
* Find a IMAV pattern
*/
#include "imavmarker.h"
/**
* Find the marker location.
* @param[in] *input The input image to filter
* @param[in] M The distance between the pixel of interest and farthest neighbor pixel [pixel]
* @return The deviation of the marker location wrt the center.
*/
bool_t marker_enabled = TRUE;
#define Img(X,Y)(source[(Y)*input->w*2+(X)*2+1])
#define Out(X,Y)(source[(Y)*input->w*2+(X)*2])
inline int AbsDiff(int A, int B);
inline int AbsDiff(int A, int B)
{
if (A < B) {
return B - A;
}
return A - B;
}
struct marker_deviation_t marker(struct image_t *input, uint8_t M)
{
struct marker_deviation_t marker_deviation;
marker_deviation.x = 0;
marker_deviation.y = 0;
marker_deviation.inlier = 0;
if (! marker_enabled)
return marker_deviation;
uint8_t *source = (uint8_t *) input->buf;
uint16_t x, y, i, j, k;
if (M < 1) { M = 1; }
source = (uint8_t *) input->buf;
int maxx = 160;
int maxy = 120;
int maxv = 0;
for (j = M; j < (input->h - M); j++) {
for (i = M; i < (input->w - M); i++) {
int bad, good;
good = bad = 0;
for (k = 1; k < M; k++) {
// Pattern must be symmetric
bad += AbsDiff(Img(i - k, j) , Img(i + k, j));
bad += AbsDiff(Img(i, j - k) , Img(i, j + k));
bad += AbsDiff(Img(i - k, j - k) , Img(i + k, j + k));
bad += AbsDiff(Img(i + k, j - k) , Img(i - k, j + k));
// Pattern: Must have perpendicular contrast
good += AbsDiff(Img(i - k, j) + Img(i + k, j), Img(i, j - k) + Img(i, j + k));
good += AbsDiff(Img(i - k, j - k) + Img(i + k, j + k), Img(i + k, j - k) + Img(i - k, j + k));
}
for (k = 4; k < M; k += 2) {
// Pattern must be symmetric
bad += AbsDiff(Img(i - k, j - k / 2) , Img(i + k, j + k / 2));
bad += AbsDiff(Img(i + k / 2, j - k) , Img(i - k / 2, j + k));
bad += AbsDiff(Img(i - k / 2, j - k) , Img(i + k / 2, j + k));
bad += AbsDiff(Img(i + k, j - k / 2) , Img(i - k, j + k / 2));
// Pattern: Must have perpendicular contrast
good += AbsDiff(Img(i - k, j - k / 2) + Img(i + k, j + k / 2), Img(i + k / 2, j - k) + Img(i - k / 2, j + k));
good += AbsDiff(Img(i - k / 2, j - k) + Img(i + k / 2, j + k), Img(i + k, j - k / 2) + Img(i - k, j + k / 2));
}
int v = good - bad;
if (v < 0) {
v = 0;
}
if (v > maxv) {
maxv = v;
maxx = i;
maxy = j;
}
if (v > 0) {
Out(i, j) = 0xff;
}
}
}
// Display the marker location and center-lines.
for (y = 0; y < input->h; y++) {
Out(maxx, y) = 0xff;
}
for (x = 0; x < input->w; x++) {
Out(x, maxy) = 0xff;
}
marker_deviation.x = ((int32_t)0) - ((int32_t)(input->w) / 2);
marker_deviation.y = -((int32_t)0) + ((int32_t)(input->h) / 2);
marker_deviation.inlier = 0;
//printf("The number of inliers = %i\n", counter3);
return marker_deviation;
}