00001 /* 00002 Copyright 2010, Ming-Yu Liu 00003 00004 All Rights Reserved 00005 00006 Permission to use, copy, modify, and distribute this software and 00007 its documentation for any non-commercial purpose is hereby granted 00008 without fee, provided that the above copyright notice appear in 00009 all copies and that both that copyright notice and this permission 00010 notice appear in supporting documentation, and that the name of 00011 the author not be used in advertising or publicity pertaining to 00012 distribution of the software without specific, written prior 00013 permission. 00014 00015 THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 00016 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00017 ANY PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 00018 ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 00019 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 00020 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 00021 OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00022 */ 00023 00024 00025 #include "LMDirectionalIntegralDistanceImage.h" 00026 //#include <opencv/highgui.h> 00027 #include <stdlib.h> 00028 #include <stdio.h> 00029 00030 LMDirectionalIntegralDistanceImage::LMDirectionalIntegralDistanceImage() 00031 { 00032 iimage_ = NULL; 00033 indices_ = NULL; 00034 } 00035 00036 LMDirectionalIntegralDistanceImage::~LMDirectionalIntegralDistanceImage() 00037 { 00038 SafeRelease(); 00039 } 00040 00041 void LMDirectionalIntegralDistanceImage::SafeRelease() 00042 { 00043 if(indices_) 00044 { 00045 delete [] indices_; 00046 } 00047 00048 indices_ = NULL; 00049 00050 if(iimage_) 00051 { 00052 cvReleaseImage(&iimage_); 00053 } 00054 00055 iimage_ = NULL; 00056 00057 } 00058 00059 void LMDirectionalIntegralDistanceImage::CreateImage(int width, int height) 00060 { 00061 width_ = width; 00062 height_ = height; 00063 iimage_ = cvCreateImage(cvSize(width+1,height+1),IPL_DEPTH_32F,1); 00064 } 00065 00066 00067 void LMDirectionalIntegralDistanceImage::Construct(IplImage *image, float dx, float dy) 00068 { 00069 if (fabs(dx) > fabs(dy)) 00070 { 00071 ds_ = dy / (dx + 1e-9f); 00072 xindexed_ = 1; 00073 } 00074 else 00075 { 00076 ds_ = dx / (dy + 1e-9f); 00077 xindexed_ = 0; 00078 } 00079 // Compute secant 00080 factor_ = sqrt(ds_*ds_ + 1); 00081 00082 ComputeIndices(); 00083 ComputeII(image); 00084 } 00085 00086 00087 void LMDirectionalIntegralDistanceImage::ComputeIndices() 00088 { 00089 00090 if (indices_) 00091 { 00092 delete[] indices_; 00093 } 00094 00095 00096 if (xindexed_) 00097 { 00098 indices_ = new int[width_]; 00099 indices_[0] = 0; 00100 00101 for (int i=0 ; i<width_;i++) 00102 { 00103 00104 indices_[i] = (int)ceil(i*ds_-0.5); 00105 //printf("I %i %i\n", i, indices_[i]); 00106 } 00107 } 00108 else 00109 { 00110 indices_ = new int[height_]; 00111 indices_[0] = 0; 00112 00113 for (int i=0 ; i<height_;i++) 00114 { 00115 indices_[i] = (int)ceil(i*ds_-0.5); 00116 //printf("I %i %i\n", i, indices_[i]); 00117 } 00118 } 00119 } 00120 00121 00122 void LMDirectionalIntegralDistanceImage::ComputeII(IplImage* image) 00123 { 00124 int x, y; 00125 00126 for (x = 0 ; x <= width_ ; x++) 00127 { 00128 cvSetReal2D(iimage_,0,x,0); 00129 } 00130 00131 for (y = 0 ; y <= height_ ; y++) 00132 { 00133 cvSetReal2D(iimage_,y,0,0); 00134 } 00135 00136 00137 if (xindexed_) 00138 { 00139 int miny, maxy; 00140 int py, cy; 00141 00142 if (indices_[width_-1]> 0 ) 00143 { 00144 miny = -indices_[width_-1]; 00145 maxy = height_; 00146 } 00147 else 00148 { 00149 miny = 0; 00150 maxy = height_-indices_[width_-1]; 00151 } 00152 00153 for (y=miny ; y<=maxy ; y++) 00154 { 00155 for (x=1 ; x<width_ ; x++) 00156 { 00157 py = y+indices_[x-1]; 00158 cy = y+indices_[x]; 00159 00160 //printf("x %i y %i py %i cy %i\n", x, y, py, cy); 00161 if (cy > 0 && cy < height_-1) 00162 { 00163 cvSetReal2D( iimage_, cy, x, 00164 cvGetReal2D(iimage_,py,x-1) + cvGetReal2D(image,cy,x) 00165 ); 00166 //printf("iimage_ cy %i x %i %f\n", cy, x, cvGetReal2D(iimage_,cy,x)); 00167 } 00168 } 00169 } 00170 } 00171 else 00172 { 00173 int minx, maxx; 00174 int px, cx; 00175 00176 if (indices_[height_-1]> 0 ) 00177 { 00178 minx = -indices_[height_-1]; 00179 maxx = width_; 00180 } 00181 else 00182 { 00183 minx = 0; 00184 maxx = width_-indices_[height_-1]; 00185 } 00186 00187 for (x=minx ; x<=maxx ; x++) 00188 { 00189 for (y=1 ; y<height_; y++) 00190 { 00191 px = x+indices_[y-1]; 00192 cx = x+indices_[y]; 00193 //printf("x %i y %i px %i cx %i\n", x, y, px, cx); 00194 if (cx > 0 && cx < width_-1) 00195 { 00196 00197 cvSetReal2D( iimage_, y, cx, 00198 cvGetReal2D(iimage_,y-1,px) + cvGetReal2D(image,y,cx) 00199 ); 00200 //printf("iimage_ y %i cx %i %f\n", y, cx, cvGetReal2D(iimage_,y,cx)); 00201 } 00202 } 00203 } 00204 00205 00206 } 00207 00208 }