contrib/mul/vimt/vimt_image_2d.cxx
Go to the documentation of this file.
00001 //:
00002 //  \file
00003 //  \brief A base class for arbitrary 2D images+transform
00004 //  \author Tim Cootes
00005 
00006 #include "vimt_image_2d.h"
00007 #include <vgl/vgl_point_2d.h>
00008 #include <vgl/vgl_vector_2d.h>
00009 
00010 //: Return vector indicating size of image in pixels
00011 //  2D image is v[0] x v[1],  3D image is v[0] x v[1] x v[2]
00012 //  Somewhat inefficient: Only use when you have to
00013 vcl_vector<unsigned> vimt_image_2d::image_size() const
00014 {
00015   vcl_vector<unsigned> d(2);
00016   d[0]=image_base().ni();
00017   d[1]=image_base().nj();
00018   return d;
00019 }
00020 
00021 
00022 //: Return 2 element vector indicating the size of a pixel
00023 vcl_vector<double> vimt_image_2d::pixel_size() const
00024 {
00025   vcl_vector<double> d(2);
00026   vgl_vector_2d<double> v =world2im_.inverse().
00027     delta(vgl_point_2d<double>(0,0), vgl_vector_2d<double>(1.0,1.0));
00028 
00029   d[0] = v.x();
00030   d[1] = v.y();
00031   return d;
00032 }
00033 
00034 
00035 //: Return vectors defining bounding box containing image in world co-ords
00036 void vimt_image_2d::world_bounds(vcl_vector<double>& b_lo,
00037                                  vcl_vector<double>& b_hi) const
00038 {
00039   b_lo.resize(2); b_hi.resize(2);
00040   vgl_point_2d<double> p = world2im_.inverse()(0,0);
00041   b_lo[0]=p.x(); b_hi[0]=p.x();
00042   b_lo[1]=p.y(); b_hi[1]=p.y();
00043 
00044   // Compute each corner
00045   for (int i=0;i<2;++i)
00046     for (int j=0;j<2;++j)
00047     {
00048       p = world2im_.inverse()(i*(image_base().ni()-1),j*(image_base().nj()-1));
00049       if (p.x()<b_lo[0]) b_lo[0]=p.x();
00050       else if (p.x()>b_hi[0]) b_hi[0]=p.x();
00051       if (p.y()<b_lo[1]) b_lo[1]=p.y();
00052       else if (p.y()>b_hi[1]) b_hi[1]=p.y();
00053     }
00054 }
00055 
00056 
00057 // Related Functions
00058 
00059 // Return bounding box containing image in world co-ords as a box
00060 vgl_box_2d<double> world_bounding_box(const vimt_image_2d& img)
00061 {
00062   vcl_vector<double> b_lo(2,0.0);
00063   vcl_vector<double> b_hi(2,0.0);
00064   img.world_bounds(b_lo,b_hi);
00065   return vgl_box_2d<double>(b_lo[0],b_hi[0],b_lo[1],b_hi[1]);
00066 }
00067 
00068 // Translate the image so that its centre is at the origin of the world coordinate system.
00069 void vimt_centre_image_at_origin(vimt_image_2d& image)
00070 {
00071   vgl_box_2d<double> bbox = world_bounding_box(image);
00072   vgl_point_2d<double> c = bbox.centroid();
00073   vimt_transform_2d& w2i = image.world2im();
00074   w2i.set_origin(w2i(c));
00075 }
00076 
00077 // Calculate the pixel dimensions from the image transform
00078 // NEEDS A TEST PROGRAM
00079 vgl_vector_2d<double> vimt_pixel_size_from_transform(const vimt_image_2d& image)
00080 {
00081   const vimt_transform_2d& i2w = image.world2im().inverse();
00082   vgl_point_2d<double> p(0,0);
00083   vgl_vector_2d<double> i(1,0);
00084   vgl_vector_2d<double> j(0,1);
00085   double dx = i2w.delta(p, i).length();
00086   double dy = i2w.delta(p, j).length();
00087   return vgl_vector_2d<double>(dx, dy);
00088 }