opencv/samples/cpp/morphology2.cpp

110 lines
3.1 KiB
C++
Raw Normal View History

2016-02-15 21:37:29 +08:00
#include "opencv2/imgproc.hpp"
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
2016-02-15 21:37:29 +08:00
#include "opencv2/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
2015-08-01 23:24:23 +08:00
#include <string>
using namespace cv;
static void help(char** argv)
2010-11-29 15:57:03 +08:00
{
2010-12-04 16:30:56 +08:00
printf("\nShow off image morphology: erosion, dialation, open and close\n"
"Call:\n %s [image]\n"
"This program also shows use of rect, ellipse and cross kernels\n\n", argv[0]);
2010-11-29 15:57:03 +08: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" );
}
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-08 01:21:29 +08:00
static void OpenClose(int, void*)
{
2018-10-31 20:48:56 +08:00
int n = open_close_pos;
int an = abs(n);
Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
if( n < 0 )
morphologyEx(src, dst, MORPH_OPEN, element);
else
morphologyEx(src, dst, MORPH_CLOSE, element);
imshow("Open/Close",dst);
}
// callback function for erode/dilate trackbar
2012-06-08 01:21:29 +08:00
static void ErodeDilate(int, void*)
{
2018-10-31 20:48:56 +08:00
int n = erode_dilate_pos;
int an = abs(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 )
{
2018-10-31 20:48:56 +08:00
cv::CommandLineParser parser(argc, argv, "{help h||}{ @image | baboon.jpg | }");
2015-08-01 23:24:23 +08:00
if (parser.has("help"))
{
help(argv);
2015-08-01 23:24:23 +08:00
return 0;
}
2018-10-31 20:48:56 +08:00
std::string filename = samples::findFile(parser.get<std::string>("@image"));
2016-02-15 21:37:29 +08:00
if( (src = imread(filename,IMREAD_COLOR)).empty() )
2015-08-01 23:24:23 +08:00
{
help(argv);
return -1;
2015-08-01 23:24:23 +08: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);
2018-10-31 20:48:56 +08:00
setTrackbarMin("iterations", "Open/Close", -max_iters);
setTrackbarMax("iterations", "Open/Close", max_iters);
setTrackbarPos("iterations", "Open/Close", 0);
createTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
2018-10-31 20:48:56 +08:00
setTrackbarMin("iterations", "Erode/Dilate", -max_iters);
setTrackbarMax("iterations", "Erode/Dilate", max_iters);
setTrackbarPos("iterations", "Erode/Dilate", 0);
for(;;)
{
OpenClose(open_close_pos, 0);
ErodeDilate(erode_dilate_pos, 0);
char c = (char)waitKey(0);
if( c == 27 )
break;
if( c == 'e' )
element_shape = MORPH_ELLIPSE;
else if( c == 'r' )
element_shape = MORPH_RECT;
else if( c == 'c' )
element_shape = MORPH_CROSS;
else if( c == ' ' )
element_shape = (element_shape + 1) % 3;
}
return 0;
}