// Path: src/pool/tests/ColorSpace.java
// public class ColorSpace {
// 	public static void main (String[] args) {
// 		CVLoader.load();
// 		Mat orig = Highgui.imread("data/topdown-6.jpg");
// 		Mat hsv = new Mat();
// 		Imgproc.cvtColor(orig, hsv, Imgproc.COLOR_BGR2YCrCb);
// 		
// 		List<Mat> channels = new ArrayList<Mat>();
// 		for(int i = 0; i < hsv.channels(); i++) {
// 			Mat channel = new Mat();
// 			channels.add(channel);
// 		}
// 		Core.split(hsv, channels);
// 		
// 		for(Mat channel: channels) {
// 			ImgWindow.newWindow(channel);
// 		}
// 	}
// 	
// 	public static Mat getChannel(Mat img, int channelIdx) {
// 		List<Mat> channels = new ArrayList<Mat>();
// 		for(int i = 0; i < img.channels(); i++) {
// 			Mat channel = new Mat();
// 			channels.add(channel);
// 		}
// 		Core.split(img, channels);
// 		return channels.get(channelIdx);
// 	}
// 	
// 	public static Mat getChannel(Mat orig, int colorSpace, int channelIdx) {
// 		Mat hsv = new Mat();
// 		Imgproc.cvtColor(orig, hsv, colorSpace);
// 		List<Mat> channels = new ArrayList<Mat>();
// 		for(int i = 0; i < hsv.channels(); i++) {
// 			Mat channel = new Mat();
// 			channels.add(channel);
// 		}
// 		Core.split(hsv, channels);
// 		return channels.get(channelIdx);
// 	}
// }


import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import pool.tests.ColorSpace;
package pool.utils;



public class MotionDetector {
	private float thresholdPercentage = 0.001f;
	private Mat lastImage;
	private Mat mask;
	
	/**
	 * @return true if motion was detected compared to the last frame
	 */
	public boolean detect(Mat frame) {
		if(lastImage == null) {
			lastImage = frame.clone();
			return true;
		}
		
		Mat diff = new Mat();
		Core.absdiff(lastImage, frame, diff);
		Imgproc.threshold(diff, diff, 35, 255, Imgproc.THRESH_BINARY);
		
		// extract color channels and merge them to single bitmask
// Path: src/pool/tests/ColorSpace.java
// public class ColorSpace {
// 	public static void main (String[] args) {
// 		CVLoader.load();
// 		Mat orig = Highgui.imread("data/topdown-6.jpg");
// 		Mat hsv = new Mat();
// 		Imgproc.cvtColor(orig, hsv, Imgproc.COLOR_BGR2YCrCb);
// 		
// 		List<Mat> channels = new ArrayList<Mat>();
// 		for(int i = 0; i < hsv.channels(); i++) {
// 			Mat channel = new Mat();
// 			channels.add(channel);
// 		}
// 		Core.split(hsv, channels);
// 		
// 		for(Mat channel: channels) {
// 			ImgWindow.newWindow(channel);
// 		}
// 	}
// 	
// 	public static Mat getChannel(Mat img, int channelIdx) {
// 		List<Mat> channels = new ArrayList<Mat>();
// 		for(int i = 0; i < img.channels(); i++) {
// 			Mat channel = new Mat();
// 			channels.add(channel);
// 		}
// 		Core.split(img, channels);
// 		return channels.get(channelIdx);
// 	}
// 	
// 	public static Mat getChannel(Mat orig, int colorSpace, int channelIdx) {
// 		Mat hsv = new Mat();
// 		Imgproc.cvtColor(orig, hsv, colorSpace);
// 		List<Mat> channels = new ArrayList<Mat>();
// 		for(int i = 0; i < hsv.channels(); i++) {
// 			Mat channel = new Mat();
// 			channels.add(channel);
// 		}
// 		Core.split(hsv, channels);
// 		return channels.get(channelIdx);
// 	}
// }


// Path: src/pool/utils/MotionDetector.java
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import pool.tests.ColorSpace;

package pool.utils;



public class MotionDetector {
	private float thresholdPercentage = 0.001f;
	private Mat lastImage;
	private Mat mask;
	
	/**
	 * @return true if motion was detected compared to the last frame
	 */
	public boolean detect(Mat frame) {
		if(lastImage == null) {
			lastImage = frame.clone();
			return true;
		}
		
		Mat diff = new Mat();
		Core.absdiff(lastImage, frame, diff);
		Imgproc.threshold(diff, diff, 35, 255, Imgproc.THRESH_BINARY);
		
		// extract color channels and merge them to single bitmask
		Mat r = ColorSpace.getChannel(diff, 2);