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

int main() {
	cv::VideoCapture camera(0);
	if (!camera.isOpened()) {
		std::cerr << "isOpened()" << std::endl;
		return EXIT_FAILURE;
	}
	std::uint32_t fps = camera.get(cv::CAP_PROP_FPS);
	cv::Mat src;

	while (cv::waitKey(1000 / fps) != 27) {
		camera >> src;
		if (src.empty()) break;
		cv::imshow("preview", src);
	}
	cv::cvtColor(src, src, cv::COLOR_BGR2GRAY);

	cv::Mat dx, dy, fmag, mag, phase;

	cv::Sobel(src, dx, CV_32FC1, 1, 0);
	cv::Sobel(src, dy, CV_32FC1, 0, 1);

	cv::magnitude(dx, dy, fmag);
	fmag.convertTo(mag, CV_8UC1);

	cv::phase(dx, dy, phase, true);
	cv::normalize(phase, phase, 0, 255, cv::NORM_MINMAX);
	phase.convertTo(phase, CV_8UC1);

	cv::Mat edge = mag > 150;

	cv::imshow("src", src);
	cv::imshow("mag", mag);
	cv::imshow("edge", edge);
	cv::imshow("phase", phase);

	cv::waitKey();
	cv::destroyAllWindows();
}