diff -p a/alg.c b/alg.c
*** a/alg.c	2007-10-11 22:23:21.000000000 +0200
--- b/alg.c	2007-10-11 22:23:21.000000000 +0200
*************** void alg_noise_tune(struct context *cnt,
*** 204,230 ****
  	unsigned char *smartmask=imgs->smartmask_final;
  
  	i=imgs->motionsize;
! 	
  	for (; i>0; i--) {
! 		diff = *ref - *new;
! 		
  		if (mask)
  			diff = ((diff * *mask++)/255);
- 
  		if (*smartmask){
! 			sum += ABS(diff) + 1;
  			count++;
  		}
- 
  		ref++;
  		new++;
  		smartmask++;
  	}
! 	
! 	if (count > 4) /* avoid divide by zero */
! 		sum /= count/4;
! 		
! 	cnt->noise = 4 + (cnt->noise+sum)/2;
  }
  
  void alg_threshold_tune(struct context *cnt, int diffs, int motion)
--- 204,226 ----
  	unsigned char *smartmask=imgs->smartmask_final;
  
  	i=imgs->motionsize;
! 			
  	for (; i>0; i--) {
! 		diff = ABS(*ref - *new);
  		if (mask)
  			diff = ((diff * *mask++)/255);
  		if (*smartmask){
! 			sum += diff + 1;
  			count++;
  		}
  		ref++;
  		new++;
  		smartmask++;
  	}
! 	if (count > 3) { /* avoid divide by zero */
! 		sum /= count / 3;
! 	}
! 	cnt->noise = 4 + (cnt->noise + sum) / 2;  /* 5: safe, 4: regular, 3: more sensitive */
  }
  
  void alg_threshold_tune(struct context *cnt, int diffs, int motion)
*************** int alg_diff_standard (struct context *c
*** 708,713 ****
--- 704,710 ----
  	int i, diffs=0;
  	int noise=cnt->noise;
  	int smartmask_speed=cnt->smartmask_speed;
+ 	register char detecting_motion = cnt->detecting_motion;
  	unsigned char *ref=imgs->ref;
  	unsigned char *out=imgs->out;
  	unsigned char *mask=imgs->mask;
*************** int alg_diff_standard (struct context *c
*** 857,870 ****
  			movq_r2r(mm3, mm0);              /* U */
  
  			/* Add to *smartmask_buffer. This is probably the fastest way to do it. */
! 			if (mmtemp.ub[0]) smartmask_buffer[0]+=SMARTMASK_SENSITIVITY_INCR;
! 			if (mmtemp.ub[1]) smartmask_buffer[1]+=SMARTMASK_SENSITIVITY_INCR;
! 			if (mmtemp.ub[2]) smartmask_buffer[2]+=SMARTMASK_SENSITIVITY_INCR;
! 			if (mmtemp.ub[3]) smartmask_buffer[3]+=SMARTMASK_SENSITIVITY_INCR;
! 			if (mmtemp.ub[4]) smartmask_buffer[4]+=SMARTMASK_SENSITIVITY_INCR;
! 			if (mmtemp.ub[5]) smartmask_buffer[5]+=SMARTMASK_SENSITIVITY_INCR;
! 			if (mmtemp.ub[6]) smartmask_buffer[6]+=SMARTMASK_SENSITIVITY_INCR;
! 			if (mmtemp.ub[7]) smartmask_buffer[7]+=SMARTMASK_SENSITIVITY_INCR;
  
  			smartmask_buffer+=8;
  			smartmask_final+=8;
--- 854,869 ----
  			movq_r2r(mm3, mm0);              /* U */
  
  			/* Add to *smartmask_buffer. This is probably the fastest way to do it. */
! 			if (!detecting_motion) {
! 				if (mmtemp.ub[0]) smartmask_buffer[0]+=SMARTMASK_SENSITIVITY_INCR;
! 				if (mmtemp.ub[1]) smartmask_buffer[1]+=SMARTMASK_SENSITIVITY_INCR;
! 				if (mmtemp.ub[2]) smartmask_buffer[2]+=SMARTMASK_SENSITIVITY_INCR;
! 				if (mmtemp.ub[3]) smartmask_buffer[3]+=SMARTMASK_SENSITIVITY_INCR;
! 				if (mmtemp.ub[4]) smartmask_buffer[4]+=SMARTMASK_SENSITIVITY_INCR;
! 				if (mmtemp.ub[5]) smartmask_buffer[5]+=SMARTMASK_SENSITIVITY_INCR;
! 				if (mmtemp.ub[6]) smartmask_buffer[6]+=SMARTMASK_SENSITIVITY_INCR;
! 				if (mmtemp.ub[7]) smartmask_buffer[7]+=SMARTMASK_SENSITIVITY_INCR;
! 			}
  
  			smartmask_buffer+=8;
  			smartmask_final+=8;
*************** int alg_diff_standard (struct context *c
*** 932,938 ****
  				   second. To be able to increase by 5 every second (with
  				   speed=10) we add 5 here. NOT related to the 5 at ratio-
  				   calculation. */
! 				(*smartmask_buffer) += SMARTMASK_SENSITIVITY_INCR;
  				/* apply smart_mask */
  				if (!*smartmask_final)
  					curdiff=0;
--- 931,938 ----
  				   second. To be able to increase by 5 every second (with
  				   speed=10) we add 5 here. NOT related to the 5 at ratio-
  				   calculation. */
! 				if (!detecting_motion)
! 					(*smartmask_buffer) += SMARTMASK_SENSITIVITY_INCR;
  				/* apply smart_mask */
  				if (!*smartmask_final)
  					curdiff=0;
*************** int alg_switchfilter(struct context *cnt
*** 1048,1050 ****
--- 1048,1119 ----
  	}
  	return 0;
  }
+ 
+ /** 
+  * alg_update_reference_frame
+  *
+  *   Called from 'motion_loop' to calculate the reference frame
+  *   Moving objects are excluded from the reference frame for a certain
+  *   amount of time to improve detection.
+  * 
+  * Parameters:
+  *
+  *   cnt    - current thread's context struct
+  *   action - UPDATE_REF_FRAME or RESET_REF_FRAME
+  *
+  */
+ /* Seconds */
+ #define ACCEPT_STATIC_OBJECT_TIME 5
+ #define DISCARD_STATIC_OBJECT_TIME 60
+ #define BLOCK_PIXEL_DURATION 1
+ #define EXCLUDE_LEVEL_PERCENT 40
+ void alg_update_reference_frame(struct context *cnt, int action) 
+ {
+ //	int accept_timer = cnt->lastrate * ACCEPT_STATIC_OBJECT_TIME;
+ //	int discard_timer = cnt->lastrate * (-DISCARD_STATIC_OBJECT_TIME);
+ 	int block_timer = cnt->lastrate * (-BLOCK_PIXEL_DURATION);
+ 	int accept_timer = cnt->lastrate * cnt->conf.in_timer;
+ 	int discard_timer = cnt->lastrate * (-cnt->conf.out_timer);
+ 	int i, threshold_ref;
+ 	int *ref_dyn = cnt->imgs.ref_dyn;
+ 	unsigned char *image_virgin = cnt->imgs.image_virgin;
+ 	unsigned char *ref = cnt->imgs.ref;
+ 	unsigned char *smartmask = cnt->imgs.smartmask_final;
+ 
+ 	if (action == UPDATE_REF_FRAME) { /* black&white only for better performance */
+ //		threshold_ref = cnt->noise * EXCLUDE_LEVEL_PERCENT / 100;
+ 		threshold_ref = cnt->noise * cnt->conf.correction_factor / 100;
+ 		for (i = cnt->imgs.motionsize; i > 0; i--) {
+ 			/* exclude pixels from ref frame well below noise level */
+ 			if (((int)(abs(*ref - *image_virgin)) > threshold_ref) && (*smartmask)) {
+ 				if (*ref_dyn < 0) { /* Static Object moves again? */
+ 					*ref = *image_virgin;
+ 					if (*ref_dyn < block_timer) /* block pixel for a while */
+ 						*ref_dyn = 0;
+ 					else
+ 						(*ref_dyn)--;
+ 				}
+ 				else if (*ref_dyn > accept_timer) { /* Include static Object after some time */
+ 					*ref_dyn = -1;
+ 					*ref = *image_virgin;
+ 				} else {
+ 					(*ref_dyn)++; /* Motionpixel? Exclude from ref frame */
+ 				}
+ 			}
+ 			else {  /* No motion: copy to ref frame */
+ 				*ref = *image_virgin;
+ 				if ((*ref_dyn >= 0) || (*ref_dyn == discard_timer)) /* Discard static object again after a while */
+ 					*ref_dyn = 0;
+ 				else
+ 					(*ref_dyn)--; /* Still keep static object in mind */
+ 			}
+ 			ref++;
+ 			image_virgin++;
+ 			smartmask++;
+ 			ref_dyn++;
+ 		} /* end for i */
+ 	} else {   /* action == RESET_REF_FRAME - also used to initialize the frame at startup */
+ 		memcpy(cnt->imgs.ref, cnt->imgs.image_virgin, cnt->imgs.size); /* copy fresh image */
+ 		memset(cnt->imgs.ref_dyn, 0, cnt->imgs.motionsize * sizeof(cnt->imgs.ref_dyn));  /* reset static objects */
+ 	}
+ }
Nur in b: alg.c.latest.c.
diff -p a/alg.h b/alg.h
*** a/alg.h	2007-10-11 22:23:21.000000000 +0200
--- b/alg.h	2007-10-11 22:23:21.000000000 +0200
*************** void alg_noise_tune(struct context *, un
*** 41,45 ****
--- 41,46 ----
  void alg_threshold_tune(struct context *, int, int);
  int alg_despeckle(struct context *, int);
  void alg_tune_smartmask(struct context *);
+ void alg_update_reference_frame(struct context *, int);
  
  #endif /* _INCLUDE_ALG_H */
diff -p a/conf.c b/conf.c
*** a/conf.c	2007-10-11 22:23:21.000000000 +0200
--- b/conf.c	2007-10-11 22:23:21.000000000 +0200
*************** struct config conf_template = {
*** 134,139 ****
--- 134,142 ----
  	minimum_motion_frames: 1,
  	pid_file:              NULL,
  	// debug_parameter:       0
+ 	correction_factor:     40,
+ 	in_timer:              5,
+ 	out_timer:             60,
  };
  
  
*************** config_param config_params[] = {
*** 1174,1180 ****
  	config_thread,
  	print_thread
  	},
! 	{ NULL, NULL, 0 , NULL, NULL }
  };
  
  /* conf_cmdline sets the conf struct options as defined by the command line.
--- 1177,1204 ----
  	config_thread,
  	print_thread
  	},
! 	{
! 	"correction_factor",
! 	"#",
! 	CONF_OFFSET(correction_factor),
! 	copy_int,
! 	print_int
! 	},
! 	{
! 	"in_timer",
! 	"#",
! 	CONF_OFFSET(in_timer),
! 	copy_int,
! 	print_int
! 	},
! 	{
! 	"out_timer",
! 	"#",
! 	CONF_OFFSET(out_timer),
! 	copy_int,
! 	print_int
! 	},
! { NULL, NULL, 0 , NULL, NULL }
  };
  
  /* conf_cmdline sets the conf struct options as defined by the command line.
diff -p a/conf.h b/conf.h
*** a/conf.h	2007-10-11 22:23:21.000000000 +0200
--- b/conf.h	2007-10-11 22:23:21.000000000 +0200
*************** struct config {
*** 120,125 ****
--- 120,128 ----
  	// int debug_parameter;
  	int argc;
  	char **argv;
+ 	int correction_factor;
+ 	int in_timer;
+ 	int out_timer;
  };
  
  /** 
diff -p a/motion.c b/motion.c
*** a/motion.c	2007-10-11 22:23:21.000000000 +0200
--- b/motion.c	2007-10-11 22:23:21.000000000 +0200
*************** static void context_destroy(struct conte
*** 239,244 ****
--- 239,246 ----
  		free(cnt->imgs.out);
  	if (cnt->imgs.ref)
  		free(cnt->imgs.ref);
+ 	if (cnt->imgs.ref_dyn)
+ 		free(cnt->imgs.ref_dyn);
  	if (cnt->imgs.image_virgin)
  		free(cnt->imgs.image_virgin);
  	if (cnt->imgs.labels)
*************** static void motion_init(struct context *
*** 592,597 ****
--- 594,600 ----
  	 * that certain code below does not run until motion has been detected the first time */
  	cnt->event_nr = 1;
  	cnt->prev_event = 0;
+ 	cnt->detecting_motion = 0;
  
  	motion_log(LOG_DEBUG, 0, "Thread %d started", (unsigned long)pthread_getspecific(tls_key_threadnr));
  
*************** static void motion_init(struct context *
*** 629,641 ****
  
  	cnt->imgs.ref = mymalloc(cnt->imgs.size);
  	cnt->imgs.out = mymalloc(cnt->imgs.size);
  	cnt->imgs.image_virgin = mymalloc(cnt->imgs.size);
  	memset(cnt->imgs.image_virgin, 0x80, cnt->imgs.size);       /* initialize to grey */
  	cnt->imgs.smartmask = mymalloc(cnt->imgs.motionsize);
  	cnt->imgs.smartmask_final = mymalloc(cnt->imgs.motionsize);
  	cnt->imgs.smartmask_buffer = mymalloc(cnt->imgs.motionsize * sizeof(int));
! 	cnt->imgs.labels = mymalloc(cnt->imgs.motionsize * sizeof(cnt->imgs.labels));
! 	cnt->imgs.labelsize = mymalloc((cnt->imgs.motionsize/2+1) * sizeof(cnt->imgs.labelsize));
  
  	/* allocate buffer here for preview buffer */
  	cnt->imgs.preview_image.image = mymalloc(cnt->imgs.size);
--- 632,645 ----
  
  	cnt->imgs.ref = mymalloc(cnt->imgs.size);
  	cnt->imgs.out = mymalloc(cnt->imgs.size);
+ 	cnt->imgs.ref_dyn = mymalloc(cnt->imgs.motionsize * sizeof(int));  /* contains the moving objects of ref. frame */
  	cnt->imgs.image_virgin = mymalloc(cnt->imgs.size);
  	memset(cnt->imgs.image_virgin, 0x80, cnt->imgs.size);       /* initialize to grey */
  	cnt->imgs.smartmask = mymalloc(cnt->imgs.motionsize);
  	cnt->imgs.smartmask_final = mymalloc(cnt->imgs.motionsize);
  	cnt->imgs.smartmask_buffer = mymalloc(cnt->imgs.motionsize * sizeof(int));
! 	cnt->imgs.labels = mymalloc(cnt->imgs.motionsize * sizeof(int));
! 	cnt->imgs.labelsize = mymalloc((cnt->imgs.motionsize/2+1) * sizeof(int));
  
  	/* allocate buffer here for preview buffer */
  	cnt->imgs.preview_image.image = mymalloc(cnt->imgs.size);
*************** static void motion_init(struct context *
*** 675,681 ****
  	}
  
  	/* create a reference frame */
! 	memcpy(cnt->imgs.ref, cnt->imgs.image_virgin, cnt->imgs.size);
  
  #ifndef WITHOUT_V4L
  #if (!defined(BSD))
--- 679,685 ----
  	}
  
  	/* create a reference frame */
! 	alg_update_reference_frame(cnt, RESET_REF_FRAME);
  
  #ifndef WITHOUT_V4L
  #if (!defined(BSD))
*************** static void motion_init(struct context *
*** 820,832 ****
  static void *motion_loop(void *arg)
  {
  	struct context *cnt = arg;
! 	int i, j, detecting_motion = 0;
  	time_t lastframetime = 0;
  	int frame_buffer_size;
  	int smartmask_ratio = 0;
  	int smartmask_count = 20;
  	int smartmask_lastrate = 0;
  	int olddiffs = 0;
  	int text_size_factor;
  	int passflag = 0;
  	long int *rolling_average_data;
--- 824,837 ----
  static void *motion_loop(void *arg)
  {
  	struct context *cnt = arg;
! 	int i, j = 0;
  	time_t lastframetime = 0;
  	int frame_buffer_size;
  	int smartmask_ratio = 0;
  	int smartmask_count = 20;
  	int smartmask_lastrate = 0;
  	int olddiffs = 0;
+ 	int previous_diffs = 0;
  	int text_size_factor;
  	int passflag = 0;
  	long int *rolling_average_data;
*************** static void *motion_loop(void *arg)
*** 1120,1126 ****
  				 * motion, the alg_diff will trigger alg_diff_standard
  				 * anyway
  				 */
! 				if (detecting_motion || cnt->conf.setup_mode)
  					cnt->current_image->diffs = alg_diff_standard(cnt, cnt->imgs.image_virgin);
  				else
  					cnt->current_image->diffs = alg_diff(cnt, cnt->imgs.image_virgin);
--- 1125,1131 ----
  				 * motion, the alg_diff will trigger alg_diff_standard
  				 * anyway
  				 */
! 				if (cnt->detecting_motion || cnt->conf.setup_mode)
  					cnt->current_image->diffs = alg_diff_standard(cnt, cnt->imgs.image_virgin);
  				else
  					cnt->current_image->diffs = alg_diff(cnt, cnt->imgs.image_virgin);
*************** static void *motion_loop(void *arg)
*** 1137,1142 ****
--- 1142,1148 ----
  						if (cnt->moved < 5)
  							cnt->moved = 5;
  						cnt->current_image->diffs = 0;
+ 						alg_update_reference_frame(cnt, RESET_REF_FRAME);
  					}
  				}
  
*************** static void *motion_loop(void *arg)
*** 1176,1182 ****
  				cnt->current_image->diffs = 0;
  
  			/* Manipulate smart_mask sensitivity (only every smartmask_ratio seconds) */
! 			if (cnt->smartmask_speed) {
  				if (!--smartmask_count){
  					alg_tune_smartmask(cnt);
  					smartmask_count = smartmask_ratio;
--- 1182,1188 ----
  				cnt->current_image->diffs = 0;
  
  			/* Manipulate smart_mask sensitivity (only every smartmask_ratio seconds) */
! 			if (cnt->smartmask_speed && !cnt->detecting_motion) {
  				if (!--smartmask_count){
  					alg_tune_smartmask(cnt);
  					smartmask_count = smartmask_ratio;
*************** static void *motion_loop(void *arg)
*** 1202,1208 ****
  			 * no frames have been recorded and only once per second
  			 */
  			if (cnt->conf.noise_tune && cnt->shots == 0) {
! 				if (!detecting_motion && (cnt->current_image->diffs <= cnt->threshold))
  					alg_noise_tune(cnt, cnt->imgs.image_virgin);
  			}
  
--- 1208,1214 ----
  			 * no frames have been recorded and only once per second
  			 */
  			if (cnt->conf.noise_tune && cnt->shots == 0) {
! 				if (!cnt->detecting_motion && (cnt->current_image->diffs <= cnt->threshold))
  					alg_noise_tune(cnt, cnt->imgs.image_virgin);
  			}
  
*************** static void *motion_loop(void *arg)
*** 1217,1226 ****
  			 * changes of threshold are used.
  			 */
  			if (cnt->conf.threshold_tune)
! 				alg_threshold_tune(cnt, cnt->current_image->diffs, detecting_motion);
  			else
  				cnt->threshold = cnt->conf.max_changes;
  
  
  		/***** MOTION LOOP - TEXT AND GRAPHICS OVERLAY SECTION *****/
  
--- 1223,1244 ----
  			 * changes of threshold are used.
  			 */
  			if (cnt->conf.threshold_tune)
! 				alg_threshold_tune(cnt, cnt->current_image->diffs, cnt->detecting_motion);
  			else
  				cnt->threshold = cnt->conf.max_changes;
  
+ 			/* Update reference frame.                                               *
+ 			 * micro-lighswitch: e.g. neighbors cat switched on the motion sensitive *
+ 			 * frontdoor illumination.                                               */
+ 			//jw if (abs(previous_diffs - cnt->current_image->diffs)) > (previous_diffs / 20)
+ 			if (1)
+ 				alg_update_reference_frame(cnt, UPDATE_REF_FRAME);
+ 			else {
+ 				alg_update_reference_frame(cnt, RESET_REF_FRAME);
+ 				printf("micro-lightswitch!\n");
+ 			}
+ 			previous_diffs = cnt->current_image->diffs;
+ 				
  
  		/***** MOTION LOOP - TEXT AND GRAPHICS OVERLAY SECTION *****/
  
*************** static void *motion_loop(void *arg)
*** 1307,1313 ****
  			 * code section.
  			 */
  			if (cnt->conf.output_all) {
! 				detecting_motion = 1;
  				cnt->current_image->flags |= IMAGE_SAVE;
  			} else if (cnt->current_image->diffs > cnt->threshold) {
  				/* flag this image, it have motion */
--- 1325,1331 ----
  			 * code section.
  			 */
  			if (cnt->conf.output_all) {
! 				cnt->detecting_motion = 1;
  				cnt->current_image->flags |= IMAGE_SAVE;
  			} else if (cnt->current_image->diffs > cnt->threshold) {
  				/* flag this image, it have motion */
*************** static void *motion_loop(void *arg)
*** 1333,1339 ****
  
  				if (frame_count >= cnt->conf.minimum_motion_frames) {
  					cnt->current_image->flags |= (IMAGE_TRIGGER | IMAGE_SAVE);
! 					detecting_motion = 1;
  					/* Mark all images in image_ring to be saved */
  					for(i = 0; i < cnt->imgs.image_ring_size; i++) {
  						cnt->imgs.image_ring[i].flags |= IMAGE_SAVE;
--- 1351,1357 ----
  
  				if (frame_count >= cnt->conf.minimum_motion_frames) {
  					cnt->current_image->flags |= (IMAGE_TRIGGER | IMAGE_SAVE);
! 					cnt->detecting_motion = 1;
  					/* Mark all images in image_ring to be saved */
  					for(i = 0; i < cnt->imgs.image_ring_size; i++) {
  						cnt->imgs.image_ring[i].flags |= IMAGE_SAVE;
*************** static void *motion_loop(void *arg)
*** 1353,1359 ****
  				cnt->postcap--;
  			} else {
  				cnt->current_image->flags |= IMAGE_PRECAP;
! 				detecting_motion = 0;
  			}
  
  			/* Is the mpeg movie to long? Then make movies
--- 1371,1377 ----
  				cnt->postcap--;
  			} else {
  				cnt->current_image->flags |= IMAGE_PRECAP;
! 				cnt->detecting_motion = 0;
  			}
  
  			/* Is the mpeg movie to long? Then make movies
*************** static void *motion_loop(void *arg)
*** 1404,1434 ****
  			/* Save/send to movie some images */
  			process_image_ring(cnt, 2);
  
- 		/***** MOTION LOOP - REFERENCE FRAME SECTION *****/
- 
- 			/* Update reference frame */
- 			if ((cnt->current_image->diffs > cnt->threshold * 2) ||
- 			    (cnt->moved && (cnt->track.type || cnt->conf.lightswitch))) {
- 				/* Prevent the motion created by moving camera or sudden light intensity
- 				 * being detected by creating a fresh reference frame. Decaying is also
- 				 * disabled when motion is above a certain threshold to make tracking
- 				 * more accurate.
- 				 */
- 				memcpy(cnt->imgs.ref, cnt->imgs.image_virgin, cnt->imgs.size);
- 			} else if (cnt->threshold) {
- 				/* Old image slowly decays, this will make it even harder on
- 			 	 * a slow moving object to stay undetected
- 			 	 */
- 				unsigned char *imgs_ref_ptr = cnt->imgs.ref;
- 				unsigned char *newimg_ptr = cnt->imgs.image_virgin;
- 				for (i=cnt->imgs.size-1; i>=0; i--) {
- 					*imgs_ref_ptr = (*imgs_ref_ptr + *newimg_ptr)/2;
- 					imgs_ref_ptr++;
- 					newimg_ptr++;
- 				}
- 			}
- 
- 
  		/***** MOTION LOOP - SETUP MODE CONSOLE OUTPUT SECTION *****/
  
  			/* If setup_mode enabled output some numbers to console */
--- 1422,1427 ----
*************** static void *motion_loop(void *arg)
*** 1546,1551 ****
--- 1539,1545 ----
  			if (cnt->shots == 0 &&
  				time_current_frame % cnt->conf.timelapse <= time_last_frame % cnt->conf.timelapse)
  				event(cnt, EVENT_TIMELAPSE, cnt->current_image->image, NULL, NULL, &cnt->current_image->timestamp_tm);
+ 				//jw event(cnt, EVENT_TIMELAPSE, cnt->imgs.ref, NULL, NULL, &cnt->current_image->timestamp_tm);
  		}
  
  		/* if timelapse mpeg is in progress but conf.timelapse is zero then close timelapse file
*************** static void *motion_loop(void *arg)
*** 1578,1584 ****
  		} else {
  			event(cnt, EVENT_IMAGE, cnt->current_image->image, NULL, &cnt->pipe, &cnt->current_image->timestamp_tm);
  			if (!cnt->conf.webcam_motion || cnt->shots == 1)
! 				event(cnt, EVENT_WEBCAM, cnt->current_image->image, NULL, NULL, &cnt->current_image->timestamp_tm);
  		}
  
  		event(cnt, EVENT_IMAGEM, cnt->imgs.out, NULL, &cnt->mpipe, cnt->currenttime_tm);
--- 1572,1579 ----
  		} else {
  			event(cnt, EVENT_IMAGE, cnt->current_image->image, NULL, &cnt->pipe, &cnt->current_image->timestamp_tm);
  			if (!cnt->conf.webcam_motion || cnt->shots == 1)
! 				//jw event(cnt, EVENT_WEBCAM, cnt->current_image->image, NULL, NULL, &cnt->current_image->timestamp_tm);
! 				event(cnt, EVENT_WEBCAM, cnt->imgs.ref, NULL, NULL, &cnt->current_image->timestamp_tm);
  		}
  
  		event(cnt, EVENT_IMAGEM, cnt->imgs.out, NULL, &cnt->mpipe, cnt->currenttime_tm);
diff -p a/motion.h b/motion.h
*** a/motion.h	2007-10-11 22:23:21.000000000 +0200
--- b/motion.h	2007-10-11 22:23:21.000000000 +0200
***************
*** 172,177 ****
--- 172,180 ----
  #define LOCATE_NORMAL    0
  #define LOCATE_BOTH      1
  
+ #define UPDATE_REF_FRAME 1
+ #define RESET_REF_FRAME  2
+ 
  /* Forward declaration, used in track.h */
  struct images;
  
*************** struct images {
*** 247,252 ****
--- 250,256 ----
  
  	unsigned char *ref;               /* The reference frame */
  	unsigned char *out;               /* Picture buffer for motion images */
+ 	int *ref_dyn;                    /* Dynamic objects to be excluded from reference frame */
  	unsigned char *image_virgin;      /* Last picture frame with no text or locate overlay */
  	struct image_data preview_image;  /* Picture buffer for best image when enables */
  	unsigned char *mask;              /* Buffer for the mask file */
*************** struct context {
*** 320,325 ****
--- 324,330 ----
  	int postcap;		/* downcounter, frames left to to send post event */
  
  	int shots;
+ 	int detecting_motion;
  	struct tm *currenttime_tm;
  	struct tm *eventtime_tm;
  
