opencv 笔记10 Imgproc_Morphology

膨胀和腐蚀
膨胀就是使用算法,将图像的边缘扩大些。作用就是将目标的边缘或者是内部的坑填掉。 腐蚀就是使用算法,将图像的边缘腐蚀掉。作用就是将目标的边缘的“毛刺”踢除掉。 使用相同次数的腐蚀与膨胀,可以使目标表面更平滑。
若先腐蚀后膨胀的过程:利用它可以消除小物体,在纤细点处分离物体,平滑较大物体边界,但同时并不会明显改变原来物体的面积。而先膨胀后腐蚀的过程:利用它可以填充物体内细小空洞,连接临近物体、平滑其边界,但同时并不会明显改变原来物体的面积。
通常由于噪声的影响,图像在阈值化后所得到的边界通常都很不平滑,物体区域具有一些噪声孔,而背景区域上散布着一些小的噪声物体,连续的开和闭运算可以有效的改善这种情况,而有时,我们需要经过多次腐蚀之,后再加上相同次数的膨胀,才能产生比较好的处理效果。可见图像腐蚀与图像膨胀相结合有时可使图像有较理想的处理效果。
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "highgui.h"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/// 全局变量
Mat src, erosion_dst, dilation_dst;

int erosion_elem = 0;
int erosion_size = 0;
int dilation_elem = 0;
int dilation_size = 0;
int const max_elem = 2;
int const max_kernel_size = 21;

/** Function Headers */
void Erosion( int, void* );
void Dilation( int, void* );

/** @function main */
int main( int argc, char** argv )
{
  /// Load 图像
  src = imread( argv[1] );

  if( !src.data )
  { return -1; }

  /// 创建显示窗口
  namedWindow( "Erosion Demo", CV_WINDOW_AUTOSIZE );
  namedWindow( "Dilation Demo", CV_WINDOW_AUTOSIZE );
  cvMoveWindow( "Dilation Demo", src.cols, 0 );

  /// 创建腐蚀 Trackbar
  createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo",
                  &erosion_elem, max_elem,
                  Erosion );

  createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo",
                  &erosion_size, max_kernel_size,
                  Erosion );

  /// 创建膨胀 Trackbar
  createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo",
                  &dilation_elem, max_elem,
                  Dilation );

  createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo",
                  &dilation_size, max_kernel_size,
                  Dilation );

  /// Default start
  Erosion( 0, 0 );
  Dilation( 0, 0 );

  waitKey(0);
  return 0;
}

/**  @function Erosion  */
void Erosion( int, void* )
{
  int erosion_type;
  if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
  else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
  else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }

  Mat element = getStructuringElement( erosion_type,
                                       Size( 2*erosion_size + 1, 2*erosion_size+1 ),
                                       Point( erosion_size, erosion_size ) );

  /// 腐蚀操作
  erode( src, erosion_dst, element );
  imshow( "Erosion Demo", erosion_dst );
}

/** @function Dilation */
void Dilation( int, void* )
{
  int dilation_type;
  if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
  else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
  else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }

  Mat element = getStructuringElement( dilation_type,
                                       Size( 2*dilation_size + 1, 2*dilation_size+1 ),
                                       Point( dilation_size, dilation_size ) );
  ///膨胀操作
  dilate( src, dilation_dst, element );
  imshow( "Dilation Demo", dilation_dst );
}
void erode(const Mat& srcMat& dst, const Mat& element, Point anchor=Point(-1, -1), int iterations=1, intborderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue())
void dilate(const Mat& srcMat& dst, const Mat& element, Point anchor=Point(-1, -1), int iterations=1, intborderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue())

 
 
Parameters:
  • src – The source image
  • dst – The destination image. It will have the same size and the same type as src
  • element – The structuring element used for dilation. If element=Mat() , a 3\times 3 rectangular structuring element is used
  • anchor – Position of the anchor within the element. The default value (-1, -1) means that the anchor is at the element center
  • iterations – The number of times erosion is applied
  • borderType – The pixel extrapolation method; see borderInterpolate()
  • borderValue – The border value in case of a constant border. The default value has a special meaning, seecreateMorphoogyFilter()
\texttt{dst} (x,y) =  \min _{(x',y'):  \, \texttt{element} (x',y') \ne0 } \texttt{src} (x+x',y+y')
Mat getStructuringElement(int shape, Size esize, Point anchor=Point(-1, -1))

 
 
Parameters:
  • shape – The element shape, one of:
  • MORPH_RECT - rectangular structuring element

    E_{ij}=1

  • MORPH_ELLIPSE - elliptic structuring element, i.e. a filled ellipse inscribed into the rectangle

    Rect(0, 0, esize.width, 0.esize.height)

  • MORPH_CROSS - cross-shaped structuring element:

    E_{ij} =  \fork{1}{if i=\texttt{anchor.y} or j=\texttt{anchor.x}}{0}{otherwise}

Parameters:                          
  • esize – Size of the structuring element
  • anchor – The anchor position within the element. The default value (-1, -1) means that the anchor is at the center. Note that only the cross-shaped element’s shape depends on the anchor position; in other cases the anchor just regulates by how much the result of the morphological operation is shifted
int createTrackbar(const string& trackbarname, const string& winname, int* value, int count, TrackbarCallback onChangeCV_DEFAULT(0), void* userdata CV_DEFAULT(0))

 
 
Parameters:                                 
  • trackbarname – Name of the created trackbar.
  • winname – Name of the window which will be used as a parent of the created trackbar.
  • value – The optional pointer to an integer variable, whose value will reflect the position of the slider. Upon creation, the slider position is defined by this variable.
  • count – The maximal position of the slider. The minimal position is always 0.
  • onChange – Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is NULL pointer, then no callbacks is called, but only value is updated
  • userdata – The user data that is passed as-is to the callback; it can be used to handle trackbar events without using global variables
void cvMoveWindow( const char* name, int x, int y );//设定窗口的位置,标题,x、y坐标

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值