Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef BACKGROUND_SUBTRACTOR_H
00017 #define BACKGROUND_SUBTRACTOR_H
00018
00019 #include "cxcore.h"
00020 #include "cv.h"
00021 #include "index.h"
00022 #include "options.h"
00023
00027 class background_subtractor
00028 {
00029 public:
00030 background_subtractor ()
00031 {
00032 im = NULL;
00033 background = cvLoadImage ("Background_1.png", CV_LOAD_IMAGE_GRAYSCALE);
00034
00035 size = cvGetSize (background);
00036
00037 bw = cvCreateImage (size, IPL_DEPTH_8U, 1);
00038 tmp_im = cvCreateImage (size, IPL_DEPTH_8U, 1);
00039 dist = cvCreateImage (size, IPL_DEPTH_32F, 1);
00040 }
00041
00042 ~background_subtractor ()
00043 {
00044 if (im != NULL)
00045 cvReleaseImage (&im);
00046 if (background != NULL)
00047 cvReleaseImage (&background);
00048 if (bw != NULL)
00049 cvReleaseImage (&bw);
00050 if (tmp_im != NULL)
00051 cvReleaseImage (&tmp_im);
00052 if (dist != NULL)
00053 cvReleaseImage (&dist);
00054 }
00055
00056 bool refresh ()
00057 {
00058 bool retval = true;
00059
00060
00061 if (im != NULL)
00062 cvReleaseImage (&im);
00063 im = cvLoadImage ("image_1_001.png", CV_LOAD_IMAGE_GRAYSCALE);
00064
00065 if (im == NULL)
00066 retval = false;
00067 else
00068 {
00069
00070 cvAbsDiff (im, background, bw);
00071 cvThreshold (bw, tmp_im, 10, 255, CV_THRESH_BINARY);
00072
00073 CvMemStorage *storage = cvCreateMemStorage (0);
00074 CvSeq *seq;
00075 cvFindContours (tmp_im, storage, &seq, sizeof (CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
00076
00077 const CvScalar white = CV_RGB (255, 255, 255);
00078
00079 cvZero (bw);
00080 for(CvSeq *c = seq; c != NULL; c = c->h_next)
00081 {
00082 if (c->total > 500)
00083 cvDrawContours (bw, c, white, white, -1, CV_FILLED, 8);
00084 }
00085
00086 cvReleaseMemStorage(&storage);
00087
00088
00089
00090
00091
00092 cvNot (bw, bw);
00093 cvDistTransform (bw, dist);
00094 }
00095
00096 return retval;
00097 }
00098
00099 CvSize get_size () const
00100 {
00101 return size;
00102 }
00103
00104 IplImage* get_segmentation ()
00105 {
00106 return bw;
00107 }
00108
00109 CvPoint project_into_segment (const CvPoint p) const
00110 {
00111 CvPoint retval;
00112
00113
00114 const float val = GRAY32F (dist, p.x, p.y);
00115 if (val > 0)
00116 {
00117 const float dx = 0.5 * (GRAY32F (dist, p.x + 1, p.y) - GRAY32F (dist, p.x - 1, p.y));
00118 const float dy = 0.5 * (GRAY32F (dist, p.x, p.y + 1) - GRAY32F (dist, p.x, p.y - 1));
00119 const float norm = sqrt (dx*dx + dy*dy);
00120 retval.x = p.x - dx * val / norm;
00121 retval.y = p.y - dy * val / norm;
00122 }
00123 else
00124 {
00125 retval.x = p.x;
00126 retval.y = p.y;
00127 }
00128
00129 return retval;
00130 }
00131
00132 private:
00133 IplImage *im, *background;
00134 IplImage *bw, *dist, *tmp_im;
00135 CvSize size;
00136 };
00137
00138 #endif // BACKGROUND_SUBTRACTOR_H
00139