morphologyEx(形態(tài)學(xué)操作)

概念

利用morphologyEx這個函數(shù)可以方便的對圖像進行一系列的膨脹腐蝕組合。

函數(shù)講解

●函數(shù)原型
○c++

void morphologyEx( InputArray src, OutputArray dst,
                                int op, InputArray kernel,
                                Point anchor = Point(-1,-1), int iterations = 1,
                                int borderType = BORDER_CONSTANT,
                                const Scalar& borderValue = morphologyDefaultBorderValue() )

○Android

void morphologyEx(Mat src, Mat dst, int op, Mat kernel, Point anchor, int iterations, int borderType, Scalar borderValue)

●參數(shù)解釋
○src:源圖像Mat對象
○dst:目標圖像Mat對象
○op:操作的類型,通過源碼我們得知總共有以下幾種類型:

enum MorphTypes{
    MORPH_ERODE    = 0, //腐蝕
    MORPH_DILATE   = 1, //膨脹
    MORPH_OPEN     = 2, //開操作
    MORPH_CLOSE    = 3, //閉操作
    MORPH_GRADIENT = 4, //梯度操作
    MORPH_TOPHAT   = 5, //頂帽操作
    MORPH_BLACKHAT = 6, //黑帽操作
    MORPH_HITMISS  = 7  
};

○kernel:用于膨脹操作的結(jié)構(gòu)元素,如果取值為Mat(),那么默認使用一個3 x 3 的方形結(jié)構(gòu)元素,可以使用getStructuringElement()來創(chuàng)建結(jié)構(gòu)元素
○anchor:參考點,其默認值為(-1,-1)說明位于kernel的中心位置。
○borderType :邊緣類型,默認為BORDER_CONSTANT。
○borderValue :邊緣值,用它的默認值即可。

各種操作的效果

●MORPH_ERODE(腐蝕)
erode(腐蝕)函數(shù)效果一樣
●MORPH_DILATE(膨脹)
dilate(膨脹)函數(shù)效果一樣
●MORPH_OPEN(開)
其實內(nèi)部就是進行了先腐蝕后膨脹的操作。

源圖像

操作后圖像

●MORPH_CLOSE(閉)
其實內(nèi)部就是進行了先膨脹后腐蝕的操作。
源圖像

操作后圖像

●MORPH_GRADIENT(梯度)
內(nèi)部是膨脹減去腐蝕
源圖像

操作后的圖像

●MORPH_TOPHAT(頂帽)
源圖像

操作后的圖像

●MORPH_BLACKHAT(黑帽)
源圖像

操作后的圖像

函數(shù)使用

●c++中

#include<opencv2/opencv.hpp>
using namespace cv;
int main() {
    Mat src = imread("C:/Users/Administrator/Desktop/wan.png");//引入源圖像
    if (src.empty()) {
        return -1;
    }
    imshow("src", src);//展示源圖像
    Mat dst;
    Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3));//創(chuàng)建結(jié)構(gòu)元素大小為3*3
    morphologyEx(src, dst,MORPH_OPEN, kernel);//開操作
    imshow("dst", dst);//展示目標圖像
    waitKey(0);
    return 0;
}

●Android中

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_beauty);//獲取源圖像Bitmap對象
Mat src = new Mat();
Mat dst = new Mat();
Utils.bitmapToMat(bitmap,src);//Bitmap轉(zhuǎn)換為Mat對象
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));//獲取結(jié)構(gòu)元素
Imgproc.morphologyEx(src,dst,Imgproc.MORPH_OPEN,kernel);//開操作
Utils.matToBitmap(dst,bitmap);//Mat轉(zhuǎn)換為Bitmap對象
imageView.setImageBitmap(bitmap);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容