opencv學習 5 腐蝕與膨脹

2021-07-14 08:29:54 字數 3990 閱讀 2993

#include "opencv2/imgproc/imgproc.hpp"

#include "opencv2/imgcodecs.hpp"

#include "opencv2/highgui/highgui.hpp"

#include #include 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;

intconst max_elem = 2;

intconst max_kernel_size = 21;

/** function headers */

void erosion( int, void* );

void dilation( int, void* );

/** *@function main

*/int main( int, char** argv )

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

return0;}

/** *@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 );

}/**

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

}

參考:

主要用到了getstructuringelement(),erode(),dilate()三個函式。

#include "opencv2/imgproc/imgproc.hpp"

#include "opencv2/imgcodecs.hpp"

#include "opencv2/highgui/highgui.hpp"

#include #include using namespace cv;

/// global variables

mat src, dst;

int morph_elem = 0;

int morph_size = 0;

int morph_operator = 0;

intconst max_operator = 4;

intconst max_elem = 2;

intconst max_kernel_size = 21;

const

char* window_name = "morphology transformations demo";

/** function headers */

void morphology_operations( int, void* );

/** *@function main

*/int main( int, char** argv )

/// create window

namedwindow( window_name, window_autosize );

/// create trackbar to select morphology operation

createtrackbar("operator:\n 0: opening - 1: closing \n 2: gradient - 3: top hat \n 4: black hat", window_name, &morph_operator, max_operator, morphology_operations );

/// create trackbar to select kernel type

createtrackbar( "element:\n 0: rect - 1: cross - 2: ellipse", window_name,&morph_elem, max_elem,morphology_operations );

/// create trackbar to choose kernel size

createtrackbar( "kernel size:\n 2n +1", window_name,&morph_size, max_kernel_size,morphology_operations );

/// default start

morphology_operations( 0, 0 );

waitkey(0);

return0;}

/** *@function morphology_operations

*/void morphology_operations( int, void* )

開運算:先腐蝕後膨脹。

閉運算:先膨脹後腐蝕。

形態學梯度:膨脹圖與腐蝕圖之差。

頂帽操作(禮帽操作):原圖與開運算之差。

黑帽操作:閉運算與原圖之差。

用到函式morphologyex()利用基本的膨脹和腐蝕操作,來執行更加高階的形態學變換。

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...

Open Cv 膨脹與腐蝕

影象形態學1 膨脹 dilate 與腐蝕 是灰度與二值影象處理中的重要手段 數學集合論和拓撲集合的基礎上發展起來 膨脹 dilate 與腐蝕 erode 最值濾波 3 3模板?膨脹 3 3模板中的最大值替換中心畫素 腐蝕 3 3模板中的最小值替換中心畫素 膨脹的作用 物件大小增加乙個畫素 3x3 平...