Changeset 4076 for trunk/MagicSoft/Cosy/videodev
- Timestamp:
- 05/15/04 16:46:27 (21 years ago)
- Location:
- trunk/MagicSoft/Cosy/videodev
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Cosy/videodev/Camera.cc
r2518 r4076 17 17 inline int Camera::Ioctl(int req, void *opt, const char *str) 18 18 { 19 int rc = ioctl(fd, req, opt); 20 21 if (rc==-1) 22 { 23 cout << "Error! Ioctl " << req << ": "; 19 while (1) 20 { 21 const int rc = ioctl(fd, req, opt); 22 if (rc>=0) 23 return rc; 24 25 // errno== 4: Interrupted system call 26 // errno==16: Device or resource busy 27 if (errno==4 || errno==16) 28 { 29 cout << "Camera: ioctl returned rc=" << rc << " '"; 30 cout << (str?str:strerror(errno)) << "' (errno = " << errno << ")" << endl; 31 usleep(10); 32 continue; 33 } 34 35 cout << "Error! Ioctl " << hex << req << ": errno=" << dec << errno << " - "; 24 36 cout << (str?str:strerror(errno)) << " (rc=" << rc << ")" << endl; 25 }26 27 return rc;37 return rc; 38 } 39 return -1; 28 40 } 29 41 30 42 void Camera::Error(const char *str, int fatal) 31 43 { 32 cout << endl 33 << (fatal?"Fatal ":"") << "Error! " << str << ": " << strerror(errno)34 44 cout << endl; 45 cout << (fatal?"Fatal ":"") << "Error! " << str << ": " << strerror(errno); 46 cout << endl; 35 47 36 48 if (fatal) … … 39 51 void Camera::SigInit() 40 52 { 41 return; 42 struct sigaction act, old; 43 44 memset(&act, 0, sizeof(act)); 45 46 act.sa_handler = SigAlarm; 47 48 sigemptyset(&act. sa_mask); 49 sigaction(SIGALRM, &act, &old); 50 51 // signal(SIGINT, ctrlc); 53 /* 54 struct sigaction act, old; 55 56 memset(&act, 0, sizeof(act)); 57 58 act.sa_handler = SigAlarm; 59 60 sigemptyset(&act. sa_mask); 61 sigaction(SIGALRM, &act, &old); 62 63 // signal(SIGINT, ctrlc); 64 */ 52 65 } 53 66 … … 74 87 int Camera::StartGrab(unsigned int frame) 75 88 { 89 // We could also get RGB555, RGB565 and RGB32. But we want 90 // RGB24 because we have a 8bit DAC which gives us 8bit per 91 // color ==> RGB24 which is in the following the most simple 92 // to process. 76 93 static struct video_mmap gb = 77 94 { 78 0, 79 rows, cols, 80 VIDEO_PALETTE_RGB24 95 0, // frame 96 rows, cols, // height, width 97 VIDEO_PALETTE_RGB24 // palette 81 98 }; 82 99 … … 95 112 } 96 113 97 Camera::Camera(PixClient &client) : fd(-1), iBufferSize(0), fClient(client), fMutex(), fCond(&fMutex) 98 { 99 cout << "Starting thread..." << flush; 100 //pthread_cond_init(&fCond, NULL); 101 //pthread_mutex_init(&fMux, NULL); 102 //pthread_mutex_lock(&fMux); 103 fMutex.Lock(); 104 pthread_create(&fThread, NULL, MapThread, this); 105 cout << "done." << endl; 106 107 cout << "/dev/video: opening..." << flush; 114 Camera::Camera(PixClient &client, Int_t nch) : fd(-1), iBufferSize(0), fClient(client), fCond(), fMutex(fCond.GetMutex()) 115 { 116 cout << "Camera: " << this << " /dev/video: opening..." << flush; 108 117 109 118 // … … 126 135 // get input channel 0 information 127 136 // 137 128 138 struct video_channel ch; 129 ch.channel = 0;139 ch.channel = nch; 130 140 Ioctl(VIDIOCGCHAN, &ch); 131 141 … … 160 170 cout << "Buffer Size: " << (void*)iBufferSize << endl; 161 171 cout << "grab: use: 768x576 24 bit TrueColor (LE: bgr) = " << (void*)(768*576*3) << "b" << endl; 172 173 // if (fMutex->UnLock()==13) 174 // cout << "Camera::Camera - tried to unlock mutex locked by other thread." << endl; 175 176 cout << "Starting thread..." << flush; 177 //pthread_cond_init(&fCond, NULL); 178 //pthread_mutex_init(&fMux, NULL); 179 //pthread_mutex_lock(&fMux); 180 // if (fMutex->Lock()==13) 181 // cout << "Camera::Camera - mutex is already locked by this thread" << endl; 182 pthread_create(&fThread, NULL, MapThread, this); 183 cout << "done." << endl; 184 162 185 } 163 186 164 187 Camera::~Camera() 165 188 { 166 cout << "Stopping thread..." << flush;167 189 pthread_cancel(fThread); 168 //pthread_mutex_destroy(&fMux); 169 //pthread_cond_destroy(&fCond); 190 191 cout << "/dev/video: closing... " << flush; 192 if (fd != -1) 193 { 194 close(fd); 195 fd = -1; 196 } 170 197 cout << "done." << endl; 171 172 cout << "/dev/video: closing... " << flush;173 198 174 199 // unmap device memory 175 200 if ((int)pMapBuffer != -1) 176 201 munmap(pMapBuffer, iBufferSize); 177 178 if (fd != -1) 179 { 180 close(fd); 181 fd = -1; 182 } 183 184 cout << " Done." << endl; 185 } 186 202 } 187 203 188 204 void *Camera::MapThread(void *arg) … … 198 214 } 199 215 216 void Camera::ExitLoop() 217 { 218 // cout << "ExitLoop..." << endl; 219 fStop = 1; 220 while (IsRunning()) 221 usleep(1); 222 // cout << "Loop exited." << endl; 223 } 224 225 // 226 // flag if the execution is running or not 227 // 228 int Camera::IsRunning() const 229 { 230 const Int_t rc = fMutex->TryLock(); 231 if (rc==0) 232 return false; 233 234 if (rc==13) 235 return false; 236 237 fMutex->UnLock(); 238 return true; 239 } 240 200 241 void Camera::LoopStep(const unsigned long n) 201 242 { … … 217 258 { 218 259 #define IsOdd(i) (2*(i/2)!=i) 260 cout << "Camera::Thread started..." << endl; 219 261 while (1) 220 262 { 221 // pthread_cond_wait(&fCond, &fMux);263 //cout << "Wait..." << flush; 222 264 fCond.Wait(); 265 //cout << "done." << endl; 266 if (fd==-1) 267 break; 223 268 224 269 MStopwatch t; … … 259 304 t.Stop(); 260 305 t.Print(i); 261 262 fRunning = 0; 263 } 306 } 307 308 fMutex->UnLock(); 309 310 cout << "Camera::Thread.. stopped." << endl; 264 311 } 265 312 … … 272 319 } 273 320 274 // 275 // Stop running loop 276 // 277 fStop = 1; 278 279 // 280 // Wait until loop is stopped (pthread_cond_wait is executing) 281 // set new number of frames to process 282 // 283 fMutex.Lock(); 284 //pthread_mutex_lock(&fMux); 285 fNum = nof; 286 fStop = 0; 287 fRunning = 1; 288 fMutex.UnLock(); 289 //pthread_mutex_unlock(&fMux); 321 cout << "Loop..." << endl; 322 ExitLoop(); 323 324 fNum = nof; 325 fStop = 0; 290 326 291 327 // … … 293 329 // 294 330 fCond.Broadcast(); 295 //pthread_cond_signal(&fCond);296 331 } 297 332 -
trunk/MagicSoft/Cosy/videodev/Camera.h
r2518 r4076 45 45 46 46 int fStop; 47 int fRunning;47 // int fRunning; 48 48 49 49 char fImg[cols*rows]; … … 53 53 54 54 pthread_t fThread; 55 TMutex fMutex;56 55 TCondition fCond; 56 TMutex *fMutex; 57 57 //pthread_mutex_t fMux; 58 58 //pthread_cond_t fCond; … … 81 81 82 82 public: 83 Camera(PixClient &client );83 Camera(PixClient &client, Int_t ch=0); 84 84 virtual ~Camera(); 85 85 … … 88 88 // 89 89 void Loop(const unsigned long nof=0); 90 void ExitLoop() 91 { 92 fStop = 1; 93 while (IsRunning()) 94 usleep(1); 95 } 96 97 // 98 // flag if the execution is running or not 99 // 100 int IsRunning() const { return fRunning; } 90 void ExitLoop(); 91 int IsRunning() const; 101 92 102 93 // -
trunk/MagicSoft/Cosy/videodev/FilterLed.cc
r2278 r4076 15 15 return; 16 16 17 for (int dx=-(int)(rpix*0.7); dx<(int)(rpix*0.7); dx+ +)17 for (int dx=-(int)(rpix*0.7); dx<(int)(rpix*0.7); dx+=2) 18 18 { 19 19 const int dy = (int)sqrt(rpix*rpix-dx*dx); … … 64 64 DrawBox(x+5, y, x+8, y, m); 65 65 DrawBox(x, y-8, x, y-5, m); 66 return;67 66 } 68 67 … … 76 75 77 76 // 78 // calculate mean value 77 // calculate mean value (speed optimized) 79 78 // 80 79 while (s<e0) … … 104 103 105 104 int FilterLed::GetMeanPosition(const int x, const int y, 106 const int box, float &mx, float &my ) const105 const int box, float &mx, float &my, unsigned int &sum) const 107 106 { 108 107 unsigned int sumx=0; 109 108 unsigned int sumy=0; 110 unsigned int sum =0; 111 109 110 sum=0; 112 111 for (int dx=x-box; dx<x+box+1; dx++) 113 112 for (int dy=y-box; dy<y+box+1; dy++) … … 129 128 { 130 129 float mx, my; 131 return GetMeanPosition(x, y, box, mx, my); 130 unsigned int sum; 131 return GetMeanPosition(x, y, box, mx, my, sum); 132 132 } 133 133 /* … … 252 252 void FilterLed::Execute(Leds &leds, int xc, int yc) const 253 253 { 254 int x0 = xc-fBox W;255 int x1 = xc+fBox W;256 int y0 = yc-fBox H;257 int y1 = yc+fBox H;254 int x0 = xc-fBox; 255 int x1 = xc+fBox; 256 int y0 = yc-fBox; 257 int y1 = yc+fBox; 258 258 259 259 if (x0<0) x0=0; … … 280 280 sq /= wx*hy; 281 281 282 // 254 because b<=max and not b<max 282 283 const double sdev = sqrt(sq-sum*sum); 283 284 const byte max = sum+fCut*sdev>254 ? 254 : (byte)(sum+fCut*sdev); … … 348 349 349 350 Float_t mx, my; 350 GetMeanPosition(pos[i]%fW, pos[i]/fW, 5, mx, my); 351 unsigned int sum; 352 GetMeanPosition(pos[i]%fW, pos[i]/fW, 5, mx, my, sum); 351 353 352 354 leds.Add(mx, my, 0, 0, mag[i]); … … 354 356 355 357 RemoveTwinsInterpol(leds, first, 5); 358 } 359 360 void FilterLed::FindStar(Leds &leds, int xc, int yc) const 361 { 362 // fBox: radius of the inner (signal) box 363 // Radius of the outer box is fBox*sqrt(2) 364 365 // 366 // Define inner box in which to search the signal 367 // 368 int x0 = xc-fBox; 369 int x1 = xc+fBox; 370 int y0 = yc-fBox; 371 int y1 = yc+fBox; 372 373 if (x0<0) x0=0; 374 if (y0<0) y0=0; 375 if (x1>fW) x1=fW; 376 if (y1>fH) y1=fH; 377 378 // 379 // Define outer box (excluding inner box) having almost 380 // the same nuymber of pixels in which the background 381 // is calculated 382 // 383 const double sqrt2 = sqrt(2.); 384 385 int xa = xc-(int)rint(fBox*sqrt2); 386 int xb = xc+(int)rint(fBox*sqrt2); 387 int ya = yc-(int)rint(fBox*sqrt2); 388 int yb = yc+(int)rint(fBox*sqrt2); 389 390 if (xa<0) xa=0; 391 if (ya<0) ya=0; 392 if (xb>fW) xb=fW; 393 if (yb>fH) yb=fH; 394 395 // 396 // Calculate average and sdev for a square 397 // excluding the inner part were we expect 398 // the signal to be. 399 // 400 double sum = 0; 401 double sq = 0; 402 403 int n=0; 404 for (int x=xa; x<xb+1; x++) 405 for (int y=ya; y<yb+1; y++) 406 { 407 if (x>x0 && x<x1 && y>y0 && y<y1) 408 continue; 409 410 byte &b = fImg[y*fW+x]; 411 412 sum += b; 413 sq += b*b; 414 n++; 415 } 416 417 sum /= n; 418 sq /= n; 419 420 // 254 because b<=max and not b<max 421 const double sdev = sqrt(sq-sum*sum); 422 const byte max = sum+fCut*sdev>254 ? 254 : (byte)(sum+fCut*sdev); 423 424 // 425 // clean image from noise 426 // (FIXME: A lookup table could accelerate things... 427 // 428 n=0; 429 for (int x=x0; x<x1; x++) 430 for (int y=y0; y<y1; y++) 431 { 432 byte &b = fImg[y*fW+x]; 433 if (b<=max) 434 b = 0; 435 else 436 n++; 437 } 438 439 // 440 // Mark the background region 441 // 442 for (int x=xa; x<xb+1; x+=2) 443 { 444 fImg[ya*fW+x]=0xf0; 445 fImg[yb*fW+x]=0xf0; 446 } 447 for (int y=ya; y<yb+1; y+=2) 448 { 449 fImg[y*fW+xa]=0xf0; 450 fImg[y*fW+xb]=0xf0; 451 } 452 453 // 454 // Check if any pixel found... 455 // 456 if (n<5) 457 return; 458 459 // 460 // Get the mean position of the star 461 // 462 float mx, my; 463 unsigned int mag; 464 int pos = GetMeanPosition(xc, yc, fBox-1, mx, my, mag); 465 466 if (pos<0 || pos>=fW*fH && fImg[pos]<sum+fCut*sdev) 467 return; 468 469 cout << "Mean=" << sum << " SDev=" << sdev << " : "; 470 cout << "Sum/n = " << sum << "/" << n << " = " << (n==0?0:mag/n) << endl; 471 472 leds.Add(mx, my, 0, 0, -2.5*log10((float)mag)+13.7); 356 473 } 357 474 -
trunk/MagicSoft/Cosy/videodev/FilterLed.h
r2278 r4076 17 17 int fW; 18 18 int fH; 19 int fBoxW; 20 int fBoxH; 19 int fBox; 21 20 float fCut; 22 21 23 22 void GetMinMax(const int offset, byte *min, byte *max) const; 24 23 int GetMeanPosition(const int x, const int y, const int box) const; 25 int GetMeanPosition(const int x, const int y, const int box, float &mx, float &my ) const;24 int GetMeanPosition(const int x, const int y, const int box, float &mx, float &my, unsigned int &sum) const; 26 25 void RemoveTwinsInterpol(Leds &leds, Int_t first, Double_t radius) const; 27 26 void DrawBox(const int x1, const int y1, … … 31 30 public: 32 31 FilterLed(byte *img, int w, int h, double cut=2.5) 33 : fImg(img), fW(w), fH(h), fBox W(w/2), fBoxH(h/2), fCut(cut)32 : fImg(img), fW(w), fH(h), fBox(w>h?w:h), fCut(cut) 34 33 { 35 34 } 36 35 37 36 FilterLed(byte *img, int w, int h, int box, double cut=2.5) 38 : fImg(img), fW(w), fH(h), fBox W(box), fBoxH(box), fCut(cut)37 : fImg(img), fW(w), fH(h), fBox(box), fCut(cut) 39 38 { 40 39 } 41 40 42 FilterLed(byte *img, int w, int h, int boxw, int boxh, double cut=2.5)43 : fImg(img), fW(w), fH(h), fBoxW(boxw), fBoxH(boxh), fCut(cut)44 { 45 }41 void SetBox(int box) { fBox = box; } 42 void SetCut(float cut) { fCut = cut; } 43 44 void FindStar(Leds &leds, int xc, int yc) const; 46 45 47 46 void Execute(Leds &leds, int xc, int yc) const; … … 55 54 void Stretch() const; 56 55 void DrawCircle(float cx, float cy, float r, byte col=0x40) const; 57 void DrawCircle(float r, byte col=0x40) const { DrawCircle( r, fW/2.+.5, fH/2.+.5, col); }56 void DrawCircle(float r, byte col=0x40) const { DrawCircle(fW/2, fH/2, r, col); } 58 57 void DrawCircle(const Ring &c, byte col=0x40) const; 59 58 void DrawCircle(const Ring &c, double r, byte col) const; -
trunk/MagicSoft/Cosy/videodev/PixGetter.h
r2278 r4076 5 5 { 6 6 public: 7 virtual ~PixGetter() { } 7 8 virtual void ExitLoop() {} 8 9 };
Note:
See TracChangeset
for help on using the changeset viewer.