source: trunk/MagicSoft/Cosy/videodev/Filter.cc@ 1621

Last change on this file since 1621 was 1531, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 4.1 KB
Line 
1#include "Filter.h"
2
3#include <memory.h> // memset
4#include <iostream.h> // cout
5
6ClassImp(Filter);
7
8void Filter::DrawBox(const int x1, const int y1,
9 const int x2, const int y2,
10 byte *buffer, const int col)
11{
12 for (int x=x1; x<x2+1; x++)
13 for (int y=y1; y<y2+1; y++)
14 buffer[y*768+x] = col;
15}
16
17void Filter::MarkPoint(const int x, const int y, byte *buffer, const int col)
18{
19 DrawBox(x-8, y, x-5, y, buffer, col);
20 DrawBox(x, y+5, x, y+8, buffer, col);
21 DrawBox(x+5, y, x+8, y, buffer, col);
22 DrawBox(x, y-8, x, y-5, buffer, col);
23 return;
24}
25
26float Filter::Mean(const byte *buffer, const int offset, int *min, int *max)
27{
28 double mean = 0.0;
29
30 *min = 0xff;
31 *max = 0x00;
32
33 //
34 // calculate mean value
35 //
36 for (int x=offset; x<768-offset; x++)
37 for (int y=offset; y<576-offset; y++)
38 {
39 byte val = buffer[y*768+x];
40
41 mean += val;
42
43 if (val>*max)
44 *max = val;
45
46 if (val<*min)
47 *min = val;
48 }
49
50 mean /= (768-2*offset)*(576-2*offset);
51
52 return mean;
53}
54
55float Filter::SDev(const byte *buffer, const int offset, const double mean)
56{
57 //
58 // calculate sigma
59 //
60 double sdev=0.0;
61
62 for (int x=offset; x<768-offset; x++)
63 for (int y=offset; y<576-offset; y++)
64 {
65 const float val = mean - buffer[y*768+x];
66
67 sdev += val*val;
68 }
69
70 sdev /= (768-2*offset)*(576-2*offset)-1;
71
72 return sqrt(sdev);
73}
74
75int Filter::GetMeanPosition(const byte *bitmap, const int x, const int y,
76 const int box)
77{
78 unsigned int sumx=0;
79 unsigned int sumy=0;
80
81 unsigned int sum=0;
82
83 for (int dx=x-box; dx<x+box+1; dx++)
84 for (int dy=y-box; dy<y+box+1; dy++)
85 {
86 const byte m = bitmap[dy*768+dx]; // desc->buffer[3*(x+y*768)]; //
87
88 sumx += m*dx;
89 sumy += m*dy;
90 sum += m;
91 }
92
93 const float px = (float)sumx/sum;
94 const float py = (float)sumy/sum;
95
96 return (int)py*768 + (int)px;
97}
98
99void Filter::Execute(byte *img)
100{
101 const int offset = 10;
102
103 int max;
104 int min;
105
106 const float mean = Mean(img, offset, &min, &max);
107 const float sdev = SDev(img, offset, mean);
108
109 const float cut = mean + 2.5*sdev;
110
111 //
112 // clean image from noise
113 //
114 for (int x=0; x<768; x++)
115 for (int y=0; y<576; y++)
116 {
117 if (img[y*768+x]>cut)
118 continue;
119
120 //
121 // FIXME: Create LOOKUP Table!
122 //
123
124 img[y*768+x] = 0;
125 }
126
127 //
128 // find mean points
129 //
130 const int maxpnt = 0x1000;
131
132 int pos[maxpnt+1][2]; // FIXME
133 int cnt = 0;
134
135 for (int x=offset; x<768-offset; x++)
136 {
137 for (int y=offset; y<576-offset; y++)
138 {
139 if (img[x+768*y]==0)
140 continue;
141
142 const int ipos = GetMeanPosition(img, x, y, 5);
143
144 int j;
145 for (j=0; j<cnt; j++)
146 {
147 if (pos[j][0]==ipos)
148 {
149 if (pos[j][1] < 0xf0)
150 pos[j][1] += 0x10;
151 break;
152 }
153 }
154 if (cnt && j<cnt)
155 continue;
156
157 pos[cnt][0] = ipos;
158 pos[cnt][1] = 0x10;
159
160 cnt++;
161
162 if (cnt==maxpnt)
163 break;
164 }
165 if (cnt==maxpnt)
166 {
167 cout << "Error! More than " << maxpnt << " stars found." << endl;
168 break;
169 }
170 }
171
172 //
173 // Draw marker for found stars into picture
174 //
175 int points=0;
176
177 byte marker[768*576];
178 memset(marker, 0, 768*576);
179
180 for (int i=0; i<cnt; i++)
181 {
182 if (pos[i][1]>0xa0)
183 {
184 points++;
185
186 int px = pos[i][0]%768;
187 int py = pos[i][0]/768;
188
189 MarkPoint(px, py, marker, pos[i][1]);
190 }
191 }
192
193 //
194 // Copy markers into image
195 //
196 for (int x=0; x<768*576; x++)
197 {
198 if (!marker[x])
199 continue;
200
201 img[x]=marker[x];
202 }
203}
204
Note: See TracBrowser for help on using the repository browser.