https://docs.opencv.org/4.3.0/db/df6/tutorial_erosion_dilatation.html

目录

Goal

Morphological Operations

Dilation 膨胀

Erosion

C/C++

Python

对两函数的理解

窗体上有控制按钮


Goal

In this tutorial you will learn how to:

  • Apply two very common morphological operators: Erosion and Dilation. For this purpose, you will use the following OpenCV functions:

    • cv::erode
    • cv::dilate

Morphological Operations

  • In short: A set of operations that process images based on shapes. Morphological operations apply a structuring element to an input image and generate an output image.
  • The most basic morphological operations are: Erosion and Dilation. They have a wide array of uses, i.e. :
    • Removing noise
    • Isolation of individual elements and joining disparate elements in an image.
    • Finding of intensity bumps or holes in an image

Dilation 膨胀

  • This operations consists of convolving an image A with some kernel ( B), which can have any shape or size, usually a square or circle.
  • The kernel B has a defined anchor point, usually being the center of the kernel.
  • As the kernel B is scanned over the image, we compute the maximal pixel value overlapped by B and replace the image pixel in the anchor point position with that maximal value. As you can deduce, this maximizing operation causes bright regions within an image to "grow" (therefore the name dilation).
  • The dilatation operation is: dst(x,y)=max(x′,y′):element(x′,y′)≠0src(x+x′,y+y′)
  • Take the above image as an example. Applying dilation we can get:

Erosion

  • This operation is the sister of dilation. It computes a local minimum over the area of given kernel.
  • As the kernel B is scanned over the image, we compute the minimal pixel value overlapped by B and replace the image pixel under the anchor point with that minimal value.
  • The erosion operation is: dst(x,y)=min(x′,y′):element(x′,y′)≠0src(x+x′,y+y′)
  • Analagously to the example for dilation, we can apply the erosion operator to the original image (shown above). You can see in the result below that the bright areas of the image get thinner, whereas the dark zones gets bigger.

C/C++

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>using namespace cv;
using namespace std;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 argc, char** argv)
{//CommandLineParser parser(argc, argv, "{@input | LinuxLogo.jpg | input image}");//src = imread(samples::findFile(parser.get<String>("@input")), IMREAD_COLOR);src = imread("NWPUSZ.jpg");resize(src, src, Size(src.cols / 4, src.rows / 4));if (src.empty()){cout << "Could not open or find the image!\n" << endl;cout << "Usage: " << argv[0] << " <Input image>" << endl;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);
}

Python

from __future__ import print_function
import cv2 as cverosion_size = 0
max_elem = 2
max_kernel_size = 21
title_trackbar_element_type = 'Element:\n 0: Rect \n 1: Cross \n 2: Ellipse'
title_trackbar_kernel_size = 'Kernel size:\n 2n +1'
title_erosion_window = 'Erosion Demo'
title_dilatation_window = 'Dilation Demo'def erosion(val):erosion_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_erosion_window)erosion_type = 0val_type = cv.getTrackbarPos(title_trackbar_element_type, title_erosion_window)if val_type == 0:erosion_type = cv.MORPH_RECTelif val_type == 1:erosion_type = cv.MORPH_CROSSelif val_type == 2:erosion_type = cv.MORPH_ELLIPSEelement = cv.getStructuringElement(erosion_type, (2*erosion_size + 1, 2*erosion_size+1), (erosion_size, erosion_size))erosion_dst = cv.erode(src, element)cv.imshow(title_erosion_window, erosion_dst)def dilatation(val):dilatation_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_dilatation_window)dilatation_type = 0val_type = cv.getTrackbarPos(title_trackbar_element_type, title_dilatation_window)if val_type == 0:dilatation_type = cv.MORPH_RECTelif val_type == 1:dilatation_type = cv.MORPH_CROSSelif val_type == 2:dilatation_type = cv.MORPH_ELLIPSEelement = cv.getStructuringElement(dilatation_type, (2*dilatation_size + 1, 2*dilatation_size+1), (dilatation_size, dilatation_size))dilatation_dst = cv.dilate(src, element)cv.imshow(title_dilatation_window, dilatation_dst)src = cv.imread("brid.jpg")
src = cv.resize(src, (int(src.shape[1]/4), int(src.shape[0]/4)))
# width = int(img.shape[1] )
# height = int(img.shape[0])if src is None:print('Could not open or find the image: ')exit(0)cv.namedWindow(title_erosion_window)
cv.createTrackbar(title_trackbar_element_type, title_erosion_window , 0, max_elem, erosion)
cv.createTrackbar(title_trackbar_kernel_size, title_erosion_window , 0, max_kernel_size, erosion)cv.namedWindow(title_dilatation_window)
cv.createTrackbar(title_trackbar_element_type, title_dilatation_window , 0, max_elem, dilatation)
cv.createTrackbar(title_trackbar_kernel_size, title_dilatation_window , 0, max_kernel_size, dilatation)erosion(0)
dilatation(0)
cv.waitKey()

对两函数的理解

膨胀和腐蚀不是用核与图像做卷积运算,将核在图像上滑动,对比核与图像重合的区域找到最大值,并将其赋给锚点的位置则是膨胀

对比核与图像重合的区域找到最小值,则是腐蚀

膨胀和腐蚀核都是0,1两个数值构成,不同类型只是由1形成的图像,如下图则是MORPH_RECT

https://docs.opencv.org/4.3.0/dd/dd7/tutorial_morph_lines_detection.html

窗体上有控制按钮

C/C++上需要控制的变量直接通过

createTrackbar("Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo", &erosion_elem, max_elem, Erosion);

createTrackbar的erosion_elem变化得到,其最大值就是max_elem,erosion_elem这个值会随着窗体上鼠标控制按钮的移动而变化

需要定义全局变量erosion_elem,如果需要控制和添加多个变量则,回调函数Erosion来确定出现在哪个窗口上

ceateTrackbar后面要调用  Erosion(0, 0)函数

python

cv2.createTrackbar(elementType, windowName, 0, max_kernelType, myDilation)
typePos = cv2.getTrackbarPos(elementType, windowName)

通过这两句确定,elementType的作用就是将两者变量的变化和获取关联起来,然后通过getTrackbarPos得知其变化的数值,再使用

Improving Opencv9 Eroding and Dilating 和对opencv窗体上有控制按钮的理解相关推荐

  1. OpenCV 【十八】图像平滑处理/腐蚀与膨胀(Eroding and Dilating)/开闭运算,形态梯度,顶帽,黑帽运算

    图像滤波总结(面试经验总结)https://blog.csdn.net/Darlingqiang/article/details/79507468 目录 part one 图像平滑处理 1原理 2代码 ...

  2. OpenCV之imgproc 模块. 图像处理(1)图像平滑处理 腐蚀与膨胀(Eroding and Dilating) 更多形态学变换 图像金字塔 基本的阈值操作

    图像平滑处理 目标 本教程教您怎样使用各种线性滤波器对图像进行平滑处理,相关OpenCV函数如下: blur GaussianBlur medianBlur bilateralFilter 原理 No ...

  3. OpenCV腐蚀和膨胀Eroding and Dilating

    OpenCV腐蚀和膨胀Eroding and Dilating 腐蚀和膨胀Eroding and Dilating 目标 形态运算 膨胀 侵蚀 代码 解释 腐蚀功能 膨胀功能 结果 腐蚀和膨胀Erod ...

  4. opencv线性插值(上采样)

    1.调用opencv的API pyrUp(src, dst, Size(src.cols * 2, src.rows * 2)); pyrUp:API详解 这里的up是指将图像的尺寸变大,所以原始图像 ...

  5. 如何在 1 秒内将 50 个 OpenCV 帧上传到云存储

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 用例 在这个现代世界中,我认为我们大多数人都熟悉使用计算机视觉应用 ...

  6. opencv 图像上画出目标运动的轨迹_基于opencv的单目和双目标定平台手眼标定

      背景介绍 基于机器视觉引导的智能机器人,在现代社会各个领域已经得到了广泛的应用,尤其是大型工厂的自动化生产线上,视觉机器人可以和基于示教器按照预定轨迹进行作业的机器人互为补充,来共同提高生产的自动 ...

  7. OpenCV windows 上安装

    1.先按照  Anaconda , 有关教程,可以去其他博客查看 2.傻瓜的装Opencv.(我采用的) 在Anaconda  Prompt中输入: conda install -c https:// ...

  8. 【opencv入门篇】 10个程序快速上手opencv【上】

    导言:本系列博客目的在于能够在vs快速上手opencv,理论知识涉及较少,大家有兴趣可以查阅其他博客深入了解相关的理论知识,本博客后续也会对图像方向的理论进一步分析,敬请期待:) PS:官方文档永远是 ...

  9. 十五天掌握OpenCV——图像上的算术运算

    魏老师学生--Cecil:学习OpenCV-机器视觉之旅 图像加法 代码演示 图像混合 代码演示 按位运算 代码演示 Aim:掌握图像的:加法.减法.位运算.cv2.add().cv2.addWeig ...

  10. opencv 图片上画一条线

    1在图片上用鼠标进行操作,opencv主要用到setMouseCallback()函数. winname 窗口名称 onMouse 鼠标事件的回调函数 userdata 传递给回调函数 还有onMou ...

最新文章

  1. mysql 基于gtid复制_深入MySQL复制(二):基于GTID复制
  2. 关于C#应用的授权认证
  3. Java 源代码和 C 源代码的运行区别
  4. sort与sorted
  5. vue router html后缀,vue-router.html
  6. 关于如何实现程序一天只启动一次的想法(C++实现)
  7. win2008下的无线网卡设置
  8. linux的常用操作——程序调试gdb
  9. linux raid autodetect,软raid的建立
  10. PasswordEncoder详解
  11. 如何用VUE从零创建网站
  12. 挂科心得: 驾校重要 教练更加重要(转)
  13. java 测试代码效率_JAVA程序的性能测试方法
  14. 何谓短缺和过剩_关于资源短缺,应用程序服务器和微服务
  15. 手机间高速传输---微传
  16. C++opencv找圆心?看着一篇,一定有你要(边缘轮廓检测,拟合,凸包)找出相应的轮廓或者全部轮廓画外界圆轮廓并且标出轮廓中心
  17. cacheable更新_Spring之缓存注解@Cacheable
  18. linux系统认证中级是什么,目前国内常见的几种Linux认证及其所需价格
  19. [531]微信之wxpy库(基于itchat库)
  20. 软件工程(一)------软件生存周期

热门文章

  1. python读取excel数据生成word_利用Python将excel数据读取到word表格
  2. php中文网是什么需要框架,框架是什么?
  3. 机器学习大作业_机器学习编程作业6-支持向量机(Python版)
  4. android 广告字幕,Android编程实现类似天气预报图文字幕垂直滚动效果的方法
  5. 轮播图的效果实现小米商城和京东商城
  6. 在spring中使用自定义的PropertyEditor
  7. scrapy 爬取百度知道,多spider子一个项目中,使用一个pielines
  8. 拒绝瞎忙,高效的学习与工作经验谈
  9. 构建LVS+Keepalived高可用群集
  10. Centos6.5+Python2.7 +ffmpeg+opencv2自动安装脚本