source: trunk/MagicSoft/Cosy/videodev/Filter2.cc@ 1692

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