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

examples/example5/example5.cpp

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <assert.h>
00004 #include <FCam/N900.h>
00005 #include <FCam/AutoFocus.h>
00006 
00009 // Select the platform
00010 namespace Plat = FCam::N900;
00011 // namespace Plat = FCam::F2;
00012 
00013 /***********************************************************/
00014 /* Autofocus                                               */
00015 /*                                                         */
00016 /* This example shows how to request streams and deal with */
00017 /* the incoming frames, and also uses the provided         */
00018 /* autofocus routine.                                      */
00019 /***********************************************************/
00020 int main(int argc, char ** argv) {
00021 
00022     // Devices 
00023     Plat::Sensor sensor;
00024     Plat::Lens lens;
00025     sensor.attach(&lens); // Attach the lens to the sensor.
00026 
00027     // Autofocus supplied by FCam API 
00028     FCam::AutoFocus autoFocus(&lens);
00029 
00030     // Shot 
00031     FCam::Shot stream1;
00032     // Set the shot parameters.
00033     stream1.exposure = 50000;
00034     stream1.gain = 1.0f;
00035 
00036     // Request a resolution, and allocate storage. 
00037     stream1.image = FCam::Image(640, 480, FCam::UYVY);
00038 
00039     // Enable the sharpness unit.
00040     stream1.sharpness.enabled = true;
00041 
00042     // We will stream until the focus stabilizes.
00043     int count = 0;        // # of frames streamed
00044 
00045     // Order the sensor to stream.
00046     sensor.stream(stream1);
00047 
00048     // Ask the autofocus algorithm to start sweeping the lens.
00049     autoFocus.startSweep();
00050 
00051     // Stream until autofocus algorithm completes.
00052     FCam::Frame frame;
00053 
00054     do {
00055         // Retrieve a frame
00056         frame = sensor.getFrame();
00057         assert(frame.shot().id == stream1.id); // Check the source of the request.
00058       
00059         // The lens has tagged each frame with where it was focused
00060         // during that frame. Let's retrieve it so we can print it
00061         // out.
00062         float diopters = frame["lens.focus"];
00063         printf("Lens focused at %2.0f cm\n", 100/diopters);   
00064 
00065         // The sensor has attached a sharpness map to each
00066         // frame. Let's sum up all the values in it so we can print
00067         // out the total sharpness of this frame.
00068         int totalSharpness = 0;
00069         for (int y = 0; y < frame.sharpness().height(); y++) {
00070             for (int x = 0; x < frame.sharpness().width(); x++) {
00071                 totalSharpness += frame.sharpness()(x, y);
00072             }
00073         }
00074         printf("Total sharpness is %d\n\n", totalSharpness);
00075 
00076         // Call the autofocus algorithm.
00077         autoFocus.update(frame);
00078       
00079         // Increment frame counter.
00080         count++;
00081     } while (!autoFocus.idle());
00082 
00083     printf("Autofocus chose to focus at %2.0f cm\n\n", 100/lens.getFocus());
00084 
00085     // Write out the focused frame if desired.
00086     if (argc > 1) FCam::saveJPEG(frame, argv[1]);
00087 
00088     // Order the sensor to stop streaming.
00089     sensor.stopStreaming();
00090     printf("Processed %d frames until autofocus completed!\n", count);
00091 
00092     // There may still be shots in the pipeline. Consume them.
00093     while (sensor.shotsPending() > 0) frame = sensor.getFrame();
00094 
00095     // Check that the pipeline is empty.
00096     assert(sensor.framesPending() == 0);
00097     assert(sensor.shotsPending() == 0);
00098 }

Generated on Thu Jul 15 2010 17:51:28 for FCam by  doxygen 1.7.1