1. 程式人生 > >基於OpenCV的膨脹和腐蝕

基於OpenCV的膨脹和腐蝕

本部落格講解形態雪中的膨脹和腐蝕操作。使用的函式為:
        cv::erode

        cv::dilate

形態學操作

簡而言之:一組基於形狀的影象處理的操作。形態學運算對輸入影象應用astructuring元素生成一個輸出影象。
最基本的形態操作是兩個:腐蝕和膨脹。他們有一個廣泛的用途,即:

  • 去除噪聲
  • 單個元素的分離和影象中的分離元素的連線
  • 發現影象中的強度顛簸或空洞

我們將簡要地解釋擴張和侵蝕,使用下面的影象作為一個例子:


Dilation

  • 該操作由卷積影象 A 和某個卷積核 ( B)的卷積完成,卷積核可以有任何形狀或大小,通常是一個方形或圓形.
  • 卷積核 B 有一個定義的錨定點( anchor point), 通常是核心的中心
    .
  • 隨著卷積核 B 掃過影象, 我們計算由卷積核 B 覆蓋的的畫素的最大值,並用該最大值替換在錨定點位置的影象畫素可以推斷,這種最大化的操作使影象內明亮區域“生長”(也就是所謂的膨脹)。以上面的圖片為例。應用擴張,我們可以得到:


圍繞黑色字母區域的背景 (亮的區域) 的膨脹

Erosion

這個操作是在核心重疊區域上計算一個區域性最小值.

隨著卷積核B 掃過影象, 計算有卷積核 B 覆蓋部分畫素的最小值,並用最小值替換在錨定點的影象畫素.

和dilation的例子一樣,我們可以對原始影象應用腐蝕運算元。你可以看到在下面的結果,影象的明亮區域(背景,顯然的),變得更薄,而黑暗的區域(“書寫”部分)變得更大。



/**
 * @file Morphology_1.cpp
 * @brief Erosion and Dilation sample code
 * @author OpenCV team
 */

#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;

/// Global variables
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, char** argv )
{
  /// Load an image
  src = imread( argv[1], IMREAD_COLOR );

  if( src.empty() )
    { return -1; }

  /// Create windows
  namedWindow( "Erosion Demo", WINDOW_AUTOSIZE );
  namedWindow( "Dilation Demo", WINDOW_AUTOSIZE );
  moveWindow( "Dilation Demo", src.cols, 0 );

  /// Create Erosion 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 );

  /// Create Dilation 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 = 0;
  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; }

  //![kernel]
  Mat element = getStructuringElement( erosion_type,
                                Size( 2*erosion_size + 1, 2*erosion_size+1 ),
                                Point( erosion_size, erosion_size ) );
  //![kernel]

  /// Apply the erosion operation
  erode( src, erosion_dst, element );
  imshow( "Erosion Demo", erosion_dst );
}
//![腐蝕]

//![<span>膨脹</span>]
/**
 * @function Dilation
 */
void Dilation( int, void* )
{
  int dilation_type = 0;
  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 ) );

  /// Apply the dilation operation
  dilate( src, dilation_dst, element );
  imshow( "Dilation Demo", dilation_dst );
}
//![<span>膨脹</span>]

例子