00001 /************************************************************************************************** 00002 Software License Agreement (BSD License) 00003 00004 Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt 00005 All rights reserved. 00006 00007 Redistribution and use in source and binary forms, with or without modification, are permitted 00008 provided that the following conditions are met: 00009 00010 *Redistributions of source code must retain the above copyright notice, this list of 00011 conditions and the following disclaimer. 00012 *Redistributions in binary form must reproduce the above copyright notice, this list of 00013 conditions and the following disclaimer in the documentation and/or other materials provided 00014 with the distribution. 00015 *Neither the name of the University of Aveiro nor the names of its contributors may be used to 00016 endorse or promote products derived from this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 00019 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00021 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00024 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00025 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 ***************************************************************************************************/ 00033 #include "opencv2/imgproc/imgproc.hpp" 00034 #include "opencv2/highgui/highgui.hpp" 00035 #include <stdlib.h> 00036 #include <stdio.h> 00037 00038 using namespace cv; 00039 00041 Mat src, dst; 00042 00043 int morph_elem = 0; 00044 int morph_size = 0; 00045 int morph_operator = 0; 00046 int const max_operator = 4; 00047 int const max_elem = 2; 00048 int const max_kernel_size = 21; 00049 00050 const char* window_name = "Morphology Transformations Demo"; 00051 00052 00054 void Morphology_Operations( int, void* ); 00055 00059 int main( int, char** argv ) 00060 { 00062 src = imread( argv[1] ); 00063 00064 if( !src.data ) 00065 { return -1; } 00066 00068 namedWindow( window_name, WINDOW_AUTOSIZE ); 00069 00071 createTrackbar("Operator:\n 0: Opening - 1: Closing \n 2: Gradient - 3: Top Hat \n 4: Black Hat", window_name, &morph_operator, max_operator, Morphology_Operations ); 00072 00074 createTrackbar( "Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name, 00075 &morph_elem, max_elem, 00076 Morphology_Operations ); 00077 00079 createTrackbar( "Kernel size:\n 2n +1", window_name, 00080 &morph_size, max_kernel_size, 00081 Morphology_Operations ); 00082 00084 Morphology_Operations( 0, 0 ); 00085 00086 waitKey(0); 00087 return 0; 00088 } 00089 00093 void Morphology_Operations( int, void* ) 00094 { 00095 00096 // Since MORPH_X : 2,3,4,5 and 6 00097 int operation = morph_operator + 2; 00098 00099 Mat element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) ); 00100 00102 morphologyEx( src, dst, operation, element ); 00103 imshow( window_name, dst ); 00104 } 00105 00106