2011-05-21 20:32:34 +02:00
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
2014-07-04 16:48:15 +02:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
2011-05-21 20:32:34 +02:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
2010-05-11 19:44:00 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static void help()
|
2010-11-29 08:57:03 +01:00
|
|
|
{
|
|
|
|
|
2010-12-04 09:30:56 +01:00
|
|
|
printf("\nShow off image morphology: erosion, dialation, open and close\n"
|
2012-06-07 19:21:29 +02:00
|
|
|
"Call:\n morphology2 [image]\n"
|
2014-03-03 16:37:47 +01:00
|
|
|
"This program also shows use of rect, ellipse and cross kernels\n\n");
|
2010-11-29 08:57:03 +01:00
|
|
|
printf( "Hot keys: \n"
|
|
|
|
"\tESC - quit the program\n"
|
|
|
|
"\tr - use rectangle structuring element\n"
|
|
|
|
"\te - use elliptic structuring element\n"
|
|
|
|
"\tc - use cross-shaped structuring element\n"
|
|
|
|
"\tSPACE - loop through all the options\n" );
|
|
|
|
}
|
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
Mat src, dst;
|
|
|
|
|
|
|
|
int element_shape = MORPH_RECT;
|
|
|
|
|
|
|
|
//the address of variable which receives trackbar position update
|
|
|
|
int max_iters = 10;
|
|
|
|
int open_close_pos = 0;
|
|
|
|
int erode_dilate_pos = 0;
|
|
|
|
|
|
|
|
// callback function for open/close trackbar
|
2012-06-07 19:21:29 +02:00
|
|
|
static void OpenClose(int, void*)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
int n = open_close_pos - max_iters;
|
|
|
|
int an = n > 0 ? n : -n;
|
|
|
|
Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
|
|
|
|
if( n < 0 )
|
2013-04-06 16:16:51 +02:00
|
|
|
morphologyEx(src, dst, MORPH_OPEN, element);
|
2010-05-11 19:44:00 +02:00
|
|
|
else
|
2013-04-06 16:16:51 +02:00
|
|
|
morphologyEx(src, dst, MORPH_CLOSE, element);
|
2010-05-11 19:44:00 +02:00
|
|
|
imshow("Open/Close",dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback function for erode/dilate trackbar
|
2012-06-07 19:21:29 +02:00
|
|
|
static void ErodeDilate(int, void*)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
int n = erode_dilate_pos - max_iters;
|
|
|
|
int an = n > 0 ? n : -n;
|
|
|
|
Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
|
|
|
|
if( n < 0 )
|
|
|
|
erode(src, dst, element);
|
|
|
|
else
|
|
|
|
dilate(src, dst, element);
|
|
|
|
imshow("Erode/Dilate",dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
2014-09-13 16:28:41 +02:00
|
|
|
char* filename = argc == 2 ? argv[1] : (char*)"../data/baboon.jpg";
|
2014-08-13 13:08:27 +02:00
|
|
|
if( (src = imread(filename,1)).empty() )
|
2010-05-11 19:44:00 +02:00
|
|
|
return -1;
|
|
|
|
|
2010-11-29 08:57:03 +01:00
|
|
|
help();
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
//create windows for output images
|
|
|
|
namedWindow("Open/Close",1);
|
|
|
|
namedWindow("Erode/Dilate",1);
|
|
|
|
|
|
|
|
open_close_pos = erode_dilate_pos = max_iters;
|
|
|
|
createTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose);
|
|
|
|
createTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
OpenClose(open_close_pos, 0);
|
|
|
|
ErodeDilate(erode_dilate_pos, 0);
|
2013-10-30 13:34:27 +01:00
|
|
|
c = waitKey(0);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
if( (char)c == 27 )
|
|
|
|
break;
|
|
|
|
if( (char)c == 'e' )
|
|
|
|
element_shape = MORPH_ELLIPSE;
|
|
|
|
else if( (char)c == 'r' )
|
|
|
|
element_shape = MORPH_RECT;
|
|
|
|
else if( (char)c == 'c' )
|
|
|
|
element_shape = MORPH_CROSS;
|
|
|
|
else if( (char)c == ' ' )
|
|
|
|
element_shape = (element_shape + 1) % 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|