// Erosion.cpp
// Chris Pocock 2014

#include "Erosion.h"

Erosion::Erosion() {

	// Default class variables
	erosion_type = MORPH_ELLIPSE;
	erosion_elem = 2;
	erosion_size = 1;
	kernel_ero = getStructuringElement( erosion_type,Size( 1 + erosion_size, 1 + erosion_size ));

	// Trackbar  upper limits
	max_elem = 2;
	max_kernel_size = 21;
}

Erosion::~Erosion() {
}

// Erode image, return result
Mat Erosion::erode(Mat &input){

	Mat output;
	cv::erode(input, output, kernel_ero);
	return output;
}

// Update erosion element type and kernel size
void Erosion::update(){

		if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
		else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
		else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }

		kernel_ero = getStructuringElement( erosion_type,
				Size( 1 + erosion_size, 1 + erosion_size ));
}

// Create windows and trackbars for observation & live parameter tweaking
void Erosion::setupUi(){

	namedWindow( "Erosion", CV_WINDOW_AUTOSIZE );
	moveWindow("Erosion", 0, 250);

	createTrackbar( "Erosion Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion",
			&erosion_elem, max_elem);

	createTrackbar( "Erosion Kernel size: \n 1+x", "Erosion",
			&erosion_size, max_kernel_size);
}
