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

examples/example4/example4.cpp

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <math.h>
00004 #include <assert.h>
00005 #include <FCam/N900.h>
00006 #include <FCam/AutoExposure.h>
00007 #include <FCam/AutoWhiteBalance.h>
00008 
00011 // Select the platform
00012 namespace Plat = FCam::N900;
00013 // namespace Plat = FCam::F2;
00014 
00015 /***********************************************************/
00016 /* Autoexposure                                            */
00017 /*                                                         */
00018 /* This example shows how to request streams and deal with */
00019 /* the incoming frames, and also uses the provided         */
00020 /* auto-exposure and auto-white-balance routines.          */
00021 /***********************************************************/
00022 int main(int argc, char ** argv) {
00023 
00024     // Make a sensor
00025     Plat::Sensor sensor;
00026     
00027     // Shot
00028     FCam::Shot stream1;
00029     // Set the shot parameters
00030     stream1.exposure = 33333;
00031     stream1.gain = 1.0f;
00032 
00033     // We don't bother to set frameTime in this example. It defaults
00034     // to zero, which the implementation will clamp to the minimum
00035     // possible value given the exposure time.
00036 
00037     // Request an image size and allocate storage
00038     stream1.image = FCam::Image(640, 480, FCam::UYVY);
00039 
00040     // Enable the histogram unit
00041     stream1.histogram.enabled = true;
00042     stream1.histogram.region = FCam::Rect(0, 0, 640, 480);
00043     
00044     // We will stream until the exposure stabilizes
00045     int count = 0;          // # of frames streamed
00046     int stableCount = 0;    // # of consecutive frames with stable exposure
00047     float exposure;         // total exposure for the current frame (exposure time * gain)
00048     float lastExposure = 0; // total exposure for the previous frame 
00049     
00050     FCam::Frame frame;
00051 
00052     do {
00053         // Ask the sensor to stream with the given parameters
00054         sensor.stream(stream1);
00055     
00056         // Retrieve a frame
00057         frame = sensor.getFrame();
00058         assert(frame.shot().id == stream1.id); // Check the source of the request
00059 
00060         printf("Exposure time: %d, gain: %f\n", frame.exposure(), frame.gain());
00061 
00062         // Calculate the total exposure used (including gain)
00063         exposure = frame.exposure() * frame.gain();
00064     
00065         // Call the autoexposure algorithm. It will update stream1
00066         // using this frame's histogram.
00067         autoExpose(&stream1, frame);
00068     
00069         // Call the auto white-balance algorithm. It will similarly
00070         // update the white balance using the histogram.
00071         autoWhiteBalance(&stream1, frame);
00072 
00073         // Increment stableCount if the exposure is within 5% of the
00074         // previous one
00075         if (fabs(exposure - lastExposure) < 0.05f * lastExposure) {
00076             stableCount++;
00077         } else {
00078             stableCount = 0;
00079         }
00080 
00081         // Update lastExposure
00082         lastExposure = exposure;
00083     
00084     } while (stableCount < 5); // Terminate when stable for 5 frames
00085 
00086     // Write out the well-exposed frame
00087     FCam::saveJPEG(frame, "/home/user/MyDocs/DCIM/example4.jpg");
00088     
00089     // Order the sensor to stop streaming
00090     sensor.stopStreaming();
00091     printf("Processed %d frames until stable for 5 frames!\n", count);
00092     
00093     // There may still be shots in the pipeline. Consume them.
00094     while (sensor.shotsPending() > 0) frame = sensor.getFrame();
00095 
00096     // Check that the pipeline is empty
00097     assert(sensor.framesPending() == 0);
00098     assert(sensor.shotsPending() == 0);
00099 }

Generated on Mon Aug 16 2010 14:25:45 for FCam by  doxygen 1.7.1