OpenCV學習教程之腐蝕與膨脹

2021-09-27 00:01:37 字數 3510 閱讀 8777

如何使用opencv提供的兩種最基本的形態學操作,腐蝕與膨脹( erosion 與 dilation)

內容來自於bradski和kaehler的大作: learning opencv .

#include "pch.h"

#include #include #include #include using namespace std;

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;

//函式宣告

void erosion(int, void*);

void dilation(int, void*);

int main()

//建立顯示視窗

namedwindow("erosion demo", cv_window_autosize);

namedwindow("dilation demo", cv_window_autosize);

movewindow("dilation demo", src.cols, 0);

//建立腐蝕

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

//建立膨脹

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

//預設執行

erosion(0, 0);

dilation(0, 0);

waitkey(0);

return 0;

}void erosion(int, void*)

else if (erosion_elem == 1)

else if (erosion_elem == 2)

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

}void dilation(int, void*)

else if (dilation_elem)

else if (dilation_elem)

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

}

大部分**應該不需要解釋了 (如果有任何疑問,請回頭參考前面的教程)。 讓我們來回顧一下本程式的總體流程:

/**  @function erosion  */

void erosion( int, void* )

else if( erosion_elem == 1 )

else if( erosion_elem == 2)

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

}

進行 腐蝕 操作的函式是 erode 。 它接受了三個引數:

mat element = getstructuringelement( erosion_type,

size( 2*erosion_size + 1, 2*erosion_size+1 ),

point( erosion_size, erosion_size ) );

我們可以為我們的核心選擇三種形狀之一:

矩形: morph_rect

交叉形: morph_cross

橢圓形: morph_ellipse

然後,我們還需要指定核心大小,以及 錨點 位置。不指定錨點位置,則預設錨點在核心中心位置。

就這些了,我們現在可以對影象進行腐蝕操作了。

/** @function dilation */

void dilation( int, void* )

else if( dilation_elem == 1 )

else if( dilation_elem == 2)

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

}

你可以看到,它和 erosion 函式是多麼相似。 這裡我們同樣可以指定核心的形狀,錨點和大小。

opencv學習 5 腐蝕與膨脹

include opencv2 imgproc imgproc.hpp include opencv2 imgcodecs.hpp include opencv2 highgui highgui.hpp include include using namespace cv global variab...

Opencv 膨脹與腐蝕

簡而言之 一組基於形狀處理影象的操作。形態操作將結構元素應用於輸入影象並生成輸出影象。最基本的形態作用是 侵蝕和擴張。它們有廣泛的用途,即 消除噪音 隔離單個元素並連線影象中的不同元素。查詢影象中的強度凸點或孔 我們將簡要解釋膨脹和侵蝕,使用以下影象作為示例 該操作包括將影象與某些核心 b 進行卷積...

OpenCV 膨脹與腐蝕

include include include using namespace cv using namespace std int element size 3 int max size 21 mat src,dst void callback demo int,void int main int...