• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

tests/testFlashLatency.cpp

00001 #include <FCam/N900.h>
00002 #include <stdio.h>
00003 #include <iostream>
00004 
00005 using namespace FCam;
00006 
00007 // to use this, place the N900 face down on a white piece of paper
00008 // with the lens cap open. It will use the rolling shutter to
00009 // calculate flash latency (and rolling shutter speed).
00010 
00011 int main(int argc, char **argv) {
00012     printf("Constructing sensor\n");
00013     N900::Sensor sensor;
00014     printf("Constructing flash\n");
00015     N900::Flash flash;
00016     printf("Attaching flash to sensor\n");
00017     sensor.attach(&flash);
00018     printf("Constructing shot\n");
00019     Shot shot;
00020     shot.exposure = 100;
00021     shot.gain = 8.0f;
00022     shot.frameTime = 200000;
00023     shot.image = Image(2592, 1968, RAW, Image::AutoAllocate);
00024 
00025     printf("Constructing fire action\n");
00026     Flash::FireAction fire(&flash);
00027     fire.duration = flash.minDuration();
00028     fire.brightness = flash.maxBrightness();
00029     shot.actions.insert(&fire);
00030 
00031     printf("Capturing shot\n");
00032     int time[80];
00033     int scanline[80];
00034     for (int i = 0; i < 80; i++) {
00035         time[i] = i*500 - 500;
00036         fire.time = time[i];
00037         sensor.capture(shot);
00038     }
00039 
00040     printf("Analyzing images\n");
00041     for (int i = 0; i < 80; i++) {        
00042         scanline[i] = -1;
00043         Frame f = sensor.getFrame();
00044         Image im = f.image();
00045 
00046         Flash::Tags tags(f);
00047 
00048         std::cout << "Flash tags: " 
00049                   << tags.start << ", " 
00050                   << tags.duration  << ", "
00051                   << tags.brightness << ", "
00052                   << tags.peak << std::endl;
00053         if (!im.valid()) {
00054             printf("Image is not valid!\n");
00055         } else {
00056             for (int y = 0; y < im.height(); y++) {
00057                 int sum = 0;
00058                 for (int x = 0; x < 128; x++) {
00059                     unsigned b = ((unsigned short *)(im(x, y)))[0];
00060                     sum += b;
00061                 }
00062                 if (sum > 1000) {
00063                     scanline[i] = y;
00064                     break;
00065                 }
00066             }
00067         }
00068         printf("%d: %d\n", time[i] + f.exposure(), scanline[i]);
00069     }
00070     
00071     // make a line of best fit through all the good data
00072     double sx2 = 0;
00073     double sx = 0;
00074     double sy = 0;
00075     double sxy = 0;
00076     int n = 0;
00077     for (int i = 0; i < 80; i++) {
00078         if (scanline[i] <= 100) continue;
00079         n++;
00080         sx2 += scanline[i]*scanline[i];
00081         sx += scanline[i];
00082         sxy += time[i]*scanline[i];
00083         sy += time[i];
00084     }
00085 
00086     double invDet = 1.0/(sx2*n - sx*sx);
00087     double a = invDet * (n * sxy - sx*sy);
00088     double b = invDet * (sx2*sy - sx*sxy);
00089     double shutterTime = shot.image.height()*a;
00090     printf("Rolling shutter time is %f us\n", shutterTime);
00091     printf("Flash latency is %f us\n", b);
00092    
00093 
00094     return 0;
00095 }
00096 

Generated on Fri Sep 24 2010 15:53:00 for FCam by  doxygen 1.7.1