#include <iostream>
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

void arithmetic() {
	Mat src1 = imread("..\\..\\resources\\images\\lenna256.bmp", IMREAD_GRAYSCALE);
	resize(src1, src1, Size(256, 256));
	Mat src2 = imread("..\\..\\resources\\images\\square.bmp", IMREAD_GRAYSCALE);
	resize(src2, src2, Size(256, 256));

	imshow("src1", src1);
	imshow("src2", src2);

	Mat dst1, dst2, dst3, dst4;
	add(src1, src2, dst1);
	addWeighted(src1, 0.5, src2, 0.5, 0, dst2);
	subtract(src1, src2, dst3);
	absdiff(src1, src2, dst4);

	imshow("dst1", dst1);
	imshow("dst2", dst2);
	imshow("dst3", dst3);
	imshow("dst4", dst4);
	waitKey();
}

void add() {
	Mat img1 = imread("..\\..\\resources\\images\\lenna.bmp", IMREAD_GRAYSCALE);
	resize(img1, img1, Size(256, 256));
	Mat img2 = imread("..\\..\\resources\\images\\Cameraman256.bmp", IMREAD_GRAYSCALE);

	Mat addimg;

	add(img1, img2, addimg, noArray(), -1);

	imshow("img1", img1);
	imshow("img2", img2);
	imshow("addimg", addimg);

	waitKey();
	destroyAllWindows();
}

bool equls(Mat &left, Mat &right) {
	for (int i=0; i < left.cols; i++)
		for (int j=0; j < left.rows; j++)
			if (left.at<uchar>(j, i) - right.at<uchar>(j, i))
				return false;
	return true;
}

void _absdiff() {
	VideoCapture video;
	video.open("..\\..\\resources\\images\\vtest.avi");
	if (!video.isOpened())
		return;
	Mat frame, previous_frame;

	video >> frame;
	previous_frame = frame;

	double fps = video.get(CAP_PROP_FPS);

	while (!frame.empty()) {
		video >> frame;
		imshow("frame", frame);
		Mat diff;
		if (equls(previous_frame, frame)) {
			absdiff(frame, previous_frame, diff);
			imshow("diff", diff);
		}
		previous_frame = frame;
		waitKey(cvRound(1000/fps));
	}
}

int main() {
	_absdiff();
	return EXIT_SUCCESS;
}