OpenCV開操作與閉操


一、概述

  案例:利用圖像的形態學操作開操作和閉操作對圖片中(二值圖像)指定噪聲進行去除

  腐蝕:用局部極小值替換錨點像素值

  膨脹:用局部極大值替換錨點像素值

  開操作:相當於先腐蝕再膨脹(erode+dilate)

  閉操作:相當於先膨脹再腐蝕(dilate+erode)

  開操作使用的原圖:

  

 

 

  閉操作使用的原圖:

  

 

 

二、示例代碼

  開操作的代碼: 這里需要注意結構元素的大小,在下面的圖中結構元素不能太小不然無法消除大的白色像素快

  Mat src = imread(filePath);
    Mat dst;
    imshow("src",src);
    //定義結構元素
    Mat structureElement = getStructuringElement(MORPH_RECT,Size(15,15),Point(-1,-1));
    erode(src,dst,structureElement,Point(-1,-1));
    imshow("erode",dst);
    //膨脹
    dilate(dst,dst,structureElement,Point(-1,-1));

    imshow("dilate",dst);

 

 

  閉操作代碼:在實際的項目中根據項目的需要動態調整結構元素的大小 

Mat src = imread(filePath);
    Mat dst;
    imshow("src",src);
    //定義結構元素
    Mat stuctureElement = getStructuringElement(MORPH_RECT,Size(7,7),Point(-1,-1));
    //膨脹
    dilate(src,dst,stuctureElement,Point(-1,-1));
    imshow("dilate",dst);
    //腐蝕
    erode(dst,dst,stuctureElement,Point(-1,-1));
    imshow("erode",dst);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM