Follow this link to skip to the main content

camera_image_io.h File Reference

#include "claraty/image_io_factory.h"
#include "claraty/camera_image.h"
#include "claraty/rgb_image.h"
#include "claraty/camera_model.h"
#include "claraty/lock_frame.h"
#include "claraty/string_util.h"
#include "claraty/parse_block.h"
#include "claraty/fdm_parse_tree.h"

Include dependency graph for camera_image_io.h:

Go to the source code of this file.

Functions

template<class T>
bool save_camera_image (const std::string &filename, const Camera_Image< T > &img, const Parse_Block *extraHeader=NULL, const RGB_Image< T > *color_img=NULL)
template<class T>
bool load_camera_image (const std::string &filename, Camera_Image< T > *&img, Framestore *&fs, Parse_Block *header=NULL)
template<class T>
bool load_stereo_camera_images (const std::string &leftFile, const std::string &rightFile, Camera_Image< T > *&leftImg, Camera_Image< T > *&rightImg, Framestore *&fs, Parse_Block *leftHdr=NULL, const std::string &refFrame="")


Detailed Description

A few, hopefully simple, functions for reading and writing camera images (including camera_models, and their framestores) into unknown file types, which is figured out using the factory mechanism.


Designer(s): Clay Kunz
Author(s): Clay Kunz
Date: March 20, 2003

Software License:
http://claraty.jpl.nasa.gov/license/open_src.html or file: ../share/license.txt

© 2007, Jet Propulsion Laboratory, California Institute of Technology
© 2007, NASA Ames Research Center

Revision
1.3

Definition in file camera_image_io.h.


Function Documentation

template<class T>
bool load_camera_image ( const std::string &  filename,
Camera_Image< T > *&  img,
Framestore *&  fs,
Parse_Block *  header = NULL 
)

Definition at line 117 of file camera_image_io.h.

References claraty::string_suffix(), and claraty::string_to_object().

Referenced by load_stereo_camera_images().

00121 {
00122   Image_IO_Factory *io =
00123     (*Image_IO_Factory::factory)(string_suffix(filename));
00124   if (io == NULL) {
00125     std::cerr << "Unable to find helper class for reading images of type "
00126               << string_suffix(filename) << std::endl;
00127     return false;
00128   }
00129 
00130   img = NULL;
00131   fs = NULL;
00132 
00133   unsigned char **rows;
00134   unsigned int nr, nc;
00135   std::string comments;
00136   int maxval;
00137   int bytes_per_pixel;
00138   bool color;
00139 
00140   if (!io->load_image_bytes(filename, rows, nr, nc, comments, maxval,
00141                             bytes_per_pixel, color)) {
00142     delete io;
00143     return false;
00144   }
00145   delete io;
00146 
00147   Parse_Block parseBlock;
00148   Parse_Block *pb = header != NULL ? header : &parseBlock;
00149 
00150   img = new Camera_Image<T>(nr, nc, NULL);
00151   img->set_maximum_value(maxval);
00152   Image_IO_Factory::move_raster_to_image(rows, bytes_per_pixel, *img, color);
00153 
00154   // get model_and_framestore, if it's there; otherwise just parse the thing
00155   // and yank out the pieces.
00156   std::string old_header = "model_and_framestore=";
00157   std::string::size_type pos = comments.find(old_header);
00158   if (pos != std::string::npos)
00159     comments = comments.substr(old_header.size());
00160 
00161   if (!pb->read_from_string(comments)) {
00162     std::cerr << "unable to parse image header" << std::endl;
00163     return false;
00164   }
00165   std::string value;
00166 
00167   if (!pb->get_entry("framestore", value)) {
00168     std::cerr << "missed framestore in image header" << std::endl;
00169     return false;
00170   }
00171 
00172   if (pb->check_for_entry("image_io_state_version")) {
00173     fs = new Framestore;
00174     bool ok = FDM_Parse_Tree::string_to_object(*fs, value);
00175     if (!ok) {
00176       std::cerr << "missed framestore (fdm representation expected) "
00177         "in image header" << std::endl;
00178       return false;
00179     }
00180   } else {
00181     Parse_Block fsPb(value);
00182     fs = new Framestore(fsPb);
00183   }
00184 
00185   if (!pb->get_entry("model", value)) {
00186     std::cerr << "missed model in image header" << std::endl;
00187     return false;
00188   }
00189 
00190   std::istringstream mis(value);
00191   Camera_Model *model = Camera_Model::build_from_stream(mis, *fs);
00192 
00193   if (model == NULL) {
00194     std::cerr << "unable to build model from stream" << std::endl;
00195     return false;
00196   } else {
00197     img->set_camera_model(model);
00198     delete model;
00199   }
00200   return true;
00201 }

Here is the call graph for this function:

template<class T>
bool load_stereo_camera_images ( const std::string &  leftFile,
const std::string &  rightFile,
Camera_Image< T > *&  leftImg,
Camera_Image< T > *&  rightImg,
Framestore *&  fs,
Parse_Block *  leftHdr = NULL,
const std::string &  refFrame = "" 
)

Definition at line 208 of file camera_image_io.h.

References load_camera_image().

00214 {
00215   Framestore *fs1;
00216   leftImg = NULL;
00217   rightImg = NULL;
00218   bool ok = load_camera_image(leftFile, leftImg, fs, leftHdr);
00219   ok &= load_camera_image(rightFile, rightImg, fs1);
00220 
00221   if (leftImg == NULL || rightImg == NULL || !ok)
00222     return false;
00223 
00224   // move the right camera into the left camera's framestore
00225   const Camera_Model& rightCM = rightImg->get_camera_model();
00226   Camera_Model *rightCopy = rightCM.clone();
00227   
00228   Frame_l rightFrame = rightCM.get_frame();
00229   Frame_l leftFrame = leftImg->get_camera_model().get_frame();
00230   if (rightFrame.get_name() == leftFrame.get_name() && refFrame != "") {
00231     // The two images have the same camera model frame - we have to be
00232     // careful in case they're in fact the same camera that's moved between
00233     // the two images, instead of two different cameras taking a pair of
00234     // pictures simultaneously.
00235     // In this case, the best we can do is to make a new frame for the
00236     // second camera, and put it in the correct location relative to the
00237     // given reference frame, which must be supplied if this is the case.
00238     Frame_l leftRef = fs->lookup(refFrame);
00239     Frame_l rightRef = fs1->lookup(refFrame);
00240     if (leftRef.is_empty() || rightRef.is_empty()) {
00241       delete rightCopy;
00242       delete fs1;
00243       delete leftImg;
00244       delete rightImg;
00245       delete fs;
00246       leftImg = rightImg = NULL;
00247       fs = NULL;
00248       return false;
00249     }
00250     Location leftCamLoc = leftRef.location_of(leftFrame);
00251     Location rightCamLoc = rightRef.location_of(rightFrame);
00252     if (leftCamLoc.get_translation() !=
00253         rightCamLoc.get_translation() ||
00254         leftCamLoc.get_rotation() !=
00255         rightCamLoc.get_rotation()) {
00256       // The camera models are the same, but in different places in the two
00257       // images. So we have to move the right one into a new frame.
00258       // If the two frames are in the same locations in the two images, then
00259       // there's nothing we can do, except assume that there is additional
00260       // extrinsic information in the camera model, which distinguishes the
00261       // two images.
00262       std::string newRtFrameName = rightFrame.get_name() + ".stereo.right";
00263       Frame_l newRightFrame =
00264         fs->add_frame(newRtFrameName.c_str(), &leftRef, rightCamLoc);
00265       rightCopy->set_frame(newRightFrame);
00266       rightImg->set_camera_model(rightCopy);
00267       delete rightCopy;
00268       delete fs1;
00269       return false;
00270     }
00271   }
00272   // Make sure that the frame information from the right store (regarding the
00273   // camera model, at least) is reflected in the model when we move it into
00274   // the left framestore.
00275   Frame_l lfsrparent = fs->lookup(rightFrame.get_parent().get_name());
00276   Location rcmloc = rightFrame.get_location();
00277   Frame_l lfsrframe = fs->lookup(rightFrame.get_name());
00278   lfsrframe.change_parent(lfsrparent);
00279   lfsrframe.set_location(rcmloc);
00280   rightCopy->set_frame(lfsrframe);
00281   rightImg->set_camera_model(rightCopy); // clones the model
00282 
00283   delete rightCopy;
00284   delete fs1;
00285   return true;
00286 }

Here is the call graph for this function: