W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在本教程中,您將學(xué)習(xí)如何:
背景(明亮)擴(kuò)大了字母的黑色地區(qū)。
為了更好地把握想法并避免可能的混亂,在另一個(gè)例子中,我們已經(jīng)將原始圖像倒過(guò)來(lái),如白色的對(duì)象現(xiàn)在是這個(gè)字母。我們已經(jīng)執(zhí)行了兩個(gè)具有大小的矩形結(jié)構(gòu)元素的擴(kuò)張3x3。
左圖:原圖反轉(zhuǎn),右圖:產(chǎn)生擴(kuò)張
膨脹使物體變白。
以相似的方式,通過(guò)對(duì)反轉(zhuǎn)的原始圖像(具有尺寸的矩形結(jié)構(gòu)元素的兩次侵蝕)施加侵蝕操作來(lái)產(chǎn)生相應(yīng)的圖像3x3:
左圖:原圖反轉(zhuǎn),右圖:造成侵蝕
侵蝕使物體變白。
本教程的代碼如下所示。您也可以在這里下載
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
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( int, char** argv )
{
src = imread( argv[1], IMREAD_COLOR );
if( src.empty() )
{ return -1; }
namedWindow( "Erosion Demo", WINDOW_AUTOSIZE );
namedWindow( "Dilation Demo", 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* )
{
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; }
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* )
{
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 ) );
dilate( src, dilation_dst, element );
imshow( "Dilation Demo", dilation_dst );
}
1、這里顯示的大部分材料是微不足道的(如果您有任何疑問(wèn),請(qǐng)參閱前幾節(jié)中的教程)。我們來(lái)看一下程序的一般結(jié)構(gòu):
a、第一個(gè)跟蹤欄 “元素”返回一個(gè)erosion_elem或dilation_ele
b、第二個(gè)跟蹤欄 “內(nèi)核大小”返回對(duì)于相應(yīng)操作的erosion_size或dilation_size。
我們分析這兩個(gè)功能:
2、侵蝕: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; }
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 );
}
執(zhí)行侵蝕操作的功能是cv :: erode。我們可以看到,它有三個(gè)參數(shù):
Mat element = getStructuringElement( erosion_type,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
我們可以為我們的內(nèi)核選擇三種形狀:
那么,我們只需要指定我們的內(nèi)核和錨點(diǎn)的大小。如果未指定,則假定為中心。
3、擴(kuò)張:
代碼如下。你可以看到,它完全類似于侵蝕代碼片段。這里我們還可以選擇定義我們的內(nèi)核,它的錨點(diǎn)和要使用的操作符的大小。
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 ) );
dilate( src, dilation_dst, element );
imshow( "Dilation Demo", dilation_dst );
}
編譯上面的代碼,并以圖像作為參數(shù)執(zhí)行。例如,使用這個(gè)圖像:
我們得到以下結(jié)果。自然地,改變軌道欄中的索引給出不同的輸出圖像。試試吧!甚至可以嘗試添加第三個(gè)Trackbar來(lái)控制迭代次數(shù)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: