形态学操作

morphologyEx(src,dst,OPT,kernel)

OPT对应操作,分为以下几类

CV_MOP_OPEN开操作

CV_MOP_CLOSE闭操作

CV_MOP_GRADIENT形态学梯度

CV_MOP_TOPHAT顶帽操作

CV_MOP_BLACKHAT黑帽操作

 

1.开操作

先腐蚀再膨胀

2.闭操作

先膨胀再腐蚀

3.形态学梯度

膨胀-腐蚀(也叫做基本梯度),形态学梯度还包括内部梯度和方向梯度

4.顶帽操作

原图像与开操作的差值

5.黑帽操作

闭操作与原图像的差值

 

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    Mat src,dst_open,dst_close,dst_gra,dst_top,dst_black;
    src = imread("b.jpg");
    if (src.empty())
    {
        cout << "load image failed" << endl;
        return -1;
    }

    imshow("input img", src);
    Mat kernel = getStructuringElement(MORPH_RECT, Size(3,3), Point(-1, -1));
    morphologyEx(src, dst_open, CV_MOP_OPEN, kernel);
    imshow("open", dst_open);
    morphologyEx(src, dst_close, CV_MOP_CLOSE, kernel);
    imshow("close", dst_close);
    morphologyEx(src, dst_gra, CV_MOP_GRADIENT, kernel);
    imshow("gradent", dst_gra);
    morphologyEx(src, dst_top, CV_MOP_TOPHAT, kernel);
    imshow("tophat", dst_top);
    morphologyEx(src, dst_black, CV_MOP_BLACKHAT, kernel);
    imshow("blackhat", dst_black);
    waitKey(0);
    return 0;

}

 

 

 

 

 

 

6,水平线垂直线的提取

分为以下几个步骤:

输入彩色图像

转为灰度图像

转为二值图像

adaptiveThreshould(src,dst,maxValue,adaptiveMethod,threshould,blocksize,c)

参数分别对应输入图像,输出图像,自适应方法(ADAPTIVE_THRESH_MEAN_C,ADAPTIVE_THRESH_GAUSSIAN_C),阈值类型,块大小,常数c

 

定义结构元素

开操作提取水平线和垂直线(~表示取反操作)

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    Mat src,dst;
    src = imread("d.jpg");
    if (src.empty())
    {
        cout << "load image failed" << endl;
        return -1;
    }

    imshow("input img", src);
    Mat gray_src;
    cvtColor(src, gray_src, CV_BGR2GRAY);
    imshow("gray", gray_src);

    Mat binImg;
    adaptiveThreshold(~gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
    imshow("binary img", binImg);
    //水平结构元素
    Mat hline = getStructuringElement(MORPH_RECT, Size(src.cols / 100, 1), Point(-1, -1));
    //垂直结构元素
    Mat wline = getStructuringElement(MORPH_RECT, Size(1, src.rows / 100), Point(-1, -1));

/*
    Mat temp;
    erode(binImg, temp, wline);
    dilate(temp, dst, wline);
*/
    morphologyEx(binImg, dst, CV_MOP_OPEN, hline);

    imshow("final result", dst);
    waitKey(0);
    return 0;

}

 

 

 

 

 

 

利用形态学降噪

 

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    Mat src,dst;
    src = imread("d.jpg");
    if (src.empty())
    {
        cout << "load image failed" << endl;
        return -1;
    }

    imshow("input img", src);
    Mat gray_src;
    cvtColor(src, gray_src, CV_BGR2GRAY);
    imshow("gray", gray_src);

    Mat binImg;
    adaptiveThreshold(~gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
    imshow("binary img", binImg);
    //水平结构元素
    Mat hline = getStructuringElement(MORPH_RECT, Size(src.cols / 100, 1), Point(-1, -1));
    //垂直结构元素
    Mat wline = getStructuringElement(MORPH_RECT, Size(1, src.rows / 100), Point(-1, -1));

    Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
/*
    Mat temp;
    erode(binImg, temp, wline);
    dilate(temp, dst, wline);
*/
    morphologyEx(binImg, dst, CV_MOP_OPEN, kernel);

    imshow("final result", dst);
    waitKey(0);
    return 0;

}

 

 

 

 

posted @ 2019-12-18 17:34  Wangtn  阅读(1308)  评论(0编辑  收藏  举报