00001 /*!@file Image/calibrateFunctions.C [put description here] */ 00002 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003 // 00004 // by the University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // //////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: Dirk Walther <walther@caltech.edu> 00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/calibrateFunctions.C $ 00034 // $Id: calibrateFunctions.C 6182 2006-01-31 18:41:41Z rjpeters $ 00035 // 00036 00037 #include "Image/calibrateFunctions.H" 00038 00039 #include "Image/Image.H" 00040 #include "Util/Assert.H" 00041 #include <iostream> 00042 #include <vector> 00043 00044 double getError (const Image<byte>& img1, 00045 const Image<byte>& img2, 00046 const int shiftx, const int shifty) 00047 { 00048 ASSERT(img1.isSameSize(img2)); 00049 00050 int w = img1.getWidth(); 00051 int h = img1.getHeight(); 00052 00053 int xb1, xe1, xb2, xe2; 00054 if (shiftx < 0) 00055 { 00056 xb1 = 0; xe1 = w + shiftx; 00057 xb2 = -shiftx; xe2 = w; 00058 } 00059 else 00060 { 00061 xb1 = shiftx; xe1 = w; 00062 xb2 = 0; xe2 = w - shiftx; 00063 } 00064 ASSERT((xb1 < xe1) && (xb2 < xe2)); 00065 00066 int yb1, ye1, yb2, ye2; 00067 if (shifty < 0) 00068 { 00069 yb1 = 0; ye1 = h + shifty; 00070 yb2 = -shifty; ye2 = h; 00071 } 00072 else 00073 { 00074 yb1 = shifty; ye1 = h; 00075 yb2 = 0; ye2 = h - shifty; 00076 } 00077 ASSERT((yb1 < ye1) && (yb2 < ye2)); 00078 00079 double sum = 0.0, diff; 00080 Image<byte>::const_iterator ptr1 = img1.begin() + (xb1 + yb1 * w); 00081 Image<byte>::const_iterator ptr2 = img2.begin() + (xb2 + yb2 * w); 00082 00083 for (int y = yb1; y < ye1; y++) 00084 { 00085 Image<byte>::const_iterator ptr1b = ptr1; 00086 Image<byte>::const_iterator ptr2b = ptr2; 00087 for (int x = xb1; x < xe1; x++) 00088 { 00089 diff = double(*ptr1b++) - double(*ptr2b++); 00090 sum += diff * diff; 00091 } 00092 ptr1 += w; ptr2 += w; 00093 } 00094 00095 return sum / (xe1 - xb1) / (ye1 - yb1); 00096 } 00097 00098 00099 int findAlign(const Image<byte>& img1, 00100 const Image<byte>& img2, 00101 const int align_xy, 00102 const int range_start, 00103 const int range_end) 00104 { 00105 ASSERT((img1.isSameSize(img2))); 00106 ASSERT((align_xy == 0) || (align_xy == 1)); 00107 int dim[2], shift[2]; 00108 dim[0] = img1.getWidth(); 00109 dim[1] = img1.getHeight(); 00110 shift[0] = 0; shift[1] = 0; 00111 //std::cout << "range: " << range_start << " : " << range_end; 00112 //std::cout << " - dim[" << align_xy << "] = " << dim[align_xy] << "\n"; 00113 ASSERT((range_start < range_end) && (range_start > -dim[align_xy]) && 00114 (range_end < dim[align_xy])); 00115 00116 int num_elem = range_end - range_start; 00117 00118 std::vector<double> err(num_elem+1); 00119 for (int i=0; i<= num_elem; i++) 00120 err[i] = -1.0; 00121 00122 int left, right, middle, mid_l, mid_r; 00123 00124 left = 0; shift[align_xy] = left + range_start; 00125 err[left] = getError(img1,img2,shift[0],shift[1]); 00126 00127 right = num_elem; shift[align_xy] = right + range_start; 00128 err[right] = getError(img1,img2,shift[0],shift[1]); 00129 00130 middle = num_elem / 2; shift[align_xy] = middle + range_start; 00131 err[middle] = getError(img1,img2,shift[0],shift[1]); 00132 00133 while ((left != middle) && (middle != right)) 00134 { 00135 mid_l = (left + middle) / 2; 00136 if (err[mid_l] < 0) 00137 { 00138 shift[align_xy] = mid_l + range_start; 00139 err[mid_l] = getError(img1,img2,shift[0],shift[1]); 00140 } 00141 00142 mid_r = (middle + right) / 2; 00143 if (err[mid_r] < 0) 00144 { 00145 shift[align_xy] = mid_r + range_start; 00146 err[mid_r] = getError(img1,img2,shift[0],shift[1]); 00147 } 00148 if ((err[middle] < err[mid_l]) && (err[middle] < err[mid_r])) 00149 { 00150 left = mid_l; 00151 right = mid_r; 00152 } 00153 else if (err[mid_l] < err[mid_r]) 00154 { 00155 right = middle; 00156 middle = mid_l; 00157 } 00158 else 00159 { 00160 left = middle; 00161 middle = mid_r; 00162 } 00163 } 00164 return middle + range_start; 00165 } 00166 00167 // ###################################################################### 00168 /* So things look consistent in everyone's emacs... */ 00169 /* Local Variables: */ 00170 /* indent-tabs-mode: nil */ 00171 /* End: */