LMDistanceImage.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
00026 #include "LMDistanceImage.h"
00027
00028 LMDistanceImage::LMDistanceImage()
00029 {
00030 dtImages_ = NULL;
00031 idtImages_ = NULL;
00032 }
00033
00034 LMDistanceImage::~LMDistanceImage()
00035 {
00036 SafeRelease();
00037 }
00038
00039
00040 void LMDistanceImage::SafeRelease()
00041 {
00042 if (dtImages_)
00043 {
00044 for(int i=0;i<nDirections_;i++)
00045 cvReleaseImage(&dtImages_[i]);
00046 delete [] dtImages_;
00047 }
00048 dtImages_ = NULL;
00049
00050 if (idtImages_)
00051 {
00052 for(int i=0;i<nDirections_;i++)
00053 idtImages_[i].SafeRelease();
00054
00055 delete [] idtImages_;
00056 }
00057 idtImages_ = NULL;
00058
00059 }
00060
00061 void LMDistanceImage::SetImage(EIEdgeImage& ei)
00062 {
00063 SafeRelease();
00064 width_ = ei.width_;
00065 height_ = ei.height_;
00066 nDirections_ = ei.nDirections_;
00067
00068 ConstructDTs(ei);
00069 UpdateCosts();
00070 ConstructDIntegrals();
00071 }
00072
00073 void LMDistanceImage::ConstructDTs(EIEdgeImage& ei)
00074 {
00075 IplImage *image=cvCreateImage(cvSize(width_,height_),IPL_DEPTH_8U,1);
00076 dtImages_ = new IplImage* [nDirections_];
00077 for (int i=0 ; i<ei.nDirections_ ; i++)
00078 {
00079 dtImages_[i] = cvCreateImage(cvSize(width_,height_),IPL_DEPTH_32F,1);
00080 ei.ConstructDirectionImage(i, image);
00081 cvDistTransform(image, dtImages_[i], CV_DIST_L2, 5);
00082
00083
00084
00085
00086 }
00087 cvReleaseImage(&image);
00088
00089 }
00090
00091
00092 void LMDistanceImage::UpdateCosts()
00093 {
00094 float* costs;
00095 costs = new float[nDirections_];
00096
00097 float **buffers = new float*[nDirections_];
00098 for (int i=0;i<nDirections_ ; i++)
00099 {
00100 buffers[i] = (float*) dtImages_[i]->imageData;
00101 }
00102
00103
00104 int wh = width_*height_;
00105 for (int k=0 ; k<wh ; k++)
00106 {
00107 for (int i=0 ; i<nDirections_ ; i++)
00108 {
00109 costs[i] = buffers[i][k];
00110 if (costs[i] > maxCost_)
00111 costs[i] = (float)maxCost_;
00112 }
00113
00114
00115 if (costs[0] > costs[nDirections_-1] + directionCost_)
00116 costs[0] = costs[nDirections_-1] + directionCost_;
00117 for (int i=1 ; i<nDirections_ ; i++)
00118 {
00119 if (costs[i] > costs[i-1] + directionCost_)
00120 costs[i] = costs[i-1] + directionCost_;
00121 }
00122
00123 if (costs[0] > costs[nDirections_-1] + directionCost_)
00124 costs[0] = costs[nDirections_-1] + directionCost_;
00125 for (int i=1 ; i<nDirections_ ; i++)
00126 {
00127 if (costs[i] > costs[i-1] + directionCost_)
00128 costs[i] = costs[i-1] + directionCost_;
00129 else
00130 break;
00131 }
00132
00133
00134 if (costs[nDirections_-1] > costs[0] + directionCost_)
00135 costs[nDirections_-1] = costs[0] + directionCost_;
00136 for (int i=nDirections_-1 ; i>0 ; i--)
00137 {
00138 if (costs[i-1] > costs[i] + directionCost_)
00139 costs[i-1] = costs[i] + directionCost_;
00140 }
00141
00142 if (costs[nDirections_-1] > costs[0] + directionCost_)
00143 costs[nDirections_-1] = costs[0] + directionCost_;
00144 for (int i=nDirections_-1 ; i>0 ; i--)
00145 {
00146 if (costs[i-1] > costs[i] + directionCost_)
00147 costs[i-1] = costs[i] + directionCost_;
00148 else
00149 break;
00150 }
00151
00152 for (int i=0 ; i<nDirections_ ; i++)
00153 {
00154 buffers[i][k] = costs[i];
00155 }
00156
00157 }
00158
00159 delete[] costs;
00160 delete[] buffers;
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 }
00174
00175
00176 void LMDistanceImage::ConstructDIntegrals()
00177 {
00178
00179 double theta;
00180
00181 printf("Directions %i\n", nDirections_);
00182 idtImages_ = new LMDirectionalIntegralDistanceImage [nDirections_];
00183
00184 for (int i=0 ; i<nDirections_ ; i++)
00185 {
00186
00187 theta = (i*M_PI)/nDirections_ + M_PI/(2*nDirections_);
00188
00189
00190 idtImages_[i].CreateImage(width_,height_);
00191 idtImages_[i].Construct(dtImages_[i], (float)cos(theta), (float)sin(theta));
00192 }
00193 }