LMDirectionalIntegralDistanceImage.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "LMDirectionalIntegralDistanceImage.h"
00026
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
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
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
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
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
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
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
00201 }
00202 }
00203 }
00204
00205
00206 }
00207
00208 }