• 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 out.
00061         float diopters = frame["lens.focus"];
00062         printf("Lens focused at %2.0f cm\n", 100/diopters);
00063 
00064         // The sensor has attached a sharpness map to each frame. 
00065         // Let's sum up all the values in it so we can print out 
00066         // the total sharpness of this frame.
00067         int totalSharpness = 0;
00068         for (int y = 0; y < frame.sharpness().height(); y++) {
00069             for (int x = 0; x < frame.sharpness().width(); x++) {
00070                 totalSharpness += frame.sharpness()(x, y);
00071             }
00072         }
00073         printf("Total sharpness is %d\n\n", totalSharpness);
00074 
00075         // Call the autofocus algorithm
00076         autoFocus.update(frame);
00077       
00078         // Increment frame counter
00079         count++;
00080     } while (!autoFocus.idle());
00081 
00082     printf("Autofocus chose to focus at %2.0f cm\n\n", 100/lens.getFocus());
00083 
00084     // Write out the focused frame
00085     FCam::saveJPEG(frame, "/home/user/MyDocs/DCIM/example5.jpg");
00086 
00087     // Order the sensor to stop streaming
00088     sensor.stopStreaming();
00089     printf("Processed %d frames until autofocus completed!\n", count);
00090 
00091     // There may still be shots in the pipeline. Consume them.
00092     while (sensor.shotsPending() > 0) frame = sensor.getFrame();
00093 
00094     // Check that the pipeline is empty
00095     assert(sensor.framesPending() == 0);
00096     assert(sensor.shotsPending() == 0);
00097 }

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