笔记:OpenCV4.5.4 【c++】膨胀与腐蚀

目录

形态学操作-膨胀

形态学操作-腐蚀 

getStructuringElement(int shape, Size ksize, Point anchor)  

膨胀 dilate(src, dst, kernel) 

腐蚀 erode(src, dst, kernel) 

 动态调整结构元素大小createTrackbar(const String & trackbarname, const String winName,  int* value, int count, Trackbarcallback func, void* userdata=0)


形态学操作-膨胀

跟卷积操作类似,假设有图像A和结构元素B,结构元素B在A上面移动,其中B定义其中心为锚点,计算B覆盖下A的最大像素值用来替换锚点的像素,其中B作为结构体可以是任意形状 

形态学操作-腐蚀 

腐蚀跟膨胀操作的过程类似,唯一不同的是以最小值替换锚点重叠下图像的像素值 

getStructuringElement(int shape, Size ksize, Point anchor)  

- 形状 (MORPH_RECT \MORPH_CROSS \MORPH_ELLIPSE)  

                矩形                          交叉型                     椭圆形

- 大小  

- 锚点 默认是Point(-1, -1)意思就是中心像素 

膨胀 dilate(src, dst, kernel) 

 

腐蚀 erode(src, dst, kernel) 

 动态调整结构元素大小createTrackbar(const String & trackbarname, const String winName,  int* value, int count, Trackbarcallback func, void* userdata=0)

#include <iostream>
#include <cstring>
#include <opencv2/highgui/highgui.hpp>
#include<cmath>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
Mat src, dst;
int pz_size = 0;//膨胀程度
int pzMax_size = 21;//膨胀最大程度
int fs_size = 0;//腐蚀程度
int fsMax_size = 21;//腐蚀程度
//膨胀
void pz(int, void*)
{
    int pzValue = pz_size * 2 + 1;
    //膨胀操作的内核,明确它的形状
    Mat structureElement = getStructuringElement(MORPH_RECT, Size(pzValue, pzValue), Point(-1, -1));
    dilate(src, dst, structureElement);
    imshow("output", dst);

}
//腐蚀
void fs(int, void*)
{
    int fsValue = fs_size * 2 + 1;
    //交叉型
    Mat structureElement = getStructuringElement(MORPH_RECT, Size(fsValue, fsValue), Point(-1, -1));
    erode(src, dst, structureElement);

    imshow("output", dst);
}
//腐蚀与膨胀一同
void fun(int, void*)
{
    int pzValue = pz_size * 2 + 1;
    int fsValue = fs_size * 2 + 1;

    //先进行膨胀
    Mat structureElement = getStructuringElement(MORPH_RECT, Size(pzValue, pzValue), Point(-1, -1));
    dilate(src, dst, structureElement);
    //后进行腐蚀
    structureElement = getStructuringElement(MORPH_RECT, Size(fsValue, fsValue), Point(-1, -1));
    erode(dst, dst, structureElement);
    imshow("output", dst);

}

int main()
{
    src = imread("D:/opencv/2/a.jpg");
    if (!src.data) 
    {
        printf("could not load image...\n");
        return -1;
    }
    namedWindow("input");
    imshow("input", src);

    namedWindow("output");
    createTrackbar("膨胀值", "output", &pz_size, pzMax_size, fun);
    createTrackbar("腐蚀值", "output", &fs_size, fsMax_size, fun);
    pz(0, 0);

    waitKey(0);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值