วันพฤหัสบดีที่ 23 มีนาคม พ.ศ. 2560

C++ opencv Morphological

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

/// Global variables
Mat frame, dst;

int morph_elem = 0;
int morph_size = 0;
int morph_operator = 0;
int const max_operator = 4;
int const max_elem = 2;
int const max_kernel_size = 21;

/** Function Headers */
void Morphology_Operations(int, void*);

/** @function main */
int main(int argc, char** argv)
{
VideoCapture cap(0);

while (1)
{
cap >> frame;
/// Create window
namedWindow("Morphological", CV_WINDOW_AUTOSIZE);

/// Create Trackbar to select Morphology operation
createTrackbar("Operator:\n 0: Opening - 1: Closing \n 2: Gradient - 3: Top Hat \n 4: Black Hat", "Morphological", &morph_operator, max_operator, Morphology_Operations);

/// Create Trackbar to select kernel type
createTrackbar("Element:\n 0: Rect - 1: Cross - 2: Ellipse", "Morphological",
&morph_elem, max_elem,
Morphology_Operations);

/// Create Trackbar to choose kernel size
createTrackbar("Kernel size:\n 2n +1", "Morphological",
&morph_size, max_kernel_size,
Morphology_Operations);

/// Default start
Morphology_Operations(0, 0);
if (waitKey(30) == 27)
break;

}
return 0;
}

//function Morphology_Operations

void Morphology_Operations(int, void*)
{
// Since MORPH_X : 2,3,4,5 and 6
int operation = morph_operator + 2;

Mat element = getStructuringElement(morph_elem, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));

/// Apply the specified morphology operation
morphologyEx(frame, dst, operation, element);
imshow("Morphological", dst);
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น