morphology2.cpp 2.6 KB
Newer Older
1 2
#define CV_NO_BACKWARD_COMPATIBILITY

3 4
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
5 6 7 8 9
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

10
static void help()
Gary Bradski's avatar
Gary Bradski committed
11 12
{

Gary Bradski's avatar
Gary Bradski committed
13
printf("\nShow off image morphology: erosion, dialation, open and close\n"
14 15
    "Call:\n   morphology2 [image]\n"
    "This program also shows use of rect, elipse and cross kernels\n\n");
Gary Bradski's avatar
Gary Bradski committed
16 17 18 19 20 21 22 23
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" );
}

24 25 26 27 28 29 30 31 32 33
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
34
static void OpenClose(int, void*)
35 36 37 38 39 40 41 42 43 44 45 46
{
    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 )
        morphologyEx(src, dst, CV_MOP_OPEN, element);
    else
        morphologyEx(src, dst, CV_MOP_CLOSE, element);
    imshow("Open/Close",dst);
}

// callback function for erode/dilate trackbar
47
static void ErodeDilate(int, void*)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
{
    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 )
{
    char* filename = argc == 2 ? argv[1] : (char*)"baboon.jpg";
    if( (src = imread(filename,1)).data == 0 )
        return -1;

Gary Bradski's avatar
Gary Bradski committed
66
    help();
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    //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);
        c = cvWaitKey(0);

        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;
}