Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef PROJECT2D
00017 #define PROJECT2D
00018
00019 #include "opencv_helper.h"
00020 #include "auxil.h"
00021
00022
00023 #define PROJ2D_SCALE 1.0
00024
00032 class project2d
00033 {
00034 public:
00041 project2d ()
00042 : dist (NULL)
00043 {
00044 size = cvSize (0, 0);
00045 }
00046
00055 void update_segment (IplImage *new_segment)
00056 {
00057
00058 const CvSize new_size = cvGetSize (new_segment);
00059 if (size.width != new_size.width || size.height != new_size.height)
00060 {
00061 if (dist != NULL)
00062 cvReleaseImage (&dist);
00063 dist = cvCreateImage (new_size, IPL_DEPTH_32F, 1);
00064 size = new_size;
00065 }
00066
00067
00068 cvNot (new_segment, new_segment);
00069 cvDistTransform (new_segment, dist);
00070 }
00071
00083 CvPoint project (CvPoint p) const
00084 {
00085 CvPoint retval;
00086
00087
00088 p.x = force_in_range (p.x, 0, size.width-1);
00089 p.y = force_in_range (p.y, 0, size.height-1);
00090
00091
00092 const float val = GRAY32F (dist, p.x, p.y);
00093 if (val > 0)
00094 {
00095 const float dx = 0.5 * (GRAY32F (dist, p.x + 1, p.y) - GRAY32F (dist, p.x - 1, p.y));
00096 const float dy = 0.5 * (GRAY32F (dist, p.x, p.y + 1) - GRAY32F (dist, p.x, p.y - 1));
00097 const float norm = sqrt (dx*dx + dy*dy);
00098 retval.x = p.x - dx * val / norm;
00099 retval.y = p.y - dy * val / norm;
00100 }
00101 else
00102 {
00103 retval.x = p.x;
00104 retval.y = p.y;
00105 }
00106
00107 return retval;
00108 }
00109
00119 bool is_inside (CvPoint p) const
00120 {
00121
00122 p.x = force_in_range (p.x, 0, size.width-1);
00123 p.y = force_in_range (p.y, 0, size.height-1);
00124
00125
00126 const float val = GRAY32F (dist, p.x, p.y);
00127 return (val <= 0.0);
00128 }
00129
00136 CvSize get_size () const
00137 {
00138 return size;
00139 }
00140
00141 private:
00142 IplImage *dist;
00143 CvSize size;
00144 };
00145
00146 #endif // PROJECT2D