// Below is the copyright agreement for the Ptolemy II system.
//
// Copyright (c) 2015-2016 The Regents of the University of California.
// All rights reserved.
//
// Permission is hereby granted, without written agreement and without
// license or royalty fees, to use, copy, modify, and distribute this
// software and its documentation for any purpose, provided that the above
// copyright notice and the following two paragraphs appear in all copies
// of this software.
//
// IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
// ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
// THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
// THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
// PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
// CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
// ENHANCEMENTS, OR MODIFICATIONS.
//
//
// Ptolemy II includes the work of others, to see those copyrights, follow
// the copyright link on the splash page or see copyright.htm.
/** Module to detect motion in a sequence of images.'
* This module provides an interface to a motion detection implementation
* taken from the webcam-capture package by Bartosz Firyn (SarXos), available from:
* [https://github.com/sarxos/webcam-capture](https://github.com/sarxos/webcam-capture).
* The webcam-capture package is licensed under the MIT License.
*
* The filter architecture follows the pattern defined by Jerry Huxtable
* in the JH Labs Java Image Processing library, available from:
* http://www.jhlabs.com/ip/filters
* and licensed under the Apache License, Version 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* @module motionDetector
* @author Edward A. Lee
* @version $$Id$$
*/
// Stop extra messages from jslint. Note that there should be no
// space between the / and the * and global.
/*globals exports, Java */
/*jshint globalstrict: true */
"use strict";
////////////////////////////////////////////////////////////
//// Private variables.
var Filter = Java.type('com.jhlabs.image.MotionDetectorFilter');
var filter = new Filter();
////////////////////////////////////////////////////////////
//// Functions provided in this module.
/** Invoke the motion detector on the specified image with the specified
* options and return the result.
* Any unrecognized options are ignored.
* Note that previously applied options for a given filter will still be
* used, even if they are not set in this call.
* If this is the first image provided, then
* no motion is detected. Otherwise, the image is compared against the one
* previously passed to this same function.
* @param image The image to filter.
* @param options An object whose fields specify filter options.
* @return The filtered image.
*/
exports.filter = function (image, options) {
image = image.asAWTImage();
if (options) {
for (var optionName in options) {
// Look for a setter function for the option.
var setter = 'set' + optionName;
if (typeof filter[setter] === 'function') {
// Invoke the setter function.
filter[setter](options[optionName]);
}
}
}
// The second (null) argument declines to give a destination image.
return filter.filter(image, null);
};
/** Return a percentage of area covered by motion in the most recently provided
* image, where 0 means no motion and 100 means full image motion.
* @return The percentage of area covered by motion.
*/
exports.area = function () {
return filter.getMotionArea();
};
/** Return an array of two numbers giving the center of gravity of
* the detected motion in pixel coordinates, or return null if no
* motion has been detected.
* @return The center of gravity of motion.
*/
exports.cog = function () {
var cog = filter.getCOG();
if (cog) {
return [cog.x, cog.y];
}
return null;
};