OpenCV学习笔记-形态学操作-腐蚀膨胀-开闭运算

一个可以调节参数的形态学例题,用到的自定义核函数为:

IplConvKernel * cvCreateStructuringElementEx(
int cols,
int rows,
int anchor_x,
int anchor_y,
int shape,
int *value=NULL
);


函数中参数cols,rows确定了构造的矩形大小。

anchor_x,anchor_y确定了封闭矩形内参考点的横纵坐标。

shape是自定义核的形状,具体为

1.CV_SHAPE_RECT  核是矩形

2.CV_SHAPE_CROSS 核是勺子交叉形

3.CV_SHAPE_ELLIPSE 核是椭圆形

4.CV_SHAPE_CUSTOM 核是用户自定义类型


其释放函数为

void cvReleaseStructuringElement( IplConvKernel **element);

实例代码如下:

#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
IplImage* src = 0;
IplImage* dst = 0;
IplConvKernel* element = 0;
int element_shape = CV_SHAPE_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
void OpenClose(int pos)   
{
    int n = open_close_pos - max_iters;
    int an = n > 0 ? n : -n;
    element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
    if( n < 0 )
    {
        cvErode(src,dst,element,1);
        cvDilate(dst,dst,element,1);
    }
    else
    {
        cvDilate(src,dst,element,1);
        cvErode(dst,dst,element,1);
    }
    cvReleaseStructuringElement(&element);
    cvShowImage("Open/Close",dst);
}  
// callback function for erode/dilate trackbar
void ErodeDilate(int pos)   
{
    int n = erode_dilate_pos - max_iters;
    int an = n > 0 ? n : -n;
    element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
    if( n < 0 )
    {
        cvErode(src,dst,element,1);
    }
    else
    {
        cvDilate(src,dst,element,1);
    }
    cvReleaseStructuringElement(&element);
    cvShowImage("Erode/Dilate",dst);
}  

int main( int argc, char** argv )
{
    char* filename = argc == 2 ? argv[1] : (char*)"baboon.jpg";
    if( (src = cvLoadImage(filename,1)) == 0 )
        return -1;
    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"
        "\tENTER - loop through all the options\n" );
    dst = cvCloneImage(src);
    //create windows for output images
    cvNamedWindow("Open/Close",1);
    cvNamedWindow("Erode/Dilate",1);
    open_close_pos = erode_dilate_pos = max_iters;
    cvCreateTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose);
    cvCreateTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
    for(;;)
    {
        int c;
        
        OpenClose(open_close_pos);
        ErodeDilate(erode_dilate_pos);
        c = cvWaitKey(0);
        if( (char)c == 27 )
            break;
        if( (char)c == 'e' )
            element_shape = CV_SHAPE_ELLIPSE;
        else if( (char)c == 'r' )
            element_shape = CV_SHAPE_RECT;
        else if( (char)c == 'c' )
            element_shape = CV_SHAPE_CROSS;
        else if( (char)c == '\n' )
            element_shape = (element_shape + 1) % 3;
    }
    //release images
    cvReleaseImage(&src);
    cvReleaseImage(&dst);
    //destroy windows 
    cvDestroyWindow("Open/Close"); 
    cvDestroyWindow("Erode/Dilate");
    return 0;
}

运行结果图为:

OpenCV学习笔记-形态学操作-腐蚀膨胀-开闭运算_第1张图片

说明:

在此例题中开运算和闭运算都是通过腐蚀-膨胀来实现的,而在OpenCV中有封装好的开闭运算可以直接调用。该函数为

void cvMorphologyEx(
const CvArr * src,
CvArr *dst,
CvArr *temp,
IplConvKernel *element,
int operation,
int iterations=1
)

参考资料:

1.http://hi.baidu.com/shentuhongfeng/blog/item/077c2b63377dfc6c0c33fab8.html

2.学习OpenCV,于仕祺,刘瑞祯,清华大学出版社,134-139

你可能感兴趣的:(c,windows,function,callback,DST,output)